Skip to content

Instantly share code, notes, and snippets.

@sirdarthvader
Last active April 1, 2018 23:52
Show Gist options
  • Save sirdarthvader/6edf8ab9eb359e86b99e9ec65abed548 to your computer and use it in GitHub Desktop.
Save sirdarthvader/6edf8ab9eb359e86b99e9ec65abed548 to your computer and use it in GitHub Desktop.
let hue = 0;
let direction = true;
function draw (e) {
if(!isDrawing) return ;
console.log(e);
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
[lastX, lastY] = [e.offsetX, e.offsetY];
hue++;
if(hue>=360){
hue = 0;
}
if(ctx.lineWidth >= 75 || ctx.lineWidth <= 1) {
direction = !direction;
}
if(direction){
ctx.lineWidth++;
} else {
ctx.lineWidth = 1;
}
}
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