Last active
April 12, 2016 06:51
-
-
Save maggocnx/8dc2fbd22d257fa3fd36a43604ee1173 to your computer and use it in GitHub Desktop.
Painting on Canvas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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