DEV Community

Cover image for What I learned wiring up a client-side signature pad (and the resize bug that wiped signatures)
Abdulkhaleq Mohamed
Abdulkhaleq Mohamed

Posted on

What I learned wiring up a client-side signature pad (and the resize bug that wiped signatures)

I needed a signature pad for RapidKit Tools, a set of free browser-based tools I'm building. Most online signature tools upload the image to a server. A signature is personal, so I wanted the drawing to stay on the user's device: no upload, no account.

I didn't write the drawing engine from scratch. The tool is built on react-signature-canvas, a React wrapper around signature_pad. Reading through the source taught me more than I expected, so here is what's actually going on under the hood.

Smooth lines are curves, not lineTo

Connecting raw pointer coordinates with straight segments looks jagged. signature_pad buffers recent points, computes control points from them, and draws cubic Bézier curves. It also varies the stroke width with pointer velocity (faster movement gives a thinner line), and it rasterizes each curve by stepping along it and drawing small filled circles.

High-DPI canvases

The wrapper sizes the canvas bitmap to offsetWidth * devicePixelRatio and scales the 2D context to match, so strokes stay sharp on phone screens. If you build your own canvas tool, forgetting this step is the classic cause of blurry output.

Transparent, cropped PNGs

Exporting a signature as JPEG gives you a white box when it's pasted into a document. The canvas here has no background fill, so toDataURL("image/png") keeps the alpha channel. trim-canvas then scans the pixels and crops the image to the bounding box of what was actually drawn.

The bug: resizing wiped the signature

By default, the wrapper clears the canvas whenever the window resizes. On mobile, a resize happens when the browser's URL bar collapses or when you rotate the screen. A signature could silently disappear while my React state still said "not empty", so the Copy and Download buttons stayed enabled and did nothing.

What I changed:

  1. Set clearOnResize={false}.
  2. On resize, snapshot the strokes with toData(), resize the backing store, rescale the points, and restore them with fromData().
  3. Derive the "empty" state from the canvas itself (isEmpty()) instead of a flag I set by hand.
  4. For the clipboard, use canvas.toBlob() and hand the blob to ClipboardItem, and show an error state if copying fails instead of only logging to the console.

What I got wrong

I described the code before reading it. My first draft of this post, written with AI help, said the tool used Pointer Events and quadratic curves. That was a plausible guess about how a signature pad works, not a description of this one. When I read the library's source, it turned out to use mouse and touch events and cubic Béziers. I rewrote the post after reading the code.

I only checked the first version on desktop. It looked fine in my browser. On my phone, the drawing area was a narrow pill: stacked paddings and large border radii ate the width, strokes were clipped near the rounded edges, and the "Copy to Clip" label wrapped onto three lines. I fixed it with a taller mobile aspect ratio, smaller radii, and full-width stacked buttons, then checked it on the phone again.

I trusted a local flag as the source of truth. The tool tracked "is the canvas empty" in a React state variable set when drawing started. Reading the code showed the problem: the library clears the canvas on resize, but my flag would still say "not empty", so Copy and Download stayed enabled with nothing to export. I found that by having an AI coding agent report what the component does, with file and line references, before I changed anything. The fix was to ask the canvas itself (isEmpty()) instead of keeping a copy of its state.

Limits worth knowing

  • The library uses mouse and touch events, not Pointer Events, so there's no stylus pressure support.
  • The export is a raster PNG, not SVG.
  • The signature itself is never sent anywhere. The site does load Google Analytics for page views, so I only make that claim about the drawing.

Try it

getrapidkit.me/tools/design/signature-pad is free, with no sign-up. If you've built canvas tools, I'd like to hear how you handle resize and smoothing, and whether you hit problems I haven't found yet.

Top comments (4)

Collapse
 
browsercraft profile image
Browser Craft •

The resize + isEmpty mismatch is a nasty one — the backing store gets wiped (mobile URL bar collapse, rotation) while React still thinks the pad is dirty, so Copy/Download stay enabled and quietly no-op. Setting clearOnResize={false}, snapshotting with toData(), resizing, rescaling points, and restoring with fromData() is the right shape.

One extra case I’d watch: a window moving between a laptop panel and an external monitor, which can change devicePixelRatio without a phone rotation and will also resize the bitmap. Deriving emptiness from isEmpty() instead of a hand-set flag should catch that too.

Also appreciate the rewrite after reading the library. Cubic Béziers plus mouse/touch events (and velocity-based stroke width) are easy to miss if you only look at the wrapper API — especially after a first draft that guessed Pointer Events and quadratic curves.

For the clipboard path, toBlob() + ClipboardItem is the right modern write. On some WebKit versions I’ve still needed a fallback “download PNG” when the image clipboard write fails. Did toBlob() cover the phones you tested, or did you hit that?

Collapse
 
abdelkhalek2 profile image
Abdulkhaleq Mohamed •

Thanks, this is a useful comment.

The monitor-switch case is one I haven't tested. The resize handler compares both the container size and devicePixelRatio before it snapshots the strokes with toData(), resizes the bitmap, and restores them with fromData(), so it should cover a ratio change as long as the browser fires a resize event. I've tested screen rotation on my phone, but not moving a window between a laptop panel and an external monitor.

On the clipboard: I tested on an Oppo Reno 7 5G (Chrome on Android) and on desktop. Copy worked on the phone, and the drawing survived rotation. I haven't tested WebKit or iOS, so I can't say whether toBlob() + ClipboardItem covers it there. If the write fails, the button currently shows a "Copy Failed" state and the user has to click Download manually. An automatic download fallback is a good idea, and I'm adding it.

Collapse
 
komopdf profile image
KomoPDF •

Handling the mobile URL bar collapse with toData() and point rescaling is a solid fix.

Two additional things we ran into when building browser canvas signing that might save you some edge-case debugging down the road:

  1. Normalized coordinates instead of pixel points: Storing strokes as unit floats (x / width, y / height) rather than raw canvas pixels makes resize and orientation changes seamless. When the canvas dimensions change, or if you later render the signature at a different scale inside a document preview, you do not need to compute scale deltas. You just replay the Bézier control points multiplied by the current dimensions.

  2. The touch-action CSS collision: On mobile Safari and Android Chrome, fast diagonal strokes often clash with pull-to-refresh or page overscroll if the canvas lacks touch-action: none. Without that rule, a quick stroke can trigger a browser pan gesture mid-draw, leaving clipped or orphaned points.

Also, because signature_pad already calculates cubic Bézier segments, you are only one step away from generating an SVG path. Exporting vector paths rather than raster PNGs keeps signatures crisp at any zoom level without bitmap pixelation.

Collapse
 
abdelkhalek2 profile image
Abdulkhaleq Mohamed •

Thanks — this is exactly the kind of feedback that saves future-me an afternoon of debugging. 🙏
On normalized coordinates: that's the cleaner architecture, and honestly tempting. I went with pixel-space + a scale delta on resize because signature_pad's toData()/fromData() hand me points in its own coordinate space — a normalized model would mean keeping a second copy of the strokes, and the whole lesson of this post was "don't keep a second source of truth for canvas state." So for now I'm trading a little rescaling math for not having two things that can disagree. If I ever fork the stroke model, unit floats are first on the list.
On touch-action: good news — it's already covered, but in a sneaky place. I checked globals.css and canvasProps and found nothing, then dug into the library and saw it sets touchAction/msTouchAction = 'none' imperatively on the canvas element in _handleTouchEvents() at mount. So the pan-gesture collision you describe shouldn't bite here — though I agree it's a landmine if you wire up your own canvas and forget it.
On SVG: you're right that _toSVG is sitting there one step away, and I left it unwired on purpose. The thing I want to verify before shipping it is whether _toSVG reconstructs the variable-width contour (the velocity-based ink thickness) or just the Bézier centerline — if it's the centerline, the SVG would lose the exact smoothing the PNG bakes in. Worth a look before I promise crispness.
Appreciate the follow too. Curious how you handle the resize/rotation path on your side — normalized or snapshot-and-rescale?