HTML Canvas Drawing Playground — Pointer Events Example
A small drawing surface with pointer input and an editable brush.
The live editor loads here with JavaScript enabled.
Run in Playground ↗The original source and explanation are available below.
Changes here stay separate from your main playground. Use Open in TryJS inside the editor to keep working with your changes.
Useful? Help keep TryJS free and independent.
Support TryJSWhat to expect
Dragging a mouse, pen or finger draws a continuous line. Clear resets the canvas.
About this example
The Canvas 2D API provides a drawing surface for tools, games and visual experiments. Pointer events give mouse, touch and pen input a shared interface. This small example keeps the drawing entirely in the preview and does not upload it.
How It Works
Start and continue a path
pointerdown starts a path; pointermove extends and strokes it while the pointer is pressed. Pointer capture keeps the gesture connected when it leaves the canvas.
Map the pointer to the canvas
The coordinates are scaled from the displayed rectangle into canvas pixels. touch-action: none lets drawing gestures stay on the canvas.
Read the original source
This is the starting code. Your edits stay in the live editor above.
HTML
<main><h1>A tiny drawing pad</h1><p>Drag to draw. Try a mouse, pen or finger.</p><canvas width="600" height="280" aria-label="Drawing surface"></canvas><p><button id="clear">Clear drawing</button></p></main>CSS
* { box-sizing: border-box; }
body { margin: 0; padding: 24px; background: #faf8f0; color: #222; font: 15px/1.6 system-ui, sans-serif; }
h1 { font-size: 24px; line-height: 1.2; margin: 0 0 12px; }
p { margin: 8px 0 16px; }
button, input { font: inherit; }
button { padding: 9px 16px; border: 1px solid #b9a200; border-radius: 7px; background: #f0d000; color: #222; cursor: pointer; }
button:disabled { opacity: .6; cursor: wait; }
input { max-width: 100%; padding: 9px 12px; border: 1px solid #aaa; border-radius: 7px; }
small { color: #595959; }
output { font-variant-numeric: tabular-nums; }
canvas { display: block; width: 100%; max-width: 600px; border: 1px solid #aaa; border-radius: 10px; background: white; touch-action: none; }JavaScript
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
ctx.lineWidth = 5;
ctx.lineCap = "round";
ctx.strokeStyle = "#5b48d6";
let drawing = false;
function point(event) {
const box = canvas.getBoundingClientRect();
return [(event.clientX - box.left) * canvas.width / box.width,
(event.clientY - box.top) * canvas.height / box.height];
}
canvas.addEventListener("pointerdown", event => {
drawing = true;
canvas.setPointerCapture(event.pointerId);
ctx.beginPath();
ctx.moveTo(...point(event));
});
canvas.addEventListener("pointermove", event => {
if (!drawing) return;
ctx.lineTo(...point(event));
ctx.stroke();
});
for (const type of ["pointerup", "pointercancel"]) {
canvas.addEventListener(type, () => { drawing = false; });
}
document.querySelector("#clear").addEventListener("click", () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
});