summaryrefslogtreecommitdiffstats
path: root/examples/painting/Drawing.qml
blob: a19da45645e76348641558705051c99bbb9fcc54 (plain)
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
import "../../Canvas"
import Qt 4.7


Canvas {
    id:canvas
    color: "white"
    property int paintX
    property int paintY
    property int count: 0
    property int lineWidth: 2
    property variant drawColor: "black"

    MouseArea {
        id:mousearea
        hoverEnabled:true
        anchors.fill: parent
        onClicked: drawPoint();
        onPositionChanged:  {
            if (mousearea.pressed)
                drawLineSegment();
            paintX = mouseX;
            paintY = mouseY;
        }
    }

    function drawLineSegment() {
        var ctx = getContext();
        ctx.save();
        ctx.beginPath();
        ctx.strokeStyle = drawColor
        ctx.lineWidth = lineWidth
        ctx.moveTo(paintX, paintY);
        ctx.lineTo(mousearea.mouseX, mousearea.mouseY);
        ctx.stroke();
        ctx.closePath();
        ctx.restore();
    }

    function drawPoint() {
        var ctx = getContext();
        ctx.save();
        ctx.lineWidth = lineWidth
        ctx.fillStyle = drawColor
        ctx.fillRect(mousearea.mouseX, mousearea.mouseY, 2, 2);
        ctx.restore();
    }

    function clear() {
        var ctx = getContext();
        ctx.clearRect(0, 0, width, height);
    }
}