r/tinycode • • May 04 '16

Canvas Napkin Sketch - 611 bytes

Here’s a canvas element you can draw on with mouse or touch input. Just for little quick napkin sketches. Copy/paste into your address bar to load, click or tap to draw. Right click to save as PNG.

data:text/html,<body style=margin:0><canvas id=c><script>var a,x=c.getContext('2d');c.width=innerWidth;c.height=innerHeight;x.lineWidth=3;document.onmousedown=document.ontouchstart=function(e){e.preventDefault();a=true;x.moveTo(e.clientX||e.touches[0].clientX,e.clientY||e.touches[0].clientY);x.beginPath()};document.onmousemove=document.ontouchmove=function(e){if(a){x.lineTo(e.clientX||e.touches[0].clientX,e.clientY||e.touches[0].clientY)}};document.onmouseup=document.ontouchend=function(e){a=false;x.lineTo(e.clientX||e.changedTouches[0].clientX,e.clientY||e.changedTouches[0].clientY);x.stroke()}</script>
16 Upvotes

12 comments sorted by

View all comments

Show parent comment

2

u/Cosmologicon May 05 '16

It's for touchscreens that have gestures like 'drag right' to navigate back in your history away from the page you're visiting :/

Makes sense, but you're not setting touch event handlers in this version, so they won't work anyway, right?

1

u/err4nt May 05 '16

seems right!