# draw > draw is a free, in-browser Python drawing pad. Python runs in an isolated Pyodide Web Worker with no network access and draws to an SVG canvas. No install or account is required. ## Links - Editor: https://titchyrascal.com/ - Learn Python by drawing (22 short lessons): https://titchyrascal.com/learn - Gallery: https://titchyrascal.com/gallery - Blog: https://titchyrascal.com/blog - Privacy notice: https://titchyrascal.com/privacy - This file: https://titchyrascal.com/llms.txt ## Quick start ```python background("#0b1020") circle(400, 300, 120, fill="#38bdf8", stroke="none") text(400, 300, "hello", size=48, anchor="middle") ``` Press Run, or Ctrl/Cmd+Enter. `main.py` is the entry point. ## How code runs - Python runs in the browser through Pyodide inside an isolated Web Worker. The sandbox has no network access, no DOM access, no browser storage access and no pip. The Python standard library is available. - Only `main.py` is executed by Run. Other workspace files are importable modules and must be imported explicitly: ```python from shapes import Rect, Point ``` - The drawing helpers are also installed in Python's `builtins`, so functions and classes in imported modules can call `circle()`, `rect()`, etc. without importing them. - `if __name__ == "__main__":` works in `main.py`. - Run always clears the canvas before executing. Auto draw also clears before each run. - The Test button runs `test.py`, `tests.py`, `test_*.py` and `*_test.py` files separately, with workspace imports available. Test failures show a traceback and a summary. - Limits: 5,000 shapes per run, 2,000 points per polygon/bezier, 1,000 characters of text, 20,000 drawing messages and 200,000 bytes of console output. ## Canvas - The canvas is an SVG with the origin at the top-left. x increases to the right and y increases downward. - Coordinates are canvas units. The canvas fits the available browser space automatically and keeps at least the default 800 x 600 extent, or you can set a fixed size in the Canvas size dialog (100 to 4000) and use "Fit to window" to return to automatic sizing. - For a predictable layout, either target an approximately 800 x 600 scene or set a fixed canvas size. - `background(color)` changes the canvas background. `clear()` removes every shape and restores the default background. ## Colours and paint - `fill` and `stroke` accept CSS colour strings such as `"#38bdf8"`, `"#f00"`, `"tomato"`, `"none"` or `"transparent"`. - The default stroke colour is `#f8fafc`; the default fill is `"none"`. - `opacity` is 0 to 1. `stroke_width` is in canvas units. - Gradients return a paint string (`url(#...)`) that can be passed to `fill` or `stroke`. ## Drawing routines ```python clear() background(color="#0b1020") circle(cx, cy, r, fill="none", stroke="#f8fafc", stroke_width=2, opacity=1.0) ellipse(cx, cy, rx, ry, fill="none", stroke="#f8fafc", stroke_width=2, opacity=1.0) rect(x, y, w, h, fill="none", stroke="#f8fafc", stroke_width=2, opacity=1.0, radius=0) line(x1, y1, x2, y2, stroke="#f8fafc", stroke_width=2, opacity=1.0) polygon(points, fill="none", stroke="#f8fafc", stroke_width=2, opacity=1.0) bezier(points, fill="none", stroke="#f8fafc", stroke_width=2, opacity=1.0) path(commands, fill="none", stroke="#f8fafc", stroke_width=2, opacity=1.0) arc(cx, cy, r, start=0, end=360, fill="none", stroke="#f8fafc", stroke_width=2, opacity=1.0) text(x, y, content, size=20, fill="#f8fafc", anchor="start", weight="normal", opacity=1.0) dot(x, y, r=4, fill="#f8fafc", opacity=1.0) linear_gradient(stops, x1=0.0, y1=0.0, x2=1.0, y2=0.0) radial_gradient(stops, cx=0.5, cy=0.5, r=0.5, fx=None, fy=None) start_mask() end_mask() ``` ### Shapes - `circle(cx, cy, r)` draws a circle centred at `(cx, cy)`. - `ellipse(cx, cy, rx, ry)` draws an ellipse with horizontal radius `rx` and vertical radius `ry`. - `rect(x, y, w, h)` draws a rectangle from the top-left corner. `radius` rounds the corners. - `line(x1, y1, x2, y2)` draws a straight line. Lines have no fill. - `polygon(points)` draws a closed polygon. `points` is a list of `(x, y)` pairs. - `bezier(points)` draws a quadratic curve from 3 points, or a cubic curve from 4 points. - `path(commands)` draws a path from a list of command tuples. - `arc(cx, cy, r, start, end)` draws a circular arc with angles in degrees, 0 at 3 o'clock, increasing clockwise. With a fill it is a pie slice. A sweep of about 360 draws a full circle. - `text(x, y, content)` draws text. `anchor` is `"start"`, `"middle"` or `"end"`. `weight` is `"normal"`, `"bold"`, `"lighter"`, `"bolder"` or `"100"` to `"900"`. - `dot(x, y, r)` is a filled circle with no stroke. ### Path commands `path` accepts a list of tuples: ```python path([ ("M", 100, 100), ("L", 300, 100), ("Q", 350, 150, 300, 200), ("C", 250, 240, 150, 240, 100, 200), ("A", 50, 50, 0, 0, 1, 50, 150), ("Z",), ], fill="none", stroke="#38bdf8", stroke_width=4) ``` - `("M", x, y)` move, `("L", x, y)` line, `("Q", cx, cy, x, y)` quadratic curve. - `("C", c1x, c1y, c2x, c2y, x, y)` cubic curve. - `("A", rx, ry, rotation, large_arc, sweep, x, y)` elliptical arc. `rotation` is in degrees; `large_arc` and `sweep` are 0 or 1. - `("Z",)` closes the path. ### Gradients Gradients use object-relative coordinates from 0 to 1, so they adapt to the shape they fill. `stops` is a list of `(offset, colour, opacity)` tuples; opacity is optional and defaults to 1. ```python sky = linear_gradient([ (0, "#0ea5e9", 1), (1, "#a855f7", 1), ]) rect(0, 0, 800, 600, fill=sky, stroke="none") glow = radial_gradient([ (0, "#f8fafc", 1), (0.5, "#38bdf8", 1), (1, "#0b1020", 0), ]) circle(400, 300, 160, fill=glow, stroke="none") ``` - `linear_gradient` runs from `(x1, y1)` to `(x2, y2)`. The default is left to right. - `radial_gradient` uses centre `(cx, cy)` and radius `r`; `fx` and `fy` optionally move the focal point. ### Masks `start_mask()` begins recording a luminance stencil. Draw white shapes to reveal the drawing that already exists and black shapes to hide it. `end_mask()` applies the stencil to the shapes drawn before `start_mask()`. The stencil shapes themselves are not drawn. Shapes drawn after `end_mask()` are not masked. ```python rect(20, 20, 760, 560, fill="#38bdf8", stroke="none") start_mask() circle(400, 300, 200, fill="white", stroke="none") end_mask() ``` ## Examples ### Small scene ```python import math background("#0b1020") # sun circle(640, 130, 56, fill="#fbbf24", stroke="none") for i in range(12): a = i * math.pi / 6 line( 640 + math.cos(a) * 70, 130 + math.sin(a) * 70, 640 + math.cos(a) * 92, 130 + math.sin(a) * 92, stroke="#fbbf24", stroke_width=4, opacity=0.7, ) # hills polygon([(0, 450), (180, 330), (360, 450), (540, 320), (800, 460), (800, 600), (0, 600)], fill="#065f46", stroke="none") polygon([(0, 520), (220, 430), (440, 520), (660, 400), (800, 500), (800, 600), (0, 600)], fill="#064e3b", stroke="none") text(60, 92, "hello, canvas", size=44, fill="#f8fafc") ``` ### Classes in another file `shapes.py`: ```python class Point: def __init__(self, x, y): self.x = x self.y = y class Rect: def __init__(self, x, y, w, h, colour="#38bdf8"): self.x = x self.y = y self.w = w self.h = h self.colour = colour def draw(self): rect(self.x, self.y, self.w, self.h, fill=self.colour, stroke="none") ``` `main.py`: ```python from shapes import Rect, Point Rect(100, 100, 200, 120).draw() ``` Drawing helpers are available inside `shapes.py` because they are installed in Python's builtins. ### Tests `test_shapes.py`: ```python from shapes import Rect rect = Rect(0, 0, 100, 50) assert rect.w == 100 assert rect.h == 50 ``` The Test button runs test files independently and reports PASS/FAIL for each file. ## Sharing and gallery - Share dialog: full-page viewer at `/view#draw=`, embeddable canvas at `/embed#draw=`, and an editable copy at `/?copy=1#draw=`. - The encoded workspace is a URL-encoded base64 JSON document containing `files`, `activeId` and canvas dimensions. The share dialog creates these links; manual encoding is not needed. - `/view` and `/embed` are marked `noindex`. The gallery and blog are public pages; gallery drawings can be opened as remixes. ## Accounts and privacy - The editor, tutorial and drawing sandbox work without an account. - Accounts are optional and are only needed to save projects, share files to the library or publish gallery drawings. - Analytics are cookieless. IP addresses are used only transiently for rate limiting and to compute a daily salted visitor hash; they are not stored in analytics events, traces or logs. See https://titchyrascal.com/privacy for details.