Free canvas

A canvas is a pz.droppable around a
pz.stack of pz.pinned
draggables. The drop event’s item_x and item_y say where the item lands: save them and the
item is drawn there.
class Canvas(pz.App): def __init__(self): self.shapes = {"a": {"x": 40, "y": 40}, "b": {"x": 220, "y": 120}}
def drop(self, e): self.shapes[e.source].update(x=e.item_x, y=e.item_y)
def view(self): pieces = [pz.pin(pz.draggable(box(), key, source="hide", lift=1.0, shadow=False), x=s["x"], y=s["y"]) for key, s in self.shapes.items()] return pz.droppable(pz.stack([pz.space(width="fill", height="fill"), *pieces]), "canvas", snap=8, guides=True, over_style=False, on_drop=self.drop)source="hide",lift=1.0andshadow=Falsemake the shape itself move, as in a design tool.snap=8(or(x, y)) rounds the landing place to a grid.guides=Truealigns the item’s edges and center with the other draggables inside the target (and the target’s own edges and center) when they are within a few pixels, and draws the guides inguide_color(pink, like Figma).- With
snaporguides, the preview itself jumps to the snapped place (snap_preview=Falsekeeps it under the pointer).
New items from a toolbar
Section titled “New items from a toolbar”A toolbar button is a draggable with source="keep" and the kind of item in data. The drop
creates it where it lands:
tool = pz.draggable(ui.icon_button("square"), "new-rect", data="rect", type="shape", source="keep")
def drop(self, e): if e.source.startswith("new-"): self.shapes[new_id()] = {"kind": e.data, "x": e.item_x, "y": e.item_y} else: self.shapes[e.source].update(x=e.item_x, y=e.item_y)Limits
Section titled “Limits”bounds="parent"on the draggables keeps them inside the canvas.axis="x"moves along one axis only (a timeline), andgrid=on the draggable moves the preview itself in steps.on_drag_movefollows the drag in Python, for rulers or coordinates in a status bar.
The design canvas tutorial builds a complete one with a layers panel.