Skip to main content

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 presetsdrawing_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/importexport_svg() and import_svg() for serialization and persistence.
  • Scoped signals — built on starelements, so each canvas instance has its own signal namespace. No cross-instance interference.
  • Collaboration-readyonElementChange callback and applyRemoteChanges method 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 readonly attribute 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

Apache 2.0

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

star_drawing-0.1.0.tar.gz (64.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

star_drawing-0.1.0-py3-none-any.whl (30.8 kB view details)

Uploaded Python 3

File details

Details for the file star_drawing-0.1.0.tar.gz.

File metadata

  • Download URL: star_drawing-0.1.0.tar.gz
  • Upload date:
  • Size: 64.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for star_drawing-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ab3817261ef0e069395a8652da648c1e52b745600d44c8ad651037cd5c86ec1e
MD5 d35d99ec46173e0bbe264ddb89bb6a48
BLAKE2b-256 0013d94d0961dba4029190cc45282efbed7a43e282cb9600fab7dd7019cf1430

See more details on using hashes here.

Provenance

The following attestation bundles were made for star_drawing-0.1.0.tar.gz:

Publisher: release.yml on banditburai/star-drawing

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file star_drawing-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: star_drawing-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 30.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for star_drawing-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e8d4e83cc3a1ce007e27f7be68684d8a5d4122acf70e32a89785ce32bc93c0b7
MD5 33fd4906d165059b25453ade25f1ed13
BLAKE2b-256 8ff3ed65bbb5d66dfbc4d810908c0e8121732389c868447d970edfab1700aa15

See more details on using hashes here.

Provenance

The following attestation bundles were made for star_drawing-0.1.0-py3-none-any.whl:

Publisher: release.yml on banditburai/star-drawing

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page