Curved baseline beside a route
Wrap part of a route in a convex hull, offset it and cut out the far side as a clean baseline for charts along the route.
/**
* Demonstrates how to build a baseline beside a stretch of route, as a base
* for charts that follow the route.
*
* This builds on the Polyline Windowing demo. Polylines.clipToBox finds the
* parts of the route inside the window; the stretch from the first entry to
* the last exit is sliced out. Offsets.besideRoute then builds a baseline on
* one side of that stretch: it wraps the stretch's points on that side, grows
* them by the offset with rounded corners and cuts the result at the
* perpendiculars through the stretch's ends. The baseline runs in the route's
* direction, so the same side points away from the route on the baseline too.
*
* Drag the window corners to change the stretch, drag the offset slider to
* move the baseline away from the route, and switch the side with the
* buttons.
*/
import {
VisQuill, Reactive, Boxes, Points, Segments, Polylines,
Offsets, Values, Svg,
} from "@visquill/visquill-gdk";
import "./styles.css";
// ── Layout ────────────────────────────────────────────────────────────────────
const POLYLINE_POINTS: [number, number][] = [
[ 60, 340], [120, 180], [190, 400], [270, 130],
[350, 300], [420, 90], [510, 360], [590, 160],
[670, 320], [750, 110], [830, 280], [900, 380],
];
const SMOOTHING = 3;
const WINDOW_TL: [number, number] = [220, 110];
const WINDOW_BR: [number, number] = [660, 410];
const OFFSET_MIN = 20;
const OFFSET_MAX = 120;
const OFFSET_START = 60;
// Points inside each rounded corner of the baseline
const CORNER_POINTS = 6;
const buttonWidth = 70;
const buttonHeight = 24;
const buttonGap = 6;
export function run(div: HTMLDivElement) {
const rvg = VisQuill.create(div, "demos-hull-baseline-");
const canvas = rvg.canvas;
// ── Instructions ──────────────────────────────────────────────────────────
const instructions = canvas.text.label(
"Drag the window corners or the offset handle, switch the side",
"instructions"
);
Reactive.do([rvg.frame], () => {
Points.moveDown(Segments.midPoint(Boxes.topSide(rvg.frame)), 25, instructions);
});
// ── Source polyline ───────────────────────────────────────────────────────
const polyline = Polylines.smooth(POLYLINE_POINTS, SMOOTHING);
// The whole route dimmed, the stretch inside the window on top of it
Polylines.copy(polyline, canvas.visuals.polyline("route"));
const stretch = canvas.visuals.polyline("stretch");
// ── Draggable window ──────────────────────────────────────────────────────
const hTL = canvas.handles.disk("window-handle", WINDOW_TL);
const hBR = canvas.handles.disk("window-handle", WINDOW_BR);
const windowBox = canvas.visuals.box("window-rect");
Reactive.do([hTL, hBR], () => {
Boxes.spannedBox(hTL, hBR, windowBox);
});
// ── Offset slider ─────────────────────────────────────────────────────────
const sliderTrack = canvas.visuals.segment("slider-track");
const sliderHandle = canvas.handles.disk("slider-handle", [0, 0]);
const sliderLabel = canvas.text.label("", "slider-label");
const offset = canvas.values.real(OFFSET_START);
// Position the slider track reactively below the frame
Reactive.do([rvg.frame], () => {
const bl = Boxes.bottomLeft(rvg.frame);
const br = Boxes.bottomRight(rvg.frame);
Points.move(bl, 80, -40, sliderTrack.at(0));
Points.move(br, -80, -40, sliderTrack.at(1));
// Clamp the handle to the track on resize
const t = Values.inverseLerp(OFFSET_MIN, OFFSET_MAX, offset);
Segments.pointAt(sliderTrack, t, sliderHandle);
});
Reactive.do([sliderHandle], () => {
Points.closestPointOnSegment(sliderHandle, sliderTrack, sliderHandle);
const t = Points.relativePositionOnSegment(sliderHandle, sliderTrack);
Values.lerp(OFFSET_MIN, OFFSET_MAX, t, offset);
Points.moveUp(sliderHandle, 16, sliderLabel);
sliderLabel.value = `offset ${Math.round(offset.value)} px`;
}, true);
// ── Side buttons ──────────────────────────────────────────────────────────
//
// Left and right are seen from the route: walking from its first point
// to its last, here from the left of the canvas to the right.
const left = canvas.values.bool(true);
const title = canvas.text.label("side", "row-title");
const buttons = ["left", "right"].map(name =>
({ box: canvas.visuals.box("button"), label: canvas.text.label(name, "button-label") }));
const note = canvas.text.label("", "note");
const notes = [
"Left of the direction of travel: the route runs left to right, so the baseline lies above it.",
"Right of the direction of travel: below the route.",
];
Reactive.do([rvg.frame], () => {
const rowLeft = Points.move(Boxes.bottomLeft(rvg.frame), 20, -130);
Points.move(rowLeft, 0, buttonHeight / 2, title);
buttons.forEach(({ box, label }, i) => {
Boxes.box(Points.moveRight(rowLeft, 50 + i * (buttonWidth + buttonGap)), buttonWidth, buttonHeight, box);
Boxes.center(box, label);
});
Points.move(rowLeft, 50, buttonHeight + 16, note);
});
const select = (index: number) => {
buttons.forEach(({ box, label }, i) => {
const directive = i === index ? "@class active" : "@class-remove active";
box.applyStyleDirective(directive);
label.applyStyleDirective(directive);
});
note.value = notes[index];
left.value = index === 0;
};
buttons.forEach(({ box }, i) => Svg.get(box).addEventListener("click", () => select(i)));
select(0);
// ── Baseline ──────────────────────────────────────────────────────────────
const baseline = canvas.visuals.polyline("baseline");
const ranges: [number, number][] = [];
Reactive.do([windowBox, offset, left], () => {
// ── 1. Stretch ────────────────────────────────────────────────────────
//
// clipToBox returns every part inside the window; the baseline covers
// one stretch, from the first entry to the last exit.
Polylines.clipToBox(polyline, windowBox, ranges);
if (ranges.length === 0) {
stretch.elements.length = 0;
baseline.elements.length = 0;
return;
}
const from = ranges[0][0];
const to = ranges[ranges.length - 1][1];
Polylines.slice(polyline, from, to, stretch);
// ── 2. Baseline ───────────────────────────────────────────────────────
//
// besideRoute depends only on the stretch's points: it lies `offset`
// away on the chosen side and runs from the first point's end to the
// last point's, in the route's direction.
Offsets.besideRoute(stretch, offset, left, CORNER_POINTS, baseline);
});
}A smooth baseline beside a stretch of a route, as a base for charts along it. The stretch is wrapped
in a convex hull (ConvexHulls.topStart), offset with Offsets.polygon, and the far side is cut
out with Polygons.slice.
How it works
Polylines.clipToBox finds the parts of the route inside the window (see polylines-08-windowing). The baseline covers one stretch, from the first entry to the last exit:
const from = ranges[0][0];
const to = ranges[ranges.length - 1][1];
Polylines.slice(polyline, from, to, stretch);
Offsets.besideRoute builds the baseline on one side of the stretch. It wraps the stretch's points on that side in their convex hull, grows it by the offset with rounded corners, and cuts it at the perpendiculars through the stretch's ends:
Offsets.besideRoute(stretch, offset, left, CORNER_POINTS, baseline);
left means the left of the direction of travel, from the route's first point to its last. The baseline runs in the same direction, so a chart drawn on it with the same left points away from the route.
API
Polylines.clipToBox: the parts of the route inside the window, as ratio rangesPolylines.slice: the stretch between two ratiosOffsets.besideRoute: the baseline beside the stretch
Notes
Where the first or last edge of the stretch is steep on the chosen side, the baseline starts or ends farther than the offset from the route.
See also
geometry/polylines-08-windowingplots/curves-05-along-baseline
API used
Also used (30)
VisQuillVisQuill.createReactiveReactive.doBoxesBoxes.topSideBoxes.spannedBoxBoxes.bottomLeftBoxes.bottomRightBoxes.boxBoxes.centerPointsPoints.moveDownPoints.movePoints.closestPointOnSegmentPoints.relativePositionOnSegmentPoints.moveUpPoints.moveRightSegmentsSegments.midPointSegments.pointAtPolylinesPolylines.smoothPolylines.copyOffsetsValuesValues.inverseLerpValues.lerpSvgSvg.get




