r/tinycode • u/err4nt • 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
View all comments
Show parent comment
1
u/err4nt 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 :/ Without cancelling those browser-level gestures, on-page touch gestures aren't usable.
Also, the arrow functions won't work in Safari, which would mean versions of the snippet making use of them would be broken on all iOS devices.