Feature Library

Pen ink that tidies into shapes

0.21 CPU ms
A still of the Pen ink that tidies into shapes demo
Still
The live demo starts when it’s in view.Write with a mouse, a finger or a pen · draw a rough circle, box, triangle, line or arrow · Ctrl Z undoes a stroke, T toggles tidying

Freehand ink from a mouse, a finger or a pen.

What it costs a frame

0.21 ms
GPU
not used
CPU, median
0.21 ms
CPU, 95th pct
0.42 ms
Draw calls
0
Weight
Light

Measured 13 November 2026 on RTX 4060 Laptop, 13th Gen Intel(R) Core(TM) i9-13900H, Chrome 157, 1920×1080 at 1×. 600 frames × 3 rounds, median kept (rounds ranged CPU 0.14–0.21 ms). What ran: Measured writing by itself: the seven sample strokes drawn point by point, recognised and morphed into shapes, then the sheet cleared and drawn again. Idle, nothing is drawn. Phones: tiers are being measured next.

On every machine it was measured on
MachineGPU msCPU msCPU p95
RTX 4060 Laptop · 13th Gen Intel(R) Core(TM) i9-13900Hreference · measuring toolnot timed0.210.42
apple · Apple A19 Proin a browser tab · 16 November 2026not timed0.300.46
Intel Arc 140V (Xe2-LPG) · Intel Core Ultra 7 258Vin a browser tab · 15 November 2026not timed0.240.55

Bars against one 16.7 ms frame. A browser-tab run is timed in a visible tab without the tool’s scripted input, so read it as close, not exact.

What it needs

Renders with
Canvas 2DA 2D canvas: every current browser.
Your browser
Runs hereNo WebGPU needed
Depends on
NothingNo other packages

What it’s for

  1. A pen or highlighter on a whiteboard or canvas app
  2. Sketching diagrams that come out clean
  3. Annotating screenshots, slides or documents
  4. Signatures and handwritten notes
  5. A stylus on a tablet with the palm resting on the screen
  6. Ink saved or exported as SVG

Pitfalls, in brief

  1. Read pressure only from pens (pointerType === "pen"): a mouse reports 0.5 while pressed and many touch screens 0 or 1, so pressure from them gives ink that is half or doubly wide. pressureOf does this.
  2. Recognise the whole smoothed path (raw from InkRecorder.end), not the simplified points: simplification is tuned to look the same, and drops the very wobble that tells a circle from a polygon.
  3. Smoothing trails the pointer: with a lag of 0.45 the last smoothed point stops short of where the pen lifted.
  4. Fill a stroke once, as one outline (traceInk traces every capsule clockwise, so the non-zero rule joins them).
  5. Simplify in screen pixels (tolerance / zoom): a fixed tolerance in world units throws detail away when zoomed in and keeps too much when zoomed out.
6 more in brief
  1. Use getCoalescedEvents for pens: a pen samples at 120–240 Hz but pointermove fires once a frame, and the rest are only in the coalesced list.
  2. Recognition is deliberately conservative, and should stay so: a wrong guess that turns writing into a box is far worse than a shape left as ink.
  3. Check a new stroke against nearby lines for an arrow head before recognising it on its own: a small "V" alone can pass for a tiny arrow.
  4. Once a pen has touched the sheet, ignore touch pointers for ink: a palm resting on a tablet otherwise draws.
  5. End the stroke on lostpointercapture too: an up the canvas never sees otherwise leaves the pointer down, so its hover moves ink and its next press is taken for a second finger.
  6. Set touch-action: none on the canvas, or a finger’s stroke scrolls the page and the pointer is cancelled mid-stroke.
11 pitfalls in full, the hook-up guide, the module source and its tests come with the part.

Where it comes from

From SpaciousPro. Fully custom code.

Ramer–Douglas–Peucker line simplification (Ramer 1972; Douglas & Peucker 1973): simplifyStroke, and the corner and turn finding in the recogniser

Lehmer’s multiplicative generator (the Park–Miller “minimal standard”, 1988): the seeded wobble of the demo’s sample strokes and of the tests’ hand-drawn paths, as in the source tests

inkpenstyluspressurefreehandhandwritingsmoothingsimplificationramer-douglas-peuckershape recognitionquick shapearrowsnapmorphhighlighterpalm rejectionpointer eventscoalesced eventscanvas 2dsvg pathundoaccessibilityno dependencies

Near it in the frame