Skip to content

Instantly share code, notes, and snippets.

@ShadowPower
Last active October 11, 2019 16:35
Show Gist options
  • Save ShadowPower/66ba79453e05e0a7e1a58f7b86a0754b to your computer and use it in GitHub Desktop.
Save ShadowPower/66ba79453e05e0a7e1a58f7b86a0754b to your computer and use it in GitHub Desktop.
QML doodle canvas
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
id: msPaint
visible: true
width: 640
height: 480
title: qsTr("MoeSoftPaint")
property real lastPosX
property real lastPosY
property bool drawing: false
Canvas {
id: paintCanvas
anchors.fill: parent
onPaint: {
var ctx = getContext("2d");
ctx.strokeStyle = 'rgba(0, 128, 128, 1)';
if (drawing) {
let curPosX = mouse.mouseX;
let curPosY = mouse.mouseY;
ctx.beginPath();
ctx.moveTo(lastPosX, lastPosY);
ctx.lineTo(curPosX, curPosY);
ctx.stroke();
ctx.closePath();
lastPosX = curPosX;
lastPosY = curPosY;
}
}
MouseArea {
id: mouse
anchors.fill: parent
onPressed: {
lastPosX = mouseX;
lastPosY = mouseY;
drawing = true;
}
onReleased: {
drawing = false;
}
onPositionChanged: {
paintCanvas.requestPaint();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment