Skip to content

Instantly share code, notes, and snippets.

@maggocnx
Last active April 12, 2016 06:51
Show Gist options
  • Save maggocnx/8dc2fbd22d257fa3fd36a43604ee1173 to your computer and use it in GitHub Desktop.
Save maggocnx/8dc2fbd22d257fa3fd36a43604ee1173 to your computer and use it in GitHub Desktop.
Painting on Canvas
function init() {
canvas = document.getElementById('can');
ctx = canvas.getContext("2d");
w = canvas.width;
h = canvas.height;
canvas.addEventListener("touchmove", function (e) {
findxy('move', e)
}, false);
canvas.addEventListener("touchstart", function (e) {
findxy('down', e)
}, false);
canvas.addEventListener("touchend", function (e) {
findxy('up', e)
// print();
}, false);
}
function draw() {
ctx.beginPath();
ctx.moveTo(prevX, prevY);
ctx.lineTo(currX, currY);
ctx.strokeStyle = x;
ctx.lineWidth = y;
ctx.stroke();
ctx.closePath();
}
function findxy(res, e) {
if (res == 'down') {
prevX = currX;
prevY = currY;
currX = e.changedTouches[0].clientX - canvas.offsetLeft;
currY = e.changedTouches[0].clientY - canvas.offsetTop;
flag = true;
dot_flag = true;
if (dot_flag) {
ctx.beginPath();
ctx.fillStyle = x;
ctx.fillRect(currX, currY, 2, 2);
ctx.closePath();
dot_flag = false;
}
}
if (res == 'up' || res == "out") {
flag = false;
}
if (res == 'move') {
if (flag) {
prevX = currX;
prevY = currY;
currX = e.changedTouches[0].clientX- canvas.offsetLeft;
currY = e.changedTouches[0].clientY- canvas.offsetTop;
draw();
}
}
}
function init() {
canvas = document.getElementById('can');
ctx = canvas.getContext("2d");
w = canvas.width;
h = canvas.height;
canvas.addEventListener("touchmove", function (e) {
findxy('move', e)
}, false);
canvas.addEventListener("touchstart", function (e) {
findxy('down', e)
}, false);
canvas.addEventListener("touchend", function (e) {
findxy('up', e)
// print();
}, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment