Values and points
A value is a reactive container: you read and write it like a variable, and VisQuill knows when it changes. Values are what you change to make a graphic move, and what relationships depend on.
The kinds of values
All values come from canvas.values:
const height = canvas.values.real(120) // height.value
const selected = canvas.values.bool(false) // selected.value
const title = canvas.values.string("Rainfall") // title.value
const row = canvas.values.item({ city: "Bonn", mm: 61 }) // row.value
const p = canvas.values.point([100, 50]) // p.x, p.y
const pin = canvas.values.pin([100, 50]) // pin.x, pin.y, pin.dx, pin.dy
height.value = 150
p.x += 10| Kind | Holds | Read and write |
|---|---|---|
real |
a number | .value |
bool |
true or false |
.value |
string |
a text | .value |
item |
any JavaScript object | .value |
point |
a position | .x, .y |
pin |
a position and a direction | .x, .y, .dx, .dy |
Writing is a change: height.value = 150 updates everything that depends on height, and so
does p.x += 10 for p. For an item, only a new object counts; changing a field inside the
object goes unnoticed (Changes VisQuill can’t see).
You will also meet values you didn’t create yourself: every visual has a visible flag, a
zIndex and more, and they work the same way, so circle.visible.value = false hides a circle.
Points
A point is a position in pixels, x to the right and y downwards. Handles are points too,
and so are the vertices of every shape: anything that has x and y can be used where a point
is expected.
Pins: points with a direction
A pin adds a direction vector (dx, dy), by default (1, 0), pointing right. Pins are for
everything that has an orientation: the anchor of a bar that should turn, a label along a
line, and the origin of a group (whose direction rotates and scales the whole group; see the
chapter on groups). Drag both handles:
const base = canvas.handles.disk("handle", [220, 170])
const tip = canvas.handles.disk("handle", [380, 100])
// A pin at the base, pointing at the tip
const pin = canvas.values.pin()
Reactive.do([base, tip], () => {
Pins.placeAt(pin, base, pin)
Pins.lookAt(pin, tip, pin)
})
// A bar that turns with the pin
const bar = canvas.visuals.rectangle("shape")
Attach.rectangleToPoint(bar, pin, { width: 120, height: 36, anchorX: "left", orientation: "relative" })The helpers in Pins do the usual pin work: placeAt moves a pin, lookAt turns it
towards a point, rotateBy turns it by an angle, withLength sets the length of its
direction. Like all geometry helpers, they write into the object passed last and leave their
input alone: Pins.lookAt(pin, tip, pin) turns pin itself, Pins.lookAt(pin, tip) returns
a turned copy.
Plain numbers and arrays
You don’t need a reactive value for everything. Wherever the GDK reads a point, [x, y] and
{ x, y } are accepted too, and wherever it reads a number, a plain number is. The difference:
- A reactive value is used as it is: the shape or attachment keeps following it.
- A plain number or array is copied into a new object when it is read, and nothing links the two afterwards.
const a = canvas.values.point([100, 100])
const line = canvas.visuals.segment("line", a, [300, 100])
a.x = 150 // the line's start moves: a is part of the line
line.at(1).x = 350 // [300, 100] became a point of its own; this is how to reach itSo when something should follow a value later, pass the value. When it is fixed, a plain number or array keeps the code short.
