Signature

A pad to sign on, with ink that thins as you speed up. Pick an eraser and clear it as dust, a cloth wipe, a dither, a melt or a rewind.

MC
Mira ChenSigning for Acme Studio
    Pick an eraser and press Erase, or sign it yourself

    Installation

    Terminal
    npx shadcn@latest add https://fabrickit.org/r/signature.json

    This copies the component into components/fabricui and its engine into lib/fabricui. It's your code from then on. First time? Add the theme once: see Installation.

    Code

    components/fabricui/signature.tsx
    "use client";
    
    import * as React from "react";
    import { mount } from "@/lib/fabricui/mount";
    import { SignatureEngine } from "@/lib/fabricui/signature-engine";
    import { Button } from "@/components/fabricui/button";
    import { Avatar } from "@/components/fabricui/avatar";
    import { Select } from "@/components/fabricui/select";
    import "./signature.css";
    
    const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
    
    export type SignatureEffect = "dust" | "wipe" | "dither" | "melt" | "rewind";
    /** A stroke: the points the pen passed, in px from the pad's top left (w, the line's width there). */
    export type SignatureStroke = { x: number; y: number; w?: number }[];
    
    components/fabricui/signature.css
    /*
     * Signature — a pad to sign on (lib/fabricui/signature-engine.js draws the
     * ink and erases it), and a footer: who's signing, the eraser to use (Select),
     * Erase and the submit button (Button). Colours are the theme's: the ink is
     * the text colour, so it's light on dark and near-black on light.
     */
    .sig:where(.fui-signature, .fui-signature *) {
      position: relative;
      width: 560px;
      max-width: 100%;
      background: var(--fabric-card);
      border-radius: 22px;
      box-shadow: var(--fabric-edge);
      color: var(--fabric-text);
    }
    
    lib/fabricui/signature-engine.js
    /*
     * signature-engine.js — Signature (signature.css). A pad to sign on, and five
     * ways to erase what's on it.
     *
     *   const s = new SignatureEngine(root, { onChange });   // root holds .sig__pad
     *   s.erase("dust");           // a promise, settled once it's gone
     *   s.write(strokes);          // draws strokes in, as if being signed
     *   s.isEmpty; s.strokes; s.toDataURL();
     *
     * The ink: each stroke is the points the pointer passed, drawn as a smooth
     * curve through their midpoints. It thins as the pen speeds up and thickens
     * as it slows (a pen's pressure is used where there is one), like ink from a
     * nib. It's drawn in the pad's text colour, and again when the theme changes.
     *
     * The erasers (all but rewind and wipe take the ink's own pixels and move them):
     *   dust     every pixel of ink lifts off as a mote of dust, a wave crossing

    Usage

    Usage
    "use client";
    
    import * as React from "react";
    import { Signature, type SignatureHandle } from "@/components/fabricui/signature";
    
    export function SignContract({ onSigned }: { onSigned: (png: string) => void }) {
      const sig = React.useRef<SignatureHandle>(null);
      return (
        <Signature
          ref={sig}
          name="Mira Chen"
          caption="Signing for Acme Studio"
          onSubmit={png => onSigned(png)}
        />
      );
    }
    
    // from code: sig.current?.erase("dust"), and "wipe", "dither", "melt" or "rewind"

    Props

    PropTypeDefault
    label

    The pad's label, top left.

    string"Sign here"
    name

    Who's signing: shown in the footer with their avatar (a weave).

    string—
    caption

    A line under the name.

    string—
    submitLabel

    The submit button. It's off until there's a signature.

    string"Continue"
    effects

    The erasers the picker offers, in order; Erase clears it with the one picked. Dust lifts off in fine motes; wipe draws the library's cloth across and gathers it off; dither dissolves it in ordered dots; melt runs it down like goo; rewind unwrites it, last stroke first.

    ("dust" | "wipe" | "dither" | "melt" | "rewind")[]all five
    defaultEffect

    The eraser picked at first.

    "dust" | "wipe" | "dither" | "melt" | "rewind"the first of effects
    defaultStrokes

    Strokes it starts with, drawn in as it appears (points in px from the pad's top left; w is the line's width).

    { x, y, w? }[][]—
    onChange

    Called as it gains a signature (true) or is erased (false).

    (signed: boolean) => void—
    onSubmit

    Called with the signature as a PNG data URL (transparent around the ink) when the submit button is pressed.

    (png: string) => void—
    ref

    Erase it or draw strokes in from code (each returns a promise that settles when it's done), or take the image.

    { erase(effect?), write(strokes), toDataURL(type?), isEmpty }—