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.1.tar.gz (65.2 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.1-py3-none-any.whl (31.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: star_drawing-0.1.1.tar.gz
  • Upload date:
  • Size: 65.2 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.1.tar.gz
Algorithm Hash digest
SHA256 4e0ecd2a41dfc955b4333d19539bec3ea0b12e4d3244a7814d1266deea9cafcc
MD5 41b9e63e51b679bd5e503860da7bddfa
BLAKE2b-256 c5a0ad7659768d0373dffd528eccf48b1602356bae7110d3af5b9888a8f816be

See more details on using hashes here.

Provenance

The following attestation bundles were made for star_drawing-0.1.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: star_drawing-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 31.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f11281459de450252a3a308778b02e4b82bc62549b090b7153a02e70a119a2f3
MD5 fd9f3b5594a824d018b45abf93dba222
BLAKE2b-256 4bfac7a54baaad23babb0d71f2bf223d2d284c12eac6dea79c929508da2c828a

See more details on using hashes here.

Provenance

The following attestation bundles were made for star_drawing-0.1.1-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