-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathWriting.qml
More file actions
100 lines (83 loc) · 2.46 KB
/
Writing.qml
File metadata and controls
100 lines (83 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import QtQuick 2.2
import "js/shortstraw.js" as Straw
Canvas {
id:canvas
property int paintX
property int paintY
property int count: 0
property int strokes: 0
property int lineWidth: 5
property string drawColor: "black"
property variant myArray: []
// required to work (handwritingEngine)
property QtObject engine
function addItem(x, y) {
// Properties in QML are not real JavaScript arrays,
// but sort of pretend to be.
// They cannot be mutated.
// Workaround :
var tmp = myArray;
tmp.push({'x':x, 'y':y})
myArray = tmp;
}
MouseArea {
id:mousearea
hoverEnabled:true
anchors.fill: parent
onClicked: drawPoint();
onPositionChanged: {
requestPaint()
}
onPressed: {
paintX = mouseX;
paintY = mouseY;
}
onReleased: {
var array = Straw.shortStraw(myArray);
var ctx = getContext("2d")
ctx.beginPath();
ctx.strokeStyle = 'red';
ctx.moveTo(array[0].x, array[0].y);
ctx.lineWidth = 2;
for (var i = 0; i < array.length; i++) {
// console.log("strokes "+strokes+": " + array[i].x + ", "+ array[i].y );
engine.query(strokes, array[i].x, array[i].y);
if (i > 0)
ctx.lineTo(array[i].x, array[i].y);
}
ctx.stroke();
ctx.closePath();
myArray = [];
strokes++;
}
}
onPaint: {
// draw segments
if (mousearea.pressed) {
var ctx = getContext("2d")
ctx.strokeStyle = drawColor
ctx.lineWidth = lineWidth
ctx.beginPath()
ctx.moveTo(paintX, paintY)
paintX = mousearea.mouseX;
paintY = mousearea.mouseY;
ctx.lineTo(mousearea.mouseX, mousearea.mouseY)
ctx.stroke()
ctx.closePath()
addItem(paintX, paintY)
}
}
function drawPoint() {
var ctx = canvas.getContext("2d")
ctx.lineWidth = lineWidth
ctx.fillStyle = drawColor
ctx.fillRect(mousearea.mouseX, mousearea.mouseY, 2, 2);
}
// reset everything
function clear() {
var ctx = canvas.getContext("2d")
strokes = 0;
engine.clear();
ctx.clearRect(0, 0, width, height);
}
}