13 property alias buffer: buffer
18 property int brushSize : 16
23 property real brushOpacity : 1
28 property int brushShape : 1
33 property int maxBrushSize: 100
38 property color paintColor: "red"
58 property color paintColor: control.paintColor
63 var bufferCtx = buffer.getContext(
"2d")
64 bufferCtx.clearRect(0, 0, width, height)
72 propagateComposedEvents:
false
75 property int spacing: 32
77 property real deltaDab: Math.max(spacing / 100 * control.brushSize, 1)
78 property var points: []
79 property point lastDrawPoint
80 property point startPos
81 property point finalPos
82 property real brushAngle: 0
86 var point =
Qt.point(mouseX, mouseY)
89 startPos =
Qt.point(point.x, point.y)
90 finalPos =
Qt.point(point.x, point.y)
92 if (control.brushShape != 1)
107 drawDab(
Qt.point(mouseX, mouseY))
110 var currentPoint =
Qt.point(mouseX, mouseY)
111 var startPoint = lastDrawPoint
115 if (control.brushShape == 1) {
118 if ( (currentPoint.x > startPoint.x))
123 brushAngle = find_angle(
Qt.point(startPoint.x, startPoint.y-10),startPoint, currentPoint)
130 brushAngle = - find_angle(
Qt.point(startPoint.x, startPoint.y-10),startPoint, currentPoint)
139 var currentSpacing = Math.sqrt(Math.pow(currentPoint.x - startPoint.x, 2) + Math.pow(currentPoint.y - startPoint.y, 2))
140 var numDabs = Math.floor(currentSpacing / deltaDab)
142 if (points.length == 1 || numDabs < 3) {
143 var endPoint = currentPoint
145 var controlPoint = points[points.length - 1]
146 endPoint =
Qt.point((controlPoint.x + currentPoint.x) / 2, (controlPoint.y + currentPoint.y) / 2)
149 var deltaT = 1 / numDabs
150 var betweenPoint = startPoint
153 while (t > 0 && t <= 1) {
154 var point = bezierCurve(startPoint, controlPoint, endPoint, t)
155 var deltaPoint = Math.sqrt(Math.pow(point.x - betweenPoint.x, 2) + Math.pow(point.y - betweenPoint.y, 2))
157 if (diff && Math.abs(deltaPoint - deltaDab) > Math.abs(diff)) {
break; }
158 diff = deltaPoint - deltaDab
159 if (Math.abs(diff <= 0.5)) {
165 t -= diff / deltaDab * deltaT
168 points.push(currentPoint)
169 lastDrawPoint = betweenPoint
176 var bufferCtx = buffer.getContext(
"2d")
180 var bufferImage = bufferCtx.getImageData(0, 0, width, height)
187 buffer.requestPaint()
192 function drawDab(point)
194 var ctx = buffer.getContext(
"2d")
213 var size = control.brushSize
214 ctx.fillStyle =
Qt.rgba(control.paintColor.r, control.paintColor.g, control.paintColor.b, control.brushOpacity);
215 var x = point.x - size / 2
216 var y = point.y - size / 2
218 if (control.brushShape == 0)
223 ctx.arc(x, y, size/2 ,0,Math.PI*2,
true);
227 ctx.translate(x+size/2,y+size/2)
229 ctx.rotate(brushAngle)
230 ctx.roundedRect(-size/4, -size/8, size/2, size/4, 2, 2)
234 buffer.requestPaint()
243 function bezierCurve(
start, control, end, t)
253 x = Math.pow((1 - t), 2) *
start.x + 2 * t * (1 - t) * control.x + t * t *
end.x
254 y = Math.pow((1 - t), 2) *
start.y + 2 * t * (1 - t) * control.y + t * t *
end.y
256 return Qt.point(x, y)
260 function rgbToHex(r, g, b)
262 if (r > 255 || g > 255 || b > 255)
263 throw "Invalid color component";
264 return ((r << 16) | (g << 8) | b).toString(16);
267 function find_angle(A,B,C) {
268 var AB = Math.sqrt(Math.pow(B.x-A.x,2)+ Math.pow(B.y-A.y,2));
269 var BC = Math.sqrt(Math.pow(B.x-C.x,2)+ Math.pow(B.y-C.y,2));
270 var AC = Math.sqrt(Math.pow(C.x-A.x,2)+ Math.pow(C.y-A.y,2));
271 return Math.acos((BC*BC+AB*AB-AC*AC)/(2*BC*AB));
Q_SCRIPTABLE Q_NOREPLY void start()
QAction * end(const QObject *recvr, const char *slot, QObject *parent)