Skip to main content

A minimal Python drawing library.

Project description

minidraw

minidraw is a minimal, pure-Python 2D drawing library designed around composable geometric primitives and spatial transformations. It enables you to build structured vector graphics, transform them fluently, and export them into multiple formats such as SVG or Python code.


✨ Features

  • Composable primitives: Line, Circle, Rectangle, Polyline, Arc, and Text.
  • Hierarchical grouping with transformation inheritance.
  • Fluent spatial transformations — translate, rotate, scale, and mirror any element.
  • Declarative styling using the Style dataclass.
  • Multiple rendering backends:
    • SVGBackend → export as .svg
    • PythonBackend → generate equivalent Python source code.
  • Simple, readable API that keeps math and rendering separate.

🧱 Core Modules

Module Description
point.py Defines Point, a transformable 2D coordinate class.
spatial.py Abstract base class defining the transformation interface.
primitives.py Geometric shapes and Group container.
style.py Explicit visual style dataclass shared by primitives.
transform.py Math helpers for rotation, scaling, and mirroring.
drawing.py Top-level Drawing object with backend rendering.

🧩 Basic Example

from minidraw import Drawing, Line, Circle, Style

d = Drawing()

# Add some primitives
l = Line((10, 10), (80, 80), style=Style(stroke="black"))
c = Circle((50, 50), 20, style=Style(stroke="red", fill="none"))

d.add(l, c)

# Apply transformations
c.copy().translate(40, 0).scale(1.2)

# Export
print(d.render_to_string("python"))   # → Python code output
d.render_to_file("example.svg")       # → SVG file

🔄 Transformations

All primitives implement:

  • translate(dx, dy) – move by offset.
  • rotate(angle, center=None) – rotate around a point.
  • resize(sx, sy, center=None) – scale along axes.
  • scale(factor, center=None) – uniform scale.
  • mirror(point, angle) – reflect across an arbitrary line.
  • mirror_vertical(x) / mirror_horizontal(y) – convenient reflections.

Transformations mutate the object in place, but copy() creates deep copies for safe chaining.


🎨 Styling

The Style dataclass allows fine-grained control over stroke, fill, text, and opacity:

Style(
    stroke="black",
    stroke_width=2.0,
    fill="none",
    opacity=0.8,
    font_size=14,
    text_anchor="middle",
)

Style objects can be merged using .merged(parent) to inherit defaults.


🧠 Example: Spatial Transform Demo

demo_spatial_transforms.py showcases all core transformations:

from minidraw import Drawing, Rectangle, Circle, Line, Polyline, Arc, Text, Style, Group

d = Drawing()
g = Group()

base = Rectangle((10, 10), (30, 20), style=Style(stroke="#999"))
rotated = base.copy().rotate(30, (25, 20)).set_style(Style(stroke="red"))

g.add(base, rotated)
d.add(g)

d.render_to_file("spatial_transforms_demo.svg")

Produces an SVG illustrating translation, rotation, scaling, and mirroring.


🧰 Backends

  • SVGBackend – converts primitives into valid SVG markup.
  • PythonBackend – generates equivalent Python code that recreates the drawing.

Custom backends can be implemented by subclassing Backend and overriding:

def render_to_string(self, elements: list[Primitive]) -> str:
    ...

def render_to_file(self, path: Path, elements: list[Primitive]) -> None:
    ...

📦 Installation

pip install minidraw

(for now, clone and install locally until released on PyPI)

git clone https://github.com/yourname/minidraw.git
cd minidraw
pip install -e .

🧾 License

MIT License © 2025 — Designed for simplicity and educational clarity.

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

minidraw-0.1.0.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

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

minidraw-0.1.0-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: minidraw-0.1.0.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.2

File hashes

Hashes for minidraw-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c71fdf986a29f01ae55fbc1fc9ed9c834048e5b2fba1136f67d54320daa5737c
MD5 5c3b668d47580c5a259e31e7ef2b4a86
BLAKE2b-256 d15195ba49a71bd5f16982bbc2dbca5deebfd364aabe409e351e8278b3d36e61

See more details on using hashes here.

File details

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

File metadata

  • Download URL: minidraw-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.2

File hashes

Hashes for minidraw-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9645cbd29611bf557eb15d9779ddd5e0163b8f43af5386797cd85bd3634dc1d0
MD5 7632f9999db701ba254e1127d2694fc6
BLAKE2b-256 e65078b04bc752005e926fbab723e85de8fdb7b267dcd278aed83adaa7f61537

See more details on using hashes here.

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