Canvas Paint
The colors are stored in an array and the paint color. When one the user clicks in one of the squares the color of the square is assigned to the paintColor
property of the row named colorTools
.
Canvas {
id: canvas
anchors {
left: parent.left
right: parent.right
top: colorTools.bottom
bottom: parent.bottom
margins: 8
}
property real lastX
property real lastY
onPaint: {
var ctx = getContext('2d')
ctx.lineWidth = 1.5
ctx.strokeStyle = canvas.color
ctx.beginPath()
ctx.moveTo(lastX, lastY)
lastX = area.mouseX
lastY = area.mouseY
ctx.lineTo(lastX, lastY)
ctx.stroke()
MouseArea {
id: area
onPressed: {
canvas.lastX = mouseX
canvas.lastY = mouseY
}
onPositionChanged: {
canvas.requestPaint()
}
}
A mouse press stores the initial mouse position into the lastX
and lastY
properties. Every change on the mouse position triggers a paint request on the canvas, which will result in calling the onPaint
handler.