Scrawl
The scratches a ballpoint leaves in a margin while it gets going (a zigzag, a worked-over corner, a star, slashes, a spiral, loops, a wave) and a proofreader's marks (a cross, an asterisk, a ring, a tick), in ink or red pen.
- zigzag
- corner
- star
- slash
import { Scrawl, type ScrawlKind } from "@/components/ui/scrawl";
const kinds: ScrawlKind[] = ["zigzag", "corner", "star", "slash"];
export default function ScrawlDemo() {
return (
<ul className="flex flex-wrap items-end justify-center gap-x-12 gap-y-8">
{kinds.map((kind, i) => (
<li key={kind} className="flex flex-col items-center gap-3">
<Scrawl kind={kind} seed={`scrawl-${kind}`} delay={i * 250} scale={kind === "corner" ? 0.8 : 1} />
<span className="text-sm text-ink-3">{kind}</span>
</li>
))}
</ul>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ scrawl"use client";
import { useId, useMemo, type CSSProperties } from "react";
import { cn } from "@/lib/utils";
import { penStyle, useInkBox, useInkSeed, usePen, type Pen } from "@/hooks/use-ink-box";
import { InkMarks, InkSvg } from "@/lib/ink";
import { asteriskPulls, cornerPulls, crossPulls, inkPulls, loopPulls, ringPulls, slashPulls, spiralPulls, starPulls, tickPulls, wavePulls, zigzagPulls } from "@/lib/ink-sketch";
export type ScrawlKind = "zigzag" | "corner" | "star" | "slash" | "spiral" | "loops" | "waves" | "cross" | "asterisk" | "ring" | "tick";
// Each kind's size, and how fast it goes on: ms per pull and between pulls.
const KINDS: Record<ScrawlKind, { size: [number, number]; dur: number; gap: number; bow?: number }> = {
zigzag: { size: [40, 92], dur: 200, gap: 0 },
corner: { size: [80, 150], dur: 130, gap: 50 },
star: { size: [46, 50], dur: 110, gap: 50 },
slash: { size: [48, 22], dur: 70, gap: 45 },
spiral: { size: [56, 56], dur: 40, gap: 0, bow: 0.12 },
loops: { size: [120, 38], dur: 40, gap: 0, bow: 0.12 },
waves: { size: [112, 28], dur: 90, gap: 60, bow: 0.12 },
cross: { size: [44, 44], dur: 130, gap: 50 },
asterisk: { size: [42, 42], dur: 110, gap: 40 },
ring: { size: [64, 46], dur: 60, gap: 60, bow: 0.15 },
tick: { size: [40, 34], dur: 110, gap: 0 },
};
const PULLS: Record<ScrawlKind, (seed: number, w: number, h: number) => ReturnType<typeof zigzagPulls>> = {
zigzag: (seed, w, h) => zigzagPulls(seed, w, h, { passes: 6 + (seed % 3) }),
corner: cornerPulls,
star: starPulls,
slash: slashPulls,
spiral: spiralPulls,
loops: loopPulls,
waves: wavePulls,
cross: crossPulls,
asterisk: asteriskPulls,
ring: ringPulls,
tick: tickPulls,
};
// How much each kind is gone over by default: slashes least, tidy shapes not at all.
const RETRACE: Record<ScrawlKind, number> = { zigzag: 0.75, corner: 0.75, star: 0.75, slash: 0.3, spiral: 0, loops: 0, waves: 0.2, cross: 0.5, asterisk: 0.5, ring: 0, tick: 0.3 };
/**
* The scratches a ballpoint collects in a margin while someone gets it
* going: a zigzag, a worked-over corner, a hurried star, a patch of slashes,
* a spiral, a row of loops, a wave; and the marks of a proofreader: a cross,
* an asterisk, a ring, a tick. In the ink or the red pen. Purely decorative
* (hidden from screen readers); place it with a class (absolute, in the
* page margin). Drawn as it scrolls into view.
*/
function Scrawl({
kind = "zigzag",
scale = 1,
rotate = 0,
flip = false,
color = "ink",
retrace,
delay = 0,
className,
style,
seed,
draw,
weight,
speed,
}: Pick<Pen, "draw" | "weight" | "speed"> & {
kind?: ScrawlKind;
scale?: number;
/** Degrees. */
rotate?: number;
/** Mirror it, so the same scribble can lean the other way. */
flip?: boolean;
/** The ink, or the red pen. */
color?: "ink" | "red";
/** 0 to 1: how much of it the pen goes back over. Each kind has its own habit. */
retrace?: number;
/** ms after it comes into view before the pen starts. */
delay?: number;
className?: string;
style?: CSSProperties;
seed?: string | number;
}) {
const pen = usePen({ draw, weight, speed });
const s = useInkSeed(seed);
const id = useId().replace(/[^\w-]/g, "");
const { size, dur, gap, bow } = KINDS[kind];
const [ref] = useInkBox(size);
const mode = pen.draw === "none" ? "none" : pen.draw === "mount" ? "mount" : "auto";
const strokes = useMemo(() => {
const [w, h] = size;
// Scrawls get gone over, each kind by its own habit.
return inkPulls(s, PULLS[kind](s, w, h), { weight: (1.3 * (pen.weight ?? 1)) / scale, opacity: 0.9, dur, gap, bow, retrace: retrace ?? RETRACE[kind] });
}, [kind, s, size, dur, gap, bow, scale, retrace, pen.weight]);
return (
<span
aria-hidden="true"
data-slot="scrawl"
data-kind={kind}
className={cn("pointer-events-none relative inline-block", color === "red" ? "text-pen-red" : "text-ink-2", className)}
style={{ width: size[0] * scale, height: size[1] * scale, rotate: `${rotate}deg`, scale: flip ? "-1 1" : undefined, ...penStyle(pen), ...style }}
>
<InkSvg ref={ref} pending={mode === "auto"} box={[0, 0, size[0], size[1]]} className="inset-0 size-full">
<InkMarks id={`sc${id}`} strokes={strokes} draw={mode} delay={delay} guide={6 / scale} />
</InkSvg>
</span>
);
}
export { Scrawl };Usage
import { Scrawl } from "@/components/ui/scrawl"
<section className="relative">
<Scrawl kind="star" rotate={-8} className="absolute -right-16 top-4" />
…
</section>Examples
As a stamp
Position a scrawl on a corner of a card, staggering the delay so each is drawn in turn.
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Scrawl } from "@/components/ui/scrawl";
export default function ScrawlStamp() {
return (
<div className="relative w-full max-w-sm">
<Card seed="ss-card">
<CardHeader>
<CardTitle>Draft: launch post</CardTitle>
<CardDescription>Reviewed by two people.</CardDescription>
</CardHeader>
<CardContent className="text-ink-2">Looks good. Ship it on Tuesday.</CardContent>
</Card>
<Scrawl kind="star" rotate={-10} delay={400} seed="ss-star" className="absolute -top-5 -right-4" />
<Scrawl kind="slash" scale={1.4} rotate={-4} delay={900} seed="ss-slash" className="absolute -bottom-3 left-8" />
</div>
);
}As dividers
A scrawl between sections, a different one each time.
Mornings
Coffee, then the first page.
Afternoons
Letters, lists and errands.
Evenings
Sketching until the lamp goes on.
import { Scrawl } from "@/components/ui/scrawl";
const sections = [
["Mornings", "Coffee, then the first page.", "zigzag"],
["Afternoons", "Letters, lists and errands.", "star"],
["Evenings", "Sketching until the lamp goes on.", "slash"],
] as const;
export default function ScrawlDividers() {
return (
<div className="flex w-full max-w-md flex-col gap-4">
{sections.map(([title, note, kind], i) => (
<div key={title} className="flex flex-col gap-4">
{i > 0 && <Scrawl kind={kind} scale={0.7} seed={`sd-${kind}`} delay={i * 200} className="self-center text-ink-3" />}
<section>
<h4 className="text-xl font-bold">{title}</h4>
<p className="text-ink-2">{note}</p>
</section>
</div>
))}
</div>
);
}Seven more
A spiral, a row of loops, a wave, and the marks of a proofreader: cross, asterisk, ring and tick.
- spiral
- loops
- waves
- cross
- asterisk
- ring
- tick
import { Scrawl, type ScrawlKind } from "@/components/ui/scrawl";
const kinds: ScrawlKind[] = ["spiral", "loops", "waves", "cross", "asterisk", "ring", "tick"];
export default function ScrawlKinds() {
return (
<ul className="grid w-full max-w-xl grid-cols-2 items-end gap-x-10 gap-y-9 sm:grid-cols-4">
{kinds.map((kind, i) => (
<li key={kind} className="flex flex-col items-center gap-3">
<Scrawl kind={kind} seed={`sk-${kind}`} delay={i * 200} />
<span className="font-mono text-sm text-ink-3">{kind}</span>
</li>
))}
</ul>
);
}Red pen
A tick, a ring, an asterisk and a cross, scaled down and set beside a to-do list.
- Buy more blue pens
- Finish the draft by Friday
- Ask about the old notebook
- Paint the shed
import { Scrawl } from "@/components/ui/scrawl";
const lines = [
["Buy more blue pens", "tick"],
["Finish the draft by Friday", "ring"],
["Ask about the old notebook", "asterisk"],
["Paint the shed", "cross"],
] as const;
export default function ScrawlRed() {
return (
<ul className="flex w-full max-w-sm flex-col gap-5 text-lg">
{lines.map(([text, kind], i) => (
<li key={text} className="relative flex items-center gap-4">
<span className="relative w-12 shrink-0">
<Scrawl kind={kind} color="red" scale={0.7} seed={`sr-${kind}`} delay={i * 300} className="absolute -top-5 left-0" />
</span>
<span className="text-ink-2">{text}</span>
</li>
))}
</ul>
);
}Testing the pen
The first page of a new notebook: a spiral, loops, a wave and a zigzag, on ruled paper.
Testing the new pen
Works. Smooth, too.
import { Paper } from "@/components/ui/paper";
import { Scrawl } from "@/components/ui/scrawl";
export default function ScrawlPenTest() {
return (
<Paper variant="ruled" margin edge="torn" lifted seed="spt" tilt={-0.8} className="w-full max-w-md pb-5">
<p className="text-ink-3">Testing the new pen</p>
<div className="relative h-[6rem]">
<Scrawl kind="spiral" scale={1.1} seed="spt-a" delay={100} className="absolute top-1 left-0" />
<Scrawl kind="loops" scale={1.3} seed="spt-b" delay={500} className="absolute top-0 left-20" />
<Scrawl kind="waves" seed="spt-c" delay={900} className="absolute top-14 left-6" />
<Scrawl kind="zigzag" scale={0.7} rotate={90} seed="spt-d" delay={1200} className="absolute top-2 right-4" />
</div>
<p>Works. Smooth, too.</p>
</Paper>
);
}In the margins
Marks around a paragraph, as if someone had read it with a pen in hand.
Read slowly, the whole page is drawn in the margins as well: a star where something was important, a tick where it checked out, and a ring round the one line worth coming back to. Nobody drew them to be looked at. They are the sound of someone reading with a pen in hand.
import { Scrawl } from "@/components/ui/scrawl";
export default function ScrawlMargins() {
return (
<div className="relative w-full max-w-md px-10 py-6">
<Scrawl kind="asterisk" color="red" scale={0.6} seed="sm-a" delay={300} className="absolute top-5 left-1" />
<Scrawl kind="tick" color="red" scale={0.7} seed="sm-b" delay={700} className="absolute top-28 left-0" />
<Scrawl kind="ring" scale={0.7} seed="sm-c" delay={1000} className="absolute right-0 bottom-8" flip />
<p className="text-lg leading-relaxed text-ink-2">
Read slowly, the whole page is drawn in the margins as well: a star where something was important, a tick where it checked out, and a
ring round the one line worth coming back to. Nobody drew them to be looked at. They are the sound of someone reading with a pen in
hand.
</p>
</div>
);
}API reference
- kinddefault
"zigzag" "zigzag" | "corner" | "star" | "slash" | "spiral" | "loops" | "waves" | "cross" | "asterisk" | "ring" | "tick"Which scribble: the ones a pen makes while it gets going (zigzag, corner, star, slash, spiral, loops, waves), or a proofreader's marks (cross, asterisk, ring, tick).
- scaledefault
1 numberSize; the line stays the same weight.
- rotatedefault
0 numberDegrees.
- delaydefault
0 numberms after it comes into view before the pen starts.
- colordefault
"ink" "ink" | "red"The ink, or the red pen.
- flipdefault
false booleanMirror it, so the same scribble can lean the other way.
- retrace
number0 to 1: how much of it the pen goes back over. Each kind has its own habit (a spiral none, a zigzag most of it).
- weightdefault
1 numberLine weight multiplier. Also settable in CSS as --ink-weight.
- seed
string | numberPins the drawing.
Pen settings
The drawing also takes draw, weight and speed, as props or from the nearest InkProvider. What each one does.