Skip to content

Free canvas

Shapes moved on a canvas with pink alignment guides, and a new shape dropped from the toolbar

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.0 and shadow=False make the shape itself move, as in a design tool.
  • snap=8 (or (x, y)) rounds the landing place to a grid.
  • guides=True aligns 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 in guide_color (pink, like Figma).
  • With snap or guides, the preview itself jumps to the snapped place (snap_preview=False keeps it under the pointer).

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)
  • bounds="parent" on the draggables keeps them inside the canvas.
  • axis="x" moves along one axis only (a timeline), and grid= on the draggable moves the preview itself in steps.
  • on_drag_move follows the drag in Python, for rulers or coordinates in a status bar.

The design canvas tutorial builds a complete one with a layers panel.