Website review
This review may include AI-generated content and is not individually verified. Domain, certificate and other facts come from the observed data on this page.
What is perfect-freehand?
perfect-freehand is a small JavaScript library for turning freehand pointer or stylus input into smooth, tapered, pressure-sensitive outlines. The page at perfect-freehand is a JavaScript demo app; its visible content only shows the "You need to enable JavaScript to run this app." notice, so the description below reflects the library's general purpose rather than details confirmed from that page.
It is not a full drawing application. It is the stroke-generation step: you feed it input points (position, and optionally pressure, tilt, or speed) and it returns a polygon outline you can render on a canvas, SVG, or WebGL surface. That makes it useful when you want pen-like lines without building your own smoothing and taper math.
Where it fits
- Sketch and whiteboard tools: natural-looking ink strokes that thin at the ends.
- Annotation layers: quick markup over documents or images.
- Signature or handwriting capture: smoother curves than raw point-to-point lines.
- Generative or creative coding: a lightweight stroke primitive.
Trade-offs to weigh
- It handles stroke shape, not undo history, layers, hit-testing, or persistence; you supply those.
- Output is an outline polygon, so you choose the renderer and manage performance yourself.
- Very high input rates or large canvases need throttling or point simplification.
- If you need full vector editing or object manipulation, a broader canvas library may suit you better.
Next step: open the demo with JavaScript enabled and draw a few strokes to see the taper and pressure response, then check the library's documentation for the input options that control thinning and smoothing.
How does perfect-freehand differ from the default HTML5 canvas line drawing?
The perfect-freehand demo page is a JavaScript-drawn canvas app, so its exact behavior is best judged by running it in a browser. Conceptually, the difference is that HTML5 canvas line drawing (moveTo/lineTo/stroke) paints a fixed-width line along the path you specify, while a freehand stroke model treats your input points as a centerline and builds an outline around them.
What changes in practice
- Width and pressure: Canvas
lineWidthis constant unless you redraw segments with varying widths yourself. A freehand approach typically computes variable thickness from speed, pressure, or simulated pressure, so strokes taper and swell like ink. - Smoothing: Raw pointer coordinates are jittery. Canvas draws exactly the points you give it; freehand libraries smooth or interpolate between them, which matters most on touchscreens and high-DPI displays.
- Outline vs. line: Canvas strokes a path. Freehand output is usually a filled polygon outline, which you can store, hit-test, or export as SVG paths instead of pixels.
- Ends and joins: Canvas
lineCapandlineJoinare limited. Freehand models give rounded, tapered ends and controllable corners more naturally.
Choosing between them
Use plain canvas lines for charts, grids, annotations, and anything where exact geometry and cheap redraws matter. Use a freehand model for signatures, sketches, whiteboard tools, and note-taking, where the feel of the stroke is the product.
A practical next step: draw the same squiggle with ctx.lineTo and with a freehand function at the same pointer speed, then compare how each looks at 2x device pixel ratio. If the canvas version looks mechanical and thin on fast strokes, the freehand model is worth the extra complexity.
How do I integrate perfect-freehand into a React drawing app?
The short answer: install the perfect-freehand package, keep a list of raw input points in state, and run those points through getStroke() at render time to produce a filled outline you draw on a canvas or as an SVG path. The package itself is framework-agnostic; React only manages the points and re-renders.
Core integration pattern
- Capture points on pointer events. On
pointerdown, start a new stroke and store the first point as[x, y, pressure]. Onpointermove, append points. Onpointerup, close the stroke. Useevent.pressurewhen available (pen/stylus) and fall back to a constant. - Convert points to an outline. Call
getStroke(points, options)to get a polygon of outline points. - Render the outline. Either draw it on a
<canvas>withPath2D/lineTo, or build an SVG path string from the outline (a common helper converts outline points to a smooth closed path).
import { useState } from "react";
import { getStroke } from "perfect-freehand";
function toPath(points) {
if (!points.length) return "";
const d = points.reduce(
(acc, [x0, y0], i, arr) => {
const [x1, y1] = arr[(i + 1) % arr.length];
acc.push(x0, y0, (x0 + x1) / 2, (y0 + y1) / 2);
return acc;
},
["M", ...points[0], "Q"]
);
d.push("Z");
return d.join(" ");
}
export default function Draw() {
const [strokes, setStrokes] = useState([]);
const [current, setCurrent] = useState(null);
const handleDown = (e) => setCurrent([[e.nativeEvent.offsetX, e.nativeEvent.offsetY, e.pressure]]);
const handleMove = (e) => {
if (!current) return;
setCurrent([...current, [e.nativeEvent.offsetX, e.nativeEvent.offsetY, e.pressure]]);
};
const handleUp = () => {
if (current) setStrokes([...strokes, current]);
setCurrent(null);
};
const render = (pts) =>
toPath(getStroke(pts, { size: 8, thinning: 0.5, smoothing: 0.5, streamline: 0.5 }));
return (
<svg
onPointerDown={handleDown}
onPointerMove={handleMove}
onPointerUp={handleUp}
style={{ touchAction: "none", width: "100%", height: "100vh" }}
>
{[...strokes, current].filter(Boolean).map((s, i) => (
<path key={i} d={render(s)} fill="black" />
))}
</svg>
);
}
Decisions that matter in practice
- SVG vs canvas. SVG paths are simpler to hit-test, select, and style per stroke, and fine for hundreds of strokes. Canvas scales better for very long sessions or thousands of strokes; you redraw the visible strokes each frame.
- When to compute the outline. For live drawing, recomputing
getStroke()on every move is usually cheap for a single active stroke. For finished strokes, cache the generated path so you don't recompute on unrelated re-renders. - Pressure and options.
size,thinning,smoothing, andstreamlinecontrol how much the line tapers and how smoothed the input feels.streamlinetrades responsiveness for steadier lines; tune it against your target device (mouse vs stylus). - State shape. Keep raw points, not the outline, in state. The outline is derived data; storing it makes undo/redo and re-rendering harder.
Next step
Build the smallest version first: one stroke, no undo, rendered as an SVG path. Once that draws correctly, add stroke history for undo, then decide whether canvas is needed based on how many strokes your users accumulate. The official demo at perfect-freehand is a useful reference for how the options change the line's feel.
What input options control stroke thickness and taper in perfect-freehand?
The available page information for perfect-freehand is limited to a notice that JavaScript must be enabled to run the app, so it does not list the specific input options that control stroke thickness and taper. Rather than invent parameter names, here is how to find and use them reliably.
How to identify the real options
Open the running demo in a browser with JavaScript enabled, then inspect the options object passed to the stroke function. In perfect-freehand this is typically a single settings object, and the relevant controls fall into a few groups:
- Size: sets the overall stroke width, usually the first thing to adjust.
- Thinning: controls how much the stroke narrows as drawing speed increases; higher values give more variation between fast and slow segments.
- Taper start and taper end: define how far the stroke narrows toward each end, which is what produces pointed or blunt tips.
- Smoothing and streamline: affect how input points are averaged, which indirectly changes how taper and thinning appear.
A practical check
Draw the same short, quick stroke and the same slow stroke with only one setting changed at a time. If thickness changes along the stroke, thinning is active; if only the ends narrow, taper is responsible. For a pen-like look, combine moderate thinning with a short taper at both ends; for a marker look, set thinning near zero and disable taper so width stays constant.
Can perfect-freehand be used for handwriting or signature capture?
Yes. perfect-freehand is a small JavaScript library that turns pointer input into smooth, pressure-sensitive stroke outlines, which is exactly the kind of data handwriting and signature capture need. The catch is that it draws strokes; it does not store or verify signatures for you.
What it handles well
- Smooth ink from raw pointer or stylus data. It takes the points your canvas or SVG already collects and produces a clean outline, so quick handwriting and signatures look natural rather than jagged.
- Pressure and speed variation. Pen tablets and styluses report pressure; the library uses it to taper strokes, which makes signatures feel closer to pen on paper.
- Framework-agnostic output. It returns outline points you can render on a canvas or as an SVG path, so it fits into most web stacks without locking you into a UI library.
What it does not do
- Signature validity or biometrics. It is a rendering aid, not a signature-verification or forensic tool. If you need legal-grade e-signature workflows, pair it with a dedicated e-signature service.
- Persistence and playback. You still need to capture and store the raw point data (with timestamps and pressure) yourself if you want to replay or audit a signature later.
- Input normalization. Touch, mouse and stylus events differ across devices; smoothing the input before it reaches the library is your job.
Practical scenario
A clinic wants patients to sign a consent form on a tablet. The team collects pointer events on a canvas, feeds the points to perfect-freehand for a natural-looking stroke, and saves the point array alongside the form record. The drawing looks right, and the stored points let staff re-render the signature later. Verification and legal binding come from the e-signature provider, not the drawing library.
Decision criterion
Use perfect-freehand when the goal is good-looking, responsive ink. Add a separate e-signature or audit service when the goal is legal validity, tamper evidence or identity checks. For related drawing and canvas tooling, see MDN Web Docs for pointer events and canvas APIs.
How do I export perfect-freehand strokes as SVG paths?
The page for perfect-freehand is a JavaScript demo app, so it renders nothing useful without JavaScript enabled and provides no written export instructions. The export method therefore comes from how the library works rather than from this page.
perfect-freehand returns stroke outline points — an array of [x, y] (optionally pressure) coordinates tracing the outer edge of the stroke. To get an SVG path, convert those outline points into a closed polygon path string.
Practical approach
import { getStroke } from 'perfect-freehand'
const stroke = getStroke(points, { size: 12, thinning: 0.5 })
const d = stroke.length
? 'M' + stroke.map(p => p.join(',')).join('L') + 'Z'
: ''
const svg = `<path d="${d}" fill="#000" />`
Key points:
- Use
fill, notstroke. The outline is already the shape of the stroke, so fill it. Adding an SVGstrokeon top would fatten the edges. - Close the path with
Zso the outline fills cleanly. - Handle empty input. If the user hasn't drawn yet,
strokeis empty; guard against producingMZ. - Smoothing is baked in. Options like
smoothingandstreamlinechange the outline itself, so set them before exporting — you can't adjust them later in the SVG.
Choosing an export format
| Goal | Better option |
|---|---|
| Vector editing in Illustrator/Figma | Filled <path> with the outline points |
| Small file, simple shape | Single <path> per stroke, grouped in one <g> |
| Animated drawing effect | A centerline path with stroke-dasharray (requires separate centerline data) |
| Handoff to non-technical users | Wrap the paths in a full <svg> document and offer a download |
Next step
If you need editable, stroke-based SVG (so a designer can change line width later), you'll need the original centerline points rather than the outline — keep both, and export the centerline as a stroked path while using the outline only for rendering. Test the output by opening the generated SVG in a browser and a vector editor to confirm the fill renders as expected.
User reviews (0)