Skip to content

Instantly share code, notes, and snippets.

@queerviolet
Last active February 9, 2025 15:33
Show Gist options
  • Save queerviolet/e43ebafdc8eb3c279727 to your computer and use it in GitHub Desktop.
Save queerviolet/e43ebafdc8eb3c279727 to your computer and use it in GitHub Desktop.
ink pad widget
<!doctype html>
<html>
<head>
<script src="inkpad.js"></script>
<style>
body {
margin: 0; padding: 0;
}
ink-pad#ink {
position: absolute;
left: 0; right: 50%; top: 0; bottom: 0;
background: fuchsia;
}
ink-pad#moreink {
position: absolute;
left: 50%; right: 0; top: 0; bottom: 0;
background: #7ef;
}
</style>
</head>
<body>
<ink-pad id="ink"></ink-pad>
<ink-pad id="moreink"></ink-pad>
<script>
// You can play with the canvases in the console. I thought this was
// pretty:
ink.ctx.lineWidth = 10;
ink.ctx.strokeStyle = 'rgba(119, 238, 255, 0.05)';
moreink.ctx.lineWidth = 10;
moreink.ctx.strokeStyle = 'rgba(255, 0, 255, 0.05)';
</script>
</body>
</html>
// inkPad < HTMLElement
var inkPad = Object.create(HTMLElement.prototype);
inkPad.createdCallback = function() {
this.canvas = document.createElement('canvas');
this.canvas.height = this.clientHeight;
this.canvas.width = this.clientWidth;
this.appendChild(this.canvas);
this.ctx = this.canvas.getContext('2d');
var drawing = false;
this.addEventListener('mousedown', function(event) {
drawing = true;
this.ctx.beginPath();
// event.offsetX! offsetX, not just x! x isn't even
// well defined, apparently.
//
// The browser wants you dead, man.
// http://www.quirksmode.org/dom/w3c_cssom.html#mousepos
this.ctx.moveTo(event.offsetX, event.offsetY);
});
this.addEventListener('mousemove', function(event) {
if (drawing) {
this.ctx.lineTo(event.offsetX, event.offsetY);
this.ctx.stroke();
}
});
this.addEventListener('mouseup', function(event) {
drawing = false;
});
}
document.registerElement('ink-pad', {prototype: inkPad});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment