Shapes and visuals

A shape is a list of points. Everything geometric in VisQuill (a segment, a rectangle, the outline of a country) is one, and a visual is a shape that is also drawn.

A shape is a list of points

Shape Points Created from points
segment 2: start and end segment(p1, p2)
box 2: opposite corners, edges parallel to the axes box(p1, p2)
triangle 3: the corners triangle(p1, p2, p3)
rectangle 4: the corners, may be rotated usually filled by a helper (below)
circle 3: points on the circle circle(p1, p2, p3), or circleAt(center, radius)
arc 3: start, a point on the arc, end arc(start, middle, end)
polygon, polyline any number polygon(vertices), or polygon(n) for n points at (0, 0)

You reach the points like the elements of a list:

const triangle = canvas.shapes.triangle([0, 0], [100, 0], [50, 80])

triangle.at(2).y = 90                // the third point
for (const p of triangle) p.x += 10  // all points
const count = triangle.elements.length

Sharing points

When you build a shape from point objects (handles, values, the vertices of another shape), the shape doesn’t copy them: it contains them. Two shapes built from the same point share it. Drag any corner:

Two triangles, built from four handles. The middle edge belongs to both.
const top = canvas.handles.disk("handle", [320, 50])
const bottom = canvas.handles.disk("handle", [320, 230])
const left = canvas.handles.disk("handle", [170, 140])
const right = canvas.handles.disk("handle", [470, 140])

// Both triangles use top and bottom: one edge, two shapes
canvas.visuals.triangle("shape", left, top, bottom)
canvas.visuals.triangle("shape", right, top, bottom)

This is the simplest relationship there is: no code runs at all. When the triangles are drawn, they read the same two points. It only works with objects, though: [x, y] arrays are copied (Plain numbers and arrays).

Shapes and visuals

canvas.shapes and canvas.visuals have the same factories, with one difference: a visual is drawn, so its factories take a style as the first argument.

  • Use a visual for what the reader should see.
  • Use a shape for geometry you only compute with: a baseline that bars stand on, a path that labels follow, a box you test points against.

A visual is a shape in every respect: all helpers take it, and you can build other shapes from its points. On top of that it has the properties of something on screen, such as visible, zIndex and mounted. They are reactive values: bar.visible.value = false hides bar.

Shapes that helpers fill in

Often you don’t know the points of a shape yet when you create it. Then create it empty, and let a helper compute the geometry:

// Empty visuals: four and three points at (0, 0)
const bar = canvas.visuals.rectangle("accent")
const dot = canvas.visuals.circle("shape")

// Helpers write the geometry into the shape passed last
Rectangles.alongSegment([100, 200], [300, 200], 80, true, bar)
Circles.circleAt([450, 140], 50, dot)

Geometry helpers take the object to write into as their last argument and return it. That’s why an empty visual is useful: create it once, then write into it, again and again, whenever the input changes (Derived values). Without that last argument, a helper returns a new plain object instead.

Rectangles.alongSegment(p1, p2, height, left, result) stands a rectangle on the segment from p1 to p2; left = true puts it on the left of the direction from p1 to p2, as you see it on screen. For a segment from left to right, that’s upwards.

const handle = canvas.handles.disk("handle", [100, 100])

// circleAt computes a circle around the handle's position *now*
const dot = canvas.visuals.circleAt("dot", handle, 20)

// To keep it there, attach it
Attach.circleToPoint(dot, handle)

Casting between shape types

Some shapes have the same points under another name: three points are a triangle, and also a circle through them. Cast changes the type without copying anything: Cast.asCircle(triangle) returns the same points typed as a circle, or null if the number of points doesn’t fit. Cast.toCircle skips that check.

Styles

The style is the first argument of every visual factory. It is a list of class names, with optional commands that start with @:

const canvas = VisQuill.create(div, "vqg-").canvas
const chart = canvas.layer("chart-")

chart.visuals.circle("bar selected")        // class="vqg-chart-bar vqg-chart-selected"
chart.visuals.circle(".highlight point")    // class="highlight vqg-chart-point"
chart.visuals.circle("bar @style fill: red") // class="vqg-chart-bar", style="fill: red"
  • Class names get a prefix: the one from VisQuill.create, followed by the prefixes of the layers the visual is in, so "bar" in the layer chart- becomes vqg-chart-bar.
  • A name starting with a dot is used as it is: ".highlight" becomes highlight, for classes your page defines anyway.
  • @style sets inline CSS, for the one-off case. Everything after it, up to the next @, is the CSS text.
  • @class is the explicit form of a class list, "@class bar selected".

Prefer classes and a stylesheet: inline styles are hard to change later, and CSS can do hover states and transitions that inline styles can’t.

Drawing order

Visuals are drawn in the order you create them; later ones lie on top. To change that, set zIndex: higher values are drawn above lower ones, within the same group. canvas.visuals.defaultZIndex and canvas.handles.defaultZIndex set it for everything created from then on, which is how the figures in this guide keep their handles on top.