Skip to content

Instantly share code, notes, and snippets.

@richarddprasad
Created March 26, 2020 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richarddprasad/b969b32d231a4fe80a17100f60f30ee2 to your computer and use it in GitHub Desktop.
Save richarddprasad/b969b32d231a4fe80a17100f60f30ee2 to your computer and use it in GitHub Desktop.
function handleMouseMove(evt: MouseEvent) {
if (mouseDown && context) {
start = {
x: end.x,
y: end.y,
};
end = {
x: evt.clientX - canvasOffsetLeft,
y: evt.clientY - canvasOffsetTop,
};
// Draw our path
context.beginPath();
context.moveTo(start.x, start.y);
context.lineTo(end.x, end.y);
context.strokeStyle = `#${randomColor()}`;
context.lineWidth = 3;
context.stroke();
context.closePath();
}
}
function randomColor(): string {
const color = new Array<string>(6);
for (let i = 0; i < 6; i++) {
const val = Math.floor(Math.random() * 16);
if (val < 10) {
color[i] = val.toString();
} else {
color[i] = String.fromCharCode(val + 87);
}
}
return color.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment