Skip to content

Instantly share code, notes, and snippets.

@sirdarthvader
Last active April 1, 2018 23:38
Show Gist options
  • Save sirdarthvader/0e5c0afeb52def6fb74848cebd848fd0 to your computer and use it in GitHub Desktop.
Save sirdarthvader/0e5c0afeb52def6fb74848cebd848fd0 to your computer and use it in GitHub Desktop.
function draw (e) {
if(!isDrawing) return ;
console.log(e);
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
[lastX, lastY] = [e.offsetX, e.offsetY];
}
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mousedown', (e) => {
isDrawing = true;
[lastX, lastY] = [e.offsetX, e.offsetY];
});
canvas.addEventListener('mouseup', ()=> isDrawing = false);
canvas.addEventListener('mouseout', () => isDrawing = false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment