Groups and coordinate systems
The canvas is a group, and every group can have child groups, called layers. A layer does three jobs: it is a coordinate system of its own, it adds to the CSS prefix, and it is the unit you hide, mask and remove.
A coordinate system of its own
Build a part of a graphic around the origin (0, 0) of its layer, and place it with the
layer’s anchor. Drag both handles:
// A small chart, built around (0, 0) of its own group
const widget = canvas.layer()
widget.visuals.box("shape", [-80, -50], [80, 50])
const base = widget.shapes.segment([-60, 35], [60, 35])
;[40, 65, 25].forEach((height, i) => {
const bar = widget.visuals.rectangle("accent")
Attach.rectangleToSegment(bar, height, base, { paddingLeft: 10 + i * 40, paddingRight: 90 - i * 40 })
})
// The anchor puts it on the screen: position, direction, and length = scale
Reactive.do([place, turn], () => {
widget.anchor.x = place.x
widget.anchor.y = place.y
widget.anchor.dx = (turn.x - place.x) / 120
widget.anchor.dy = (turn.y - place.y) / 120
})The anchor is a pin in the coordinates of the parent group:
- its position is where the layer’s origin lies,
- its direction turns the layer,
- the length of its direction scales it:
(1, 0)is the original size,(2, 0)twice as large,(0, 1)turned by 90°.
Everything inside keeps its own coordinates: the bars still stand on y = 35, whatever the
anchor does. Layers nest, and their anchors add up, so a part can be built from smaller parts
that each have their own origin. Handles work in their layer’s coordinates too.
To go between the coordinates of a layer and those of the whole graphic, ask the layer:
const widget = canvas.layer()
widget.anchor.x = 300
widget.anchor.y = 100
const onScreen = widget.toGraphic({ x: 50, y: 0 }) // (350, 100)
const inWidget = widget.fromGraphic({ x: 300, y: 150 }) // (0, 50)Ask the layer, not a visual in it: visual.toGraphic(p) takes p in the visual’s own
coordinates, which for a point or a handle are relative to that point.
Layers and CSS
canvas.layer("chart-") adds chart- to the prefix of everything created in the layer, after
the prefixes of its parents (Styles). canvas.layer() without an
argument adds nothing. That’s useful when you need the coordinate system, but not the names.
Showing, hiding, removing
A layer has the same reactive properties as a visual, and they apply to everything inside:
legend.visible.value = false // hidden, still laid out and updated
legend.mounted.value = false // taken out of the page, still updated
chart.zIndex.value = 1 // drawn above its siblings with a lower zIndex
legend.dispose() // gone for good, with everything created in itvisiblehides the layer, but leaves it in the page.mountedtakes it out of the page. Its objects still update, and when it is mounted again it shows the current state. Use it for parts that are hidden for a long time, so the browser has less to lay out.zIndexorders a layer among its siblings, as for visuals.dispose()removes the layer for good, with all values, shapes, visuals, attachments and child layers created in it. Afterwards none of them can be used any more.
Masks
A mask limits a layer to a region: only what lies inside the mask’s shapes is drawn. Drag the handle:
// Only what lies inside the mask's circle is drawn
const spot = full.mask.circle("white")
Reactive.do([lamp], () => {
Circles.circleAt(lamp, 80, spot)
})layer.mask.circle(color) and layer.mask.box(color) add an empty shape to the mask and
return it, so you fill and move it like any other shape. The color says how much shows through:
"white" all of it, black nothing, grey something in between. mask.clear() removes all shapes,
and the layer is drawn completely again.
