Map a shape onto a path
Lay a polyline pattern along another curve so that its proportions and offsets follow the path; edit both with handles.
import {
VisQuill, Reactive, Boxes, Segments, Points, Polylines
} from "@visquill/visquill-gdk"
import './styles.css'
// ─── Layout ───────────────────────────────────────────────────────────────
// The "shape" polyline — a zigzag wave defined in its own local space.
// The length proportions of its segments determine how the mapped result
// is distributed along the target path.
const initialShapePoints: [number, number][] = [
[50+ 0, 500],
[50+ 80, 460],
[50+160, 500],
[50+240, 560],
[50+320, 500],
[50+400, 560],
[50+480, 500],
[50+560, 460],
[50+640, 500],
]
// The "path" polyline — defines where the mapped result is drawn.
const initialPathPoints: [number, number][] = [
[ 80, 300],
[200, 150],
[360, 380],
[520, 120],
[680, 300],
[820, 200],
]
const smoothingIterations = 3
/**
* Demonstrates `Polylines.mapOnPolyline()`.
*
* Two sets of draggable handles define the shape and the path.
* The shape's segment-length proportions are projected onto the path:
* a short segment in the shape maps to a short portion of the path,
* a long one to a longer portion. The mapped vertices lie on the path;
* the shape's up-and-down offsets are not carried over.
*
* Drag the orange shape handles to change the pattern being mapped.
* Drag the blue path handles to change where and how it is laid out.
*
* @param div - The div element that will contain the graphic.
*/
export function run(div: HTMLDivElement) {
const rvg = VisQuill.create(div, "demos-polyline-map-")
const canvas = rvg.canvas
// ─── Instruction label ─────────────────────────────────────────────
const instructions = canvas.text.label(
"Orange handles: edit shape · Blue handles: edit path",
"instructions"
)
Reactive.do([rvg.frame], () => {
Points.moveDown(
Segments.midPoint(Boxes.topSide(rvg.frame)),
25,
instructions
)
})
// ─── Shape polyline + handles ──────────────────────────────────────
// The shape stays in its original position for reference. Its segment
// proportions — not its absolute coordinates — determine the mapping.
const shapeHandles = initialShapePoints.map(pt =>
canvas.handles.disk("shape-handle", pt)
)
const shapePolyline = canvas.visuals.polyline("shape-reference")
// ─── Path polyline + handles ───────────────────────────────────────
const pathHandles = initialPathPoints.map(pt =>
canvas.handles.disk("path-handle", pt)
)
const pathSmooth = canvas.visuals.polyline("path-smooth")
// ─── Mapped result ─────────────────────────────────────────────────
// One vertex per shape vertex, placed on the smooth path.
const mapped = canvas.visuals.polyline("mapped")
// ─── Top-left info labels ──────────────────────────────────────────
const labelShape = canvas.text.label("shape: drag orange handles", "info-label")
const labelPath = canvas.text.label("path: drag blue handles", "info-label")
Reactive.do([rvg.frame], () => {
const topLeft = Boxes.topLeft(rvg.frame)
Points.move(topLeft, 20, 60, labelShape)
Points.move(topLeft, 20, 80, labelPath)
})
// ─── Reactive mapping ──────────────────────────────────────────────
// Rebuild whenever either set of handles moves.
Reactive.do([...shapeHandles, ...pathHandles], () => {
// Rebuild the shape polyline from its handles.
Polylines.polyline(shapeHandles, shapePolyline)
// Rebuild and smooth the path polyline.
Polylines.smooth(pathHandles, smoothingIterations, pathSmooth)
// Map: each vertex of the shape lands on the path at the same
// fraction of the length at which it sits along the shape.
Polylines.mapOnPolyline(shapePolyline, pathSmooth, mapped)
})
}A zigzag shape laid out along a curved path. Polylines.mapOnPolyline places each vertex of the
shape at the same fraction of the path's length as on the shape itself.
How it works
Polylines.mapOnPolyline looks at how far along its own length each vertex of the shape sits, as a fraction. It then puts a vertex at the same fraction of the path's length. A short edge in the shape becomes a short stretch of the path; a long edge a long one.
Reactive.do([...shapeHandles, ...pathHandles], () => {
Polylines.polyline(shapeHandles, shapePolyline)
Polylines.smooth(pathHandles, smoothingIterations, pathSmooth)
Polylines.mapOnPolyline(shapePolyline, pathSmooth, mapped)
})
The result has as many vertices as the shape, and they all lie on the path. Only the spacing of the shape matters: moving an orange handle up or down changes the result only as far as it changes the edge lengths.
API
Polylines.mapOnPolyline: places a polyline's vertices along another polyline by length fractionPolylines.polyline: writes vertices into a polylinePolylines.smooth: rounds the path
See also
geometry/polylines-07-distance-smaplingplots/curves-01-along-polyline




