Design canvas

A canvas like Figma’s or Canva’s:
examples/dnd/design_canvas.py.
python examples/dnd/design_canvas.py-
Shapes with positions. Each shape is a dict with
x,y,w,h. The layers list is also the drawing order: the first one is on top. -
The canvas is a droppable. A stack of pinned draggables, inside a droppable that snaps and draws guides:
canvas = pz.droppable(pz.stack([pz.space(width="fill", height="fill"), *[self.placed(s) for s in reversed(self.layers)]]),"canvas", accepts="shape", snap=self.grid or None, guides=self.guides, over_style=False,on_drop=self.drop) -
Shapes move themselves.
source="hide"hides the original andlift=1.0,shadow=False,drop_animation=0make the preview look like the shape itself:piece = pz.draggable(self.body(s), s["id"], type="shape", source="hide", lift=1.0, shadow=False,drop_animation=0)return pz.pin(piece, x=s["x"], y=s["y"]) -
Drop where it lands. The event’s
item_xanditem_yare already snapped and aligned:def drop(self, event):if event.source.startswith("new-"):item = shape(event.data, event.item_x, event.item_y) # from the toolbarself.layers.insert(0, item)else:item = next(s for s in self.layers if s["id"] == event.source)item["x"], item["y"] = event.item_x, event.item_y -
The toolbar. Each tool is a draggable that stays (
source="keep") and carries the kind of shape indata. -
Layers. A sortable of the same list with
lock_axis=True: reordering a layer changes what is drawn on top.