Geometry helpers
Most of the work in a VisQuill graphic is geometry: where does this label go, how long is that line, what is the outline around these points. The GDK has a few hundred helpers for that. You don’t need to know them; you need to know the one pattern they share, and where to look.
One pattern for all of them
A helper is a static function on a class named after what it works on, in the plural:
Points, Segments, Circles, Polygons, … Its inputs come first, and the object to write
the result into comes last:
const a = canvas.values.point([0, 0])
const b = canvas.values.point([300, 400])
const d = Points.distanceToPoint(a, b) // a new { value: 500 }
const m = Points.midPoint(a, b) // a new { x: 150, y: 200 }
const mid = canvas.values.point()
Points.midPoint(a, b, mid) // written into mid, and mid is returned
// Numbers too: map 18 °C on a scale from -10..40 °C to y = 250..30
const y = Values.remap(18, -10, 40, 250, 30) // { value: 126.8 }- With a target, the helper writes into it and returns it. That’s how a helper feeds a graphic: write into a value or visual that was created once, every time the inputs change (Derived values).
- Without one, it returns a new plain object:
{ x, y }for a point,{ value }for a number. Fine for a temporary result inside a block; nothing can depend on it. - Inputs are never changed. They can be reactive objects or plain ones:
[x, y],{ x, y }, numbers.
What there is
The API reference has every function; this is the map, by task.
| To | Look at |
|---|---|
| measure: lengths, distances, areas, angles | Segments.len; len, distanceTo on Polylines and Arcs; area, perimeter, centroid on Polygons; Circles.area; Shapes.boundingBox for any shape; Points.distanceToPoint, distanceToSegment; Vectors.angle |
| find a point on something | pointAt (by ratio) on Segments, Polylines, Polygons, Circles, Arcs; pinAt (with the direction there) and closestPoint on all of them but Segments; for segments Points.closestPointOnSegment |
| place a pin on or beside something | Pins.placeOnSegment, placeBesideSegment, placeOnCircle, placeOnArc, placeInRectangle |
| spread points out | Distribute.pointsAlongSegment, pointsOnCircle, pointsOnPolyline, pointsOnArc |
| construct a shape | Rectangles.alongSegment, Bars.fromSegment / fromPin, Circles.circleAt, Arcs.arcAt / arcBetween, Boxes.spannedBox, Symbols.square / diamond / triangle |
| move, turn, scale, mirror a shape | Shapes.moveBy, rotateBy, scaleBy, mirror, transform; Vectors.rotateBy for directions |
| find extremes in a set of points | Extremes.leftMost, topMost, closestTo, farthestFrom; ConvexHulls |
| draw around or along something | Offsets.polygon, polyline, convexContour; Curves.alongPolyline (a line at data-driven distances); Polylines.smooth, simplify |
| fit one thing into another | Fits.boxInBox, circleInBox, …: they return a pin, ready to be a group’s anchor |
| compute with numbers | Values.remap, lerp, clamp, smoothstep, round, … |
Two examples
A point that stays on a curve: closestPoint puts the dot on the polyline, distanceTo
measures how far the handle is from it.
const path = canvas.visuals.polyline("line", wave)
const handle = canvas.handles.disk("handle", [330, 60])
const nearest = canvas.visuals.point("dot")
canvas.visuals.segment("line", handle, nearest)
const label = canvas.text.label("", "label")
Reactive.do([handle], () => {
Polylines.closestPoint(path, handle, nearest)
label.x = handle.x + 16
label.y = handle.y - 12
label.value = `${Polylines.distanceTo(path, handle).value.toFixed(0)} px`
})An outline around a set of points, computed from scratch on every change:
Offsets.convexContour writes a rounded outline into an empty polygon.const points = [[200, 90], [330, 60], [440, 130], [380, 220], [240, 200]]
.map(p => canvas.handles.disk("handle", p))
// A rounded outline 30 px around all points
const outline = canvas.visuals.polygon("shape")
Reactive.do(points, () => {
Offsets.convexContour(points, 30, 6, outline)
})The outline is an empty canvas.visuals.polygon that the helper fills. Helpers that produce
polygons and polylines set as many vertices as they need, so you don’t have to know the count
beforehand.
Results are not reactive by themselves
A helper computes once, when it is called. Polygons.area(shape) doesn’t keep track of the
shape; call it again in a Reactive.do that depends on the shape, as in the
triangle areas. The only
ready-made relationships are attachments.
