Skip to content

Isometric line figures that answer the pointer. For React and for anything with a DOM.

Figures: every inner ring of a slab keeps an even gap to its edge as drawn

Lucas Marques465059ef2461
diff --git a/packages/hairline/package.json b/packages/hairline/package.json
index 043139f..253ebcd 100644
--- a/packages/hairline/package.json
+++ b/packages/hairline/package.json
@@ -83,7 +83,7 @@
{
"name": "vanilla, every figure",
"path": "dist/index.js",
- "limit": "42.0 kB"
+ "limit": "42.3 kB"
},
{
"name": "vanilla, one figure",
@@ -94,7 +94,7 @@
{
"name": "react, every component",
"path": "dist/react.js",
- "limit": "42.4 kB",
+ "limit": "42.7 kB",
"ignore": [
"react",
"react/jsx-runtime"
diff --git a/packages/hairline/src/figures/hub.ts b/packages/hairline/src/figures/hub.ts
index 96e206b..aec89ac 100644
--- a/packages/hairline/src/figures/hub.ts
+++ b/packages/hairline/src/figures/hub.ts
@@ -1,7 +1,7 @@
import { Cam, clamp, facing, fit, hull, open, poly, proj, ringAt, rrect, seg, type Ring, type Vec2, type Vec3 } from "../core/iso";
import { reducedMotion, spring, stepS, tdone, tset, tval, tween, type Spring, type Tween } from "../core/motion";
import { disposer, mk, pointer, put, register, solid, type FigureMount, type Solid } from "../core/stage";
-import { lid, slab } from "../slab";
+import { inset, slab } from "../slab";
/**
* Hub: a thick plate in the middle of a faint floor grid, eight thin tiles
@@ -79,14 +79,16 @@ export const mount: FigureMount = ({ stage, svg, read }, value) => {
const order: (Tile | null)[] = [...tiles, null].sort((a, b) => (a ? a.x + a.y : 0) - (b ? b.x + b.y : 0));
for (const t of order) {
if (!t) {
- const [r, ri] = lid(-HUB, -HUB, HUB, HUB, 5, 1.8);
+ // the lip and the frame keep an even gap to the plate's edge as drawn; the mark stands free
+ const r = rrect(-HUB, -HUB, HUB, HUB, 7, 8), ri = inset(C, r, 1.3), fr = inset(C, r, 2.8), mr = rrect(-7, -7, 7, 7, 2, 8);
put(solid(g), slab(P, front, r, ri, 0, HT));
- mk("path", { d: open(ringAt(P, rrect(-13, -13, 13, 13, 3.5, 4).concat(rrect(-13, -13, 13, 13, 3.5, 4).slice(0, 1)), HT)), class: "nf lo" }, g);
- const md = open(ringAt(P, rrect(-7, -7, 7, 7, 2, 4).concat(rrect(-7, -7, 7, 7, 2, 4).slice(0, 1)), HT));
+ mk("path", { d: open(ringAt(P, fr.concat(fr.slice(0, 1)), HT)), class: "nf lo" }, g);
+ const md = open(ringAt(P, mr.concat(mr.slice(0, 1)), HT));
mark = mk("path", { d: md, class: "nf hi" }, g);
continue;
}
- [t.ring, t.inner] = lid(t.x - HS, t.y - HS, t.x + HS, t.y + HS, 2.8, 1.8);
+ t.ring = rrect(t.x - HS, t.y - HS, t.x + HS, t.y + HS, 4, 8);
+ t.inner = inset(C, t.ring, 1.1);
t.el = solid(g);
}
diff --git a/packages/hairline/src/figures/rebuild.ts b/packages/hairline/src/figures/rebuild.ts
index 7859cc5..16c20af 100644
--- a/packages/hairline/src/figures/rebuild.ts
+++ b/packages/hairline/src/figures/rebuild.ts
@@ -1,7 +1,7 @@
-import { Cam, facing, fit, hull, lerp, poly, proj, ringAt, rings, seg, unproj, type Ring, type Vec2, type Vec3 } from "../core/iso";
+import { Cam, facing, fit, hull, lerp, poly, proj, ringAt, rrect, seg, unproj, type Ring, type Vec2, type Vec3 } from "../core/iso";
import { EASE_LIFT, reducedMotion, tdone, tset, tval, tween, type Tween } from "../core/motion";
import { disposer, mk, pointer, put, register, solid, type FigureMount, type Solid } from "../core/stage";
-import { lid, slab } from "../slab";
+import { inset, slab } from "../slab";
/**
* Rebuild: a monorepo as a tree of package tiles on a rounded plinth, a thick
@@ -49,7 +49,7 @@ export const mount: FigureMount = ({ stage, svg, read }, value) => {
const bag = disposer();
let rise = value, act = -1, grace: number | null = null;
const NODES = TREE.map((t) => ({ ...t }) as Node);
- const half = (n: Node) => (n.l ? HALF : HUBH), thick = (n: Node) => (n.l ? T : TH);
+ const half = (n: Node) => (n.l ? HALF : HUBH), thick = (n: Node) => (n.l ? T : TH), round = (n: Node) => (n.l ? 4.6 : 7);
NODES.forEach((n, i) => { n.i = i; n.x = n.d + n.s; n.y = n.d - n.s; n.h = half(n); n.tw = tween(0); n.drawn = NaN; n.want = !n.i; n.at = undefined; });
const C = Cam(45, 0.5, 1.5);
@@ -77,19 +77,18 @@ export const mount: FigureMount = ({ stage, svg, read }, value) => {
// each dash is its own path, so the pulse can take them one by one (the stage eases the ink)
for (const L of links) for (const e of L.dashes) e.el = mk("path", { d: e.d, class: "nf" }, g);
for (const n of NODES) {
- const [r] = rings(n.x - n.h, n.y - n.h, n.x + n.h, n.y + n.h, 3, 1);
+ const r = rrect(n.x - n.h, n.y - n.h, n.x + n.h, n.y + n.h, round(n), 8);
n.ring = r;
mk("path", { d: poly(ringAt(P, r, 0)), class: "nf lo" }, g);
}
// Back to front by x + y of the centre.
const order = NODES.slice().sort((a, b) => a.x + a.y - (b.x + b.y));
for (const n of order) {
- const [r, ri] = lid(n.x - n.h, n.y - n.h, n.x + n.h, n.y + n.h, n.l ? 2.8 : 3.6, n.l ? 1.8 : 2);
- n.ring = r; n.inner = ri; n.el = solid(g);
+ const r = rrect(n.x - n.h, n.y - n.h, n.x + n.h, n.y + n.h, round(n), 8);
+ n.ring = r; n.inner = inset(C, r, n.l ? 1.1 : 1.3); n.el = solid(g);
// the root carries a recessed square in its lid
if (!n.l) {
- const [rs] = rings(n.x - 8, n.y - 8, n.x + 8, n.y + 8, 2, 0.5);
- n.rec = rs; n.recEl = mk("path", { class: "nf lo" }, n.el.g);
+ n.rec = inset(C, r, 2.8); n.recEl = mk("path", { class: "nf lo" }, n.el.g);
}
}
const draw = (n: Node, h: number) => {
diff --git a/packages/hairline/src/figures/relay.ts b/packages/hairline/src/figures/relay.ts
index 958e5ff..df42052 100644
--- a/packages/hairline/src/figures/relay.ts
+++ b/packages/hairline/src/figures/relay.ts
@@ -1,7 +1,7 @@
import { Cam, facing, fit, lerp, poly, proj, ringAt, rrect, seg, type Ring, type Vec2, type Vec3 } from "../core/iso";
import { tdone, tset, tval, tween, type Tween } from "../core/motion";
import { disposer, mk, pointer, put, register, solid, type FigureMount, type Solid } from "../core/stage";
-import { lid, slab } from "../slab";
+import { inset, slab } from "../slab";
/**
* Relay: a hub plate and four branches of thin tiles on a floor, joined by
@@ -75,15 +75,16 @@ export const mount: FigureMount = ({ stage, svg, read }, value) => {
// The hub: a thick plate with a recessed square set in its top.
const hub = solid(g);
- const [hr, hi] = lid(-HS, -HS, HS, HS, 5, 2);
- put(hub, slab(P, front, hr, hi, 0, HT));
- const rim = mk("path", { d: poly(ringAt(P, rrect(-HS + 6, -HS + 6, HS - 6, HS - 6, 2.5, 4), HT)), class: "nf hi" }, g);
+ const hr = rrect(-HS, -HS, HS, HS, 7, 8);
+ put(hub, slab(P, front, hr, inset(C, hr, 1.3), 0, HT));
+ const rim = mk("path", { d: poly(ringAt(P, inset(C, hr, 2.8), HT)), class: "nf hi" }, g);
const rimW = tween(1); let rimD = true;
- mk("path", { d: poly(ringAt(P, rrect(-HS + 9, -HS + 9, HS - 9, HS - 9, 1.2, 4), HT)), class: "nf lo" }, g);
+ mk("path", { d: poly(ringAt(P, inset(C, hr, 3.8), HT)), class: "nf lo" }, g);
// The tiles, back to front by x + y.
for (const n of nodes.slice().sort((p, q) => p.c[0] + p.c[1] - q.c[0] - q.c[1])) {
- [n.ring, n.inner] = lid(n.c[0] - TS, n.c[1] - TS, n.c[0] + TS, n.c[1] + TS, 2.5, 1.6);
+ n.ring = rrect(n.c[0] - TS, n.c[1] - TS, n.c[0] + TS, n.c[1] + TS, 3.6, 8);
+ n.inner = inset(C, n.ring, 1);
n.el = solid(g);
}
diff --git a/packages/hairline/src/figures/settle.ts b/packages/hairline/src/figures/settle.ts
index d0a8189..07dad09 100644
--- a/packages/hairline/src/figures/settle.ts
+++ b/packages/hairline/src/figures/settle.ts
@@ -1,7 +1,7 @@
import { Cam, clamp, facing, fit, lerp, poly, proj, rad, ringAt, rrect, seg, unproj, type Ring, type Vec2, type Vec3 } from "../core/iso";
import { reducedMotion, spring, stepS, tdone, tset, tval, tween, type Spring, type Tween } from "../core/motion";
import { disposer, mk, pointer, put, register, solid, type FigureMount, type Solid } from "../core/stage";
-import { lid, slab } from "../slab";
+import { inset, slab } from "../slab";
/**
* Settle: a hub plate on a faint floor grid and twelve thin tiles lying
@@ -70,11 +70,11 @@ export const mount: FigureMount = ({ stage, svg, read }, value) => {
// the hub: a thick slab with a recessed square set into its lid
const hub = mk("g", {}, g);
- const [hr, hi] = lid(-HUB, -HUB, HUB, HUB, 4.5, 1.8);
+ const hr = rrect(-HUB, -HUB, HUB, HUB, 7, 8);
const hs = solid(hub);
- put(hs, slab(P, front, hr, hi, 0, HUBT));
+ put(hs, slab(P, front, hr, inset(C, hr, 1.3), 0, HUBT));
hs.sil.classList.add("hi");
- mk("path", { d: poly(ringAt(P, rrect(-9.5, -9.5, 9.5, 9.5, 2.5, 4), HUBT)), class: "nf lo" }, hub);
+ mk("path", { d: poly(ringAt(P, inset(C, hr, 2.8), HUBT)), class: "nf lo" }, hub);
const tiles: Tile[] = T.map((t, i) => ({
i, t, el: solid(mk("g", {}, g)),
@@ -101,7 +101,8 @@ export const mount: FigureMount = ({ stage, svg, read }, value) => {
tl.k = p.x + p.y + 6 * p.z;
if (key === tl.key) return;
tl.key = key;
- put(tl.el, slab(P, front, turned(p.x, p.y, HALF, 2.3, p.a), turned(p.x, p.y, HALF - 1.6, 1, p.a), p.z, p.z + TH));
+ const r = turned(p.x, p.y, HALF, 3.6, p.a);
+ put(tl.el, slab(P, front, r, inset(C, r, 1), p.z, p.z + TH));
}
let order = "";
diff --git a/packages/hairline/src/slab.ts b/packages/hairline/src/slab.ts
index f5ca109..2aebf05 100644
--- a/packages/hairline/src/slab.ts
+++ b/packages/hairline/src/slab.ts
@@ -1,4 +1,4 @@
-import { hull, open, poly, ringAt, rrect, run, type PrismPaths, type Projector, type Ring, type Sample } from "./core/iso";
+import { hull, open, poly, ringAt, rrect, run, type Camera, type PrismPaths, type Projector, type Ring, type Sample } from "./core/iso";
/**
* Slabs: the tiles, plates and frames of the graph figures, drawn whole. Not
@@ -24,6 +24,41 @@ export function slab(
};
}
+/**
+ * A ring set d inside another, the gap measured on the screen, so it reads even all the way round, corners
+ * included. Concentric in plan is not enough: the 2:1 view squeezes the top and bottom corners, which pinches
+ * the gap there, and stretches the side corners, which opens it. So each sample goes to the view plane with
+ * its normal, moves d + m in, and stays only if it is that far inside every tangent; then it grows back m, and
+ * where the cut took a corner out the ring turns round it at radius m. Then back to plan, to ride any height.
+ */
+export function inset(C: Camera, ring: readonly Sample[], d: number, m = 1): Ring {
+ const c = Math.cos(C.az), s = Math.sin(C.az), k = C.k, e = d + m, out: Ring = [];
+ const put = (x: number, y: number, a: number, b: number) => {
+ const nu = c * a + k * s * b, nv = k * c * b - s * a, l = Math.hypot(nu, nv);
+ out.push({ u: x * c + (y / k) * s, v: (y / k) * c - x * s, nu: nu / l, nv: nv / l });
+ };
+ // the view plane: the normal goes by the inverse transpose, so it stays square to the edge as drawn
+ const v = ring.map((q) => {
+ const a = q.nu * c - q.nv * s, b = (q.nu * s + q.nv * c) / k, l = Math.hypot(a, b);
+ return [q.u * c - q.v * s, (q.u * s + q.v * c) * k, a / l, b / l];
+ });
+ const w = v
+ .map(([x, y, a, b]) => [x - e * a, y - e * b, a, b])
+ .filter(([x, y]) => v.every(([X, Y, a, b]) => (x - X) * a + (y - Y) * b < 1e-6 - e));
+ w.forEach(([x, y, a, b], i) => {
+ const [X, Y, A, B] = w[(i + 1) % w.length], t = Math.atan2(a * B - b * A, a * A + b * B);
+ put(x + m * a, y + m * b, a, b);
+ if (Math.abs(t) < 0.1) return;
+ // the two edges meet at (px, py): round it
+ const h = x * a + y * b, H = X * A + Y * B, D = a * B - b * A, px = (h * B - H * b) / D, py = (a * H - A * h) / D;
+ for (let j = 0, n = Math.ceil(Math.abs(t) / 0.2); j <= n; j++) {
+ const r = Math.atan2(b, a) + (t * j) / n, ca = Math.cos(r), sa = Math.sin(r);
+ put(px + m * ca, py + m * sa, ca, sa);
+ }
+ });
+ return out;
+}
+
/** A rounded footprint and its lip, inset by b, sampled finely enough for a large tile: eight samples per corner. */
export const lid = (x0: number, y0: number, x1: number, y1: number, r: number, b: number): [Ring, Ring] => [
rrect(x0, y0, x1, y1, r, 8),