StageDraw
stage-drawLightweight transparent overlay for presentation annotations, fading laser pointer, and click-through pass-through.
Usage
Floating Toolbar & Click-Through Verification
Mount StageDraw over a slide: sketch with pen/highlighter, guide focus with laser, erase strokes, and switch back to pass-through to click buttons underneath.
<div className="relative h-[300px]">
<SlideContent />
<StageDraw tool={tool} onToolChange={setTool} toolbar />
</div>External Controls & Laser Pointer Demo
Disable the built-in toolbar and drive StageDraw with controlled tool and apiRef; the laser leaves a glowing trail that fades out over 750ms without persisting strokes.
<StageDraw tool={tool} apiRef={apiRef} laserDecayMs={750} />When to Use
Place StageDraw over slide decks, full-screen presentations, dashboards, or teaching stages so presenters can circle key metrics, highlight text, or guide audience attention with a laser pointer. When tool="none", the overlay applies pointer-events-none so clicks, hovers, and controls underneath remain completely unobstructed.
- For freeform rectangle layout with infinite pan and zoom, use DesignCanvas.
- For proportional big-screen scaling without annotation, use FitScreen.
Import
import {
StageDraw,
normalizePoint,
strokeToSvgPath,
hitTestStroke,
pruneLaserPoints,
} from "@hulianui/ui"Props
| Name | Type | Default | Description |
|---|---|---|---|
| tool | "none" | "pen" | "highlighter" | "laser" | "eraser" | "none" | Active drawing tool; "none" lets all pointer events pass through to underlying content |
| strokes | StageDrawStroke[] | — | Controlled strokes array; updates are reported via onStrokesChange |
| defaultStrokes | StageDrawStroke[] | [] | Initial strokes when used uncontrolled |
| onStrokesChange | (strokes: StageDrawStroke[]) => void | — | Called whenever strokes are drawn, erased, undone, redone, or cleared |
| color | string | "#ef4444" | Stroke color for pen and highlighter (CSS color or variable) |
| strokeWidth | number | 3 | Reference pen stroke width in pixels; highlighter defaults to strokeWidth * 5 |
| highlighterOpacity | number | 0.35 | Opacity applied to highlighter strokes (0 to 1) |
| laserColor | string | "#ef4444" | Glow and trail color for the laser pointer |
| laserDecayMs | number | 750 | Decay duration in milliseconds for laser trail points (recommended 600 to 1000) |
| eraserRadius | number | 0.025 | Normalized hit-test distance threshold used by the eraser tool |
| toolbar | boolean | false | Renders a compact floating toolbar at the bottom; stays pointer-events-auto even when tool="none" |
| onToolChange | (tool: StageDrawTool) => void | — | Called when a tool is selected from the built-in toolbar |
| onColorChange | (color: string) => void | — | Called when a color swatch is selected from the built-in toolbar |
| presetColors | string[] | ["#ef4444", "#f59e0b", "#22c55e", "#3b82f6", "#8b5cf6"] | Preset color swatches shown in the built-in toolbar |
| disabled | boolean | false | Disables drawing and toolbar interactions while allowing pointer pass-through |
| apiRef | MutableRefObject<StageDrawApi | null> | — | Imperative handle exposing undo, redo, clear, canUndo, and canRedo |
| labels | Partial<StageDrawLabels> | Built-in defaults | Overrides accessible labels and toolbar tooltips |
| className | string | — | Outer overlay container class name (defaults to absolute inset-0; pass fixed inset-0 for full-viewport overlays) |
StageDrawStroke
| Name | Type | Default | Description |
|---|---|---|---|
| id* | string | — | Unique stroke identifier |
| tool* | "pen" | "highlighter" | — | Stroke tool type |
| color* | string | — | Stroke color |
| width* | number | — | Reference stroke width in pixels |
| opacity | number | 1 for pen, 0.35 for highlighter | Stroke opacity |
| points* | StageDrawPoint[] | — | Sequence of normalized points |
StageDrawPoint
| Name | Type | Default | Description |
|---|---|---|---|
| x* | number | — | Normalized X coordinate in [0, 1] relative to overlay width |
| y* | number | — | Normalized Y coordinate in [0, 1] relative to overlay height |
| pressure | number | — | Optional normalized pointer pressure in [0, 1] |
StageDrawLaserPoint
| Name | Type | Default | Description |
|---|---|---|---|
| x* | number | — | Normalized X coordinate in [0, 1] |
| y* | number | — | Normalized Y coordinate in [0, 1] |
| t* | number | — | Sample timestamp in milliseconds |
StageDrawRect
| Name | Type | Default | Description |
|---|---|---|---|
| left* | number | — | Container left offset in pixels |
| top* | number | — | Container top offset in pixels |
| width* | number | — | Container width in pixels |
| height* | number | — | Container height in pixels |
StageDrawApi
| Name | Type | Default | Description |
|---|---|---|---|
| undo* | () => void | — | Reverts the last stroke mutation |
| redo* | () => void | — | Reapplies the last undone mutation |
| clear* | () => void | — | Clears all strokes (undoable) |
| canUndo* | () => boolean | — | Returns whether undo history is available |
| canRedo* | () => boolean | — | Returns whether redo history is available |
StageDrawLabels
| Name | Type | Default | Description |
|---|---|---|---|
| stage* | string | Built-in default | Accessible name for the overlay and toolbar |
| none* | string | Built-in default | Label for pass-through cursor mode |
| pen* | string | Built-in default | Label for pen tool |
| highlighter* | string | Built-in default | Label for highlighter tool |
| laser* | string | Built-in default | Label for laser pointer tool |
| eraser* | string | Built-in default | Label for eraser tool |
| undo* | string | Built-in default | Label for undo action |
| redo* | string | Built-in default | Label for redo action |
| clear* | string | Built-in default | Label for clear action |
Pure Utilities
normalizePoint(clientX, clientY, rect, pressure?): converts viewport coordinates into clamped[0, 1]normalized coordinates.strokeToSvgPath(points, width, height): converts normalized points into a smooth quadratic Bezier SVG path string.hitTestStroke(stroke, point, threshold): tests whether a normalized point lies withinthresholddistance of any stroke segment.pruneLaserPoints(points, now, decayMs): drops laser trail points older thandecayMs.
Pitfalls
StageDrawusesabsolute inset-0by default, so wrap your presentation container inrelative(or passclassName="fixed inset-0 z-50"for viewport-wide annotation).- Laser pointer trails (
tool="laser") are strictly ephemeral: they never mutatestrokesor fireonStrokesChange, and the internalrequestAnimationFrameloop automatically stops once the trail finishes decaying so idle CPU usage stays at zero.
Related
Playground
<StageDraw tool="pen" strokeWidth={3} toolbar />