组件

Sketchpad

A real drawing surface. Pointer, touch, pen pressure or the keyboard put strokes on paper, and every stroke is rendered by the interface's own pen (variable-width ribbons, pooled ends, felt / graphite / marker grain), so what people draw looks like the rest of the page. Undo, redo, export as svg, replay.

安装

terminal
npx shadcn@latest add @wobko/sketchpad

第一次使用?先把 registry 加进 components.json。 如何安装

示例

Default

Draw with the mouse, a finger or a pen. Every line is written by the same pen as the buttons: pressure swells it, felt pools where you stop, pencil has tooth.

Every pen

Felt, brush, pencil, marker and highlighter on one page. Same points, five materials: the grain and the blend come from the pen, not from the data.

Replay

A read-only sketchpad that draws itself on again, stroke after stroke, along each center line. Reduced motion shows the finished picture.

Your own toolbar

The parts compose. A pencil-and-eraser sketchbook on graph paper, four colors, taped to the desk, with a plot already begun.

Postcard

A real use: draw a card, watch a live thumbnail (the same strokes, read-only), save the picture as svg or copy the JSON to keep it in your database.

API 参考

Sketchpad

The root: it owns the strokes, the pen in hand, the history and the export handle, and lays out a toolbar and a canvas when you give it no children. Strokes are plain JSON, stored in canvas units, so a saved drawing looks identical everywhere.

属性类型默认值说明
value / defaultValueSketchStroke[]-The drawing: { id, tool, color, size, pts: [x, y, pressure?][] }. Controlled or uncontrolled. Strokes without an id are given one.
onValueChange(strokes: SketchStroke[]) => void-Called after every stroke, erase, undo, redo and clear.
tool / defaultTool / onToolChange"felt" | "brush" | "pencil" | "marker" | "highlighter" | "eraser""felt"The pen in hand. The eraser removes whole strokes it touches.
toolsSketchTool[]["felt","pencil","marker","highlighter","eraser"]Which tools the toolbar shows and the keyboard offers.
color / defaultColor / onColorChangeSketchColor"ink"One of the theme's pens and accents: ink, ink-you, red, green, marker, tangerine, hi, teal, grape, bubblegum. Choosing the highlighter over a dark ink switches to yellow.
size / defaultSize / onSizeChangenumber1Width multiplier of the tool's own nominal line. Presets: 0.6 fine, 1 medium, 1.9 bold.
width / heightnumber640 / 400The canvas's logical size. Strokes live in these units and the canvas scales to its container.
readOnlybooleanfalseLook, do not draw. The default toolbar is left out and the canvas becomes a labeled image.
disabledbooleanfalseDraws nothing, dims the toolbar, leaves the tab order.
replay / replaySpeedboolean / numberfalse / 1Draw the strokes on again, in order, each one uncovered along its own center line. Reduced motion shows the finished picture.
apiRefRef<SketchpadHandle>-undo(), redo(), clear(), replay(), strokes(), toSVG(), toDataURL() (svg, no canvas needed), toPNG() (async, null where there is no canvas).
seedstring | number-Salts every stroke's hand. Leave it out and the same JSON draws the same picture in every browser.

SketchpadCanvas

The sheet the strokes land on. Pointer events with coalesced samples and pen pressure (palm rejection while a pen is in range), touch-action none, and keyboard drawing: arrows move a cursor, Space puts the pen down and lifts it. Children are guides printed under the strokes.

属性类型默认值说明
paper"plain" | "ruled" | "graph" | "dots" | "kraft""plain"What it is drawn on.
paperPropsPaperProps-Anything the sheet takes: tape, fastener, edge, stock.
labelstring"Drawing"Accessible name of the canvas.
seedstring | number-Pins the frame's hand.

SketchpadTools

A radio group of tool buttons. Each shows a sample of what its pen writes, in the current color; the selected one is boxed and highlighted. Roving focus: arrow keys move and select.

属性类型默认值说明
toolsSketchTool[]-Override the root's tool list for this group.

SketchpadTool

One tool button (Radix radio item and ink host).

属性类型默认值说明
valueSketchTool-Which tool.
seedstring | number-Pins the hand.

SketchpadColors

A radio group of scribbled color swatches.

属性类型默认值说明
colorsSketchColor[]-Which swatches to show.

SketchpadColor

One swatch, named for screen readers (Ink, Blue, Red…).

属性类型默认值说明
valueSketchColor-Which color.

SketchpadSize

One width preset.

属性类型默认值说明
value"fine" | "medium" | "bold"-Which preset.

无障碍

  • The canvas is role="application" (aria-roledescription "drawing canvas") with a name and a description of the keys; the drawn strokes are aria-hidden. Read-only, it is an image named "Drawing, N strokes".
  • Keyboard drawing: arrow keys move a cursor (Shift = 32 units, Alt = 2), Space or Enter puts the pen down ("Pen down.") and lifts it ("Stroke added."), Escape cancels the stroke or hides the cursor. Leaving the canvas lifts the pen.
  • Focus: Undo, Redo and Clear disable themselves when they have nothing left to do; if one of them had focus, focus moves to the canvas instead of falling to the top of the page. Every part composes its own `onClick` with the built-in action (call `event.preventDefault()` to cancel it).
  • Shortcuts on the focused canvas: F felt, B brush, P pencil, M marker, H highlighter, E eraser (only tools that are offered), [ and ] change the width, Ctrl/Cmd+Z undo, Ctrl/Cmd+Shift+Z or Ctrl+Y redo.
  • Tools, colors and widths are radio groups (Radix): Tab enters a group, arrow keys move and select. Every swatch has a color name; toolbar buttons are at least 28 px with a bigger hit area.
  • Adding, erasing, undoing and clearing are announced politely ("Stroke added. 3 strokes.").
  • Windows high contrast: the strokes keep their own colors (forced-color-adjust: none), the toolbar gets real outlines. Reduced motion: replay shows the finished drawing at once.
  • Touch: the canvas sets touch-action: none so drawing never scrolls the page. That also means a canvas that fills the screen on a phone traps the scroll: leave a margin around it, or put it in a tab or dialog. Palm rejection: a finger is ignored for 1.5 s after the pen was last seen near the screen (and a palm that got there first is dropped when the pen lands), so a person who switches between pencil and finger is never locked out.
  • Scale: a stroke is stored as thinned points (samples closer than 0.8 units are held back) and drawn by the pen as ribbons, which is about 15 to 30 KB of svg for a hundred points and up to 1 MB for a very long scribble; the finished markup is cached (bounded by size, 24 MB) and a live stroke is previewed with at most 480 points. A page with many hundreds of long strokes belongs in the replay/export flow (toSVG), not in a live canvas.

源码

CLI 会复制进你项目的文件。

正在加载源码