SVG drawing canvas web component for StarHTML/Datastar
Project description
star-drawing
SVG drawing canvas web component for StarHTML and Datastar.
One app.register() call gives you a <drawing-canvas> custom element with drawing, shape, text, and annotation tools — plus a reactive toolbar wired to the canvas state. Built on starelements, so each canvas instance gets its own scoped signal namespace.
Why?
Adding a drawing surface to a web app typically means choosing a JS canvas library, wiring up toolbars, managing undo state, and handling pointer events yourself. star-drawing wraps all of that into a single starelements component — import it, register it, and you have a working canvas with 10 tools, undo/redo, keyboard shortcuts, and collaboration hooks.
Features
- Full drawing toolkit — pen, highlighter, line, arrow, rectangle, ellipse, diamond, text, and eraser tools out of the box.
- WYSIWYG text editing — click to place text, edit inline with font family, size, and alignment options.
- Toolbar presets —
drawing_toolbar()for the full suite,annotation_toolbar()for markup,diagram_toolbar()for shapes and connectors. - Undo/redo and keyboard shortcuts — Ctrl+Z, Ctrl+Y, Delete, Ctrl+A, Ctrl+D, and arrow-key nudging all wired up.
- SVG export/import —
export_svg()andimport_svg()for serialization and persistence. - Scoped signals — built on starelements, so each canvas instance has its own signal namespace. No cross-instance interference.
- Collaboration-ready —
onElementChangecallback andapplyRemoteChangesmethod for syncing state across clients. - Configurable palettes — stroke colors, fill colors, highlighter colors, width presets, font options, and arrowhead styles are all customizable.
- Readonly mode — set the
readonlyattribute for view-only canvases. - Skeleton loading — shimmer placeholder shown until the component initializes, preventing layout shift.
Installation
Requires Python 3.12+ and StarHTML.
pip install star-drawing
Quick Start
from starhtml import *
from star_drawing import DrawingCanvas, drawing_toolbar
app, rt = star_app()
app.register(DrawingCanvas)
@rt("/")
def index():
canvas = DrawingCanvas(cls="drawing-container")
return Div(
drawing_toolbar(canvas),
canvas,
)
serve()
This gives you a full-featured drawing canvas with all tools, color palettes, style options, and undo/redo.
Toolbar Presets
For focused use cases, use a preset instead of the full toolbar:
# Annotation — pen, highlighter, eraser only
annotation_toolbar(canvas)
# Diagramming — select, shapes, lines, arrows, text
diagram_toolbar(canvas)
Both presets accept the same keyword arguments as drawing_toolbar() for further customization.
Configuration
Attributes on <drawing-canvas> control defaults:
| Attribute | Default | Description |
|---|---|---|
default-tool |
"pen" |
Initial active tool |
default-stroke-color |
"#1a1a2e" |
Initial stroke color |
default-fill-color |
"#ffffff" |
Initial fill color |
default-stroke-width |
2 |
Initial stroke width in px |
default-opacity |
1 |
Initial element opacity |
default-layer |
"default" |
Initial active layer |
throttle-ms |
16 |
Input throttle interval |
readonly |
— | Disables all drawing interaction |
Set these as element attributes in Python:
DrawingCanvas(
default_tool="select",
default_stroke_color="#3568d4",
default_stroke_width=4,
readonly=True,
)
Toolbar Customization
drawing_toolbar() accepts keyword arguments to override defaults:
drawing_toolbar(
canvas,
tools=("pen", "line", "rect", "text"), # subset of tools to show
show_colors=True, # color palette panel
show_styles=True, # style options panel
show_undo=True, # undo/redo/clear buttons
color_palette=[("#000", "Black"), ("#fff", "White")],
width_presets=(1, 2, 4, 8),
)
Development
TypeScript sources live in typescript/ and are bundled with bun:
bun run build # build drawing-canvas.js
bun run dev # watch mode
uv run ruff check src/ tests/ # lint Python
uv run ruff format --check src/ # check formatting
npx tsc --noEmit # type-check TypeScript
The hatch build hook runs bun run build automatically during pip install / uv build, so the generated JS is never checked into git.
License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file star_drawing-0.1.2.tar.gz.
File metadata
- Download URL: star_drawing-0.1.2.tar.gz
- Upload date:
- Size: 78.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0cf98dccf1bc37e5ed4202c74ce5d64b5736c6267e1c020d7b9f9e45e24177fa
|
|
| MD5 |
6bed0ba04f28aa888358bba226009247
|
|
| BLAKE2b-256 |
1a38baf03b61062325dd647d6d1eae758039fcc248687112ce1b45e4492e0a0a
|
Provenance
The following attestation bundles were made for star_drawing-0.1.2.tar.gz:
Publisher:
release.yml on banditburai/star-drawing
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
star_drawing-0.1.2.tar.gz -
Subject digest:
0cf98dccf1bc37e5ed4202c74ce5d64b5736c6267e1c020d7b9f9e45e24177fa - Sigstore transparency entry: 974821110
- Sigstore integration time:
-
Permalink:
banditburai/star-drawing@ba211bc702fc9b636db7c766068a866cdc99d8cb -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/banditburai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@ba211bc702fc9b636db7c766068a866cdc99d8cb -
Trigger Event:
push
-
Statement type:
File details
Details for the file star_drawing-0.1.2-py3-none-any.whl.
File metadata
- Download URL: star_drawing-0.1.2-py3-none-any.whl
- Upload date:
- Size: 36.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09b4c9acf2c30f76884e167a624a1ffd3a65b20b7830aa40996f58f7368dd10d
|
|
| MD5 |
9806a70879fcd8a600cfb90116dfeb9a
|
|
| BLAKE2b-256 |
bacd12655e2776ede34d9d532d83315a3df4d57e2481be886db62360bcd12e71
|
Provenance
The following attestation bundles were made for star_drawing-0.1.2-py3-none-any.whl:
Publisher:
release.yml on banditburai/star-drawing
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
star_drawing-0.1.2-py3-none-any.whl -
Subject digest:
09b4c9acf2c30f76884e167a624a1ffd3a65b20b7830aa40996f58f7368dd10d - Sigstore transparency entry: 974821163
- Sigstore integration time:
-
Permalink:
banditburai/star-drawing@ba211bc702fc9b636db7c766068a866cdc99d8cb -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/banditburai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@ba211bc702fc9b636db7c766068a866cdc99d8cb -
Trigger Event:
push
-
Statement type: