| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- import { writeFileSync } from "node:fs";
- import { dirname, join } from "node:path";
- import { fileURLToPath } from "node:url";
- import * as k from "../../kit/iso-kit.mjs";
- import { kitScript } from "../../scripts/inline-kit.mjs";
- const HERE = dirname(fileURLToPath(import.meta.url));
- const theme = process.argv.includes("--light") ? "light" : "dark";
- const WIDTH = 600;
- const HEIGHT = 380;
- const PLATE = { x: -128, y: -64, w: 262, d: 128, r: 8 };
- const PLATE_H = 12;
- const FOOT_H = 5;
- const MAG = { x: -112, y: -20, w: 44, d: 40, r: 3 };
- const MAG_H = 24;
- const POST = [-98, 0];
- const POST_TOP = 122;
- const CLAMP_Z = 100;
- const DIAL = [58, 0];
- const DIAL_R = 20;
- const DIAL_Z = 82;
- const DIAL_H = 10;
- const DIAL_TOP = DIAL_Z + DIAL_H;
- const PER_TURN = 20;
- const RAIL_FROM = 2;
- const RAIL_TO = 124;
- const CARRIAGE = { x: -28, y: -24, w: 56, d: 48, r: 3 };
- const CARRIAGE_Z = 3;
- const CARRIAGE_TOP = 8;
- const BLOCKS = [18, 14, 10];
- const BLOCK_W = 15.6;
- const STEP = 16;
- const TIP_R = 2.6;
- const CENTRE = -24 + BLOCK_W / 2;
- const STOPS = BLOCKS.map((_, index) => DIAL[0] - CENTRE - STEP * index);
- const REACH = [STOPS[STOPS.length - 1] - 6, STOPS[0] + 6];
- const P = k.fitProjection(
- [...k.boxCorners(PLATE, -PLATE_H - FOOT_H, 0), [POST[0], POST[1], POST_TOP + 2], [DIAL[0] + DIAL_R, 0, DIAL_TOP]],
- WIDTH,
- HEIGHT,
- { pad: 30, azimuth: 42 },
- );
- const at = (point) => k.iso(point, P);
- const S = (paths, style) => k.solidSvg(paths, style);
- const L = (d, style) => k.lineSvg(d, style);
- const D = (points, style) => k.dotsSvg(points, style);
- const byDepth = (points) => [...points].sort((a, b) => k.depthOf([a[0], a[1], 0], P) - k.depthOf([b[0], b[1], 0], P));
- function plate() {
- const out = [`<path class="iso-halo" d="${k.haloOf(PLATE, -PLATE_H - FOOT_H, PLATE_H, P)}"/>`];
- for (const [x, y] of byDepth(k.corners(PLATE, 22))) {
- out.push(S(k.cylinder(x, y, 7, -PLATE_H - FOOT_H, 1.6, P, 28), { tone: "lo" }));
- out.push(S(k.cylinder(x, y, 5.4, -PLATE_H - FOOT_H + 1.6, FOOT_H - 1.6, P, 28), { tone: "mid" }));
- }
- out.push(S(k.slabOf(PLATE, -PLATE_H, PLATE_H, P, 8, 1.6), { tone: "mid" }));
- out.push(L(k.planOutline(k.insetPlan(PLATE, 5), 0, P), { tone: "lo" }));
- const screws = k.corners(PLATE, 10);
- out.push(L(screws.map(([x, y]) => k.ring(x, y, 2.2, 0, P, 16)).join(""), { tone: "lo" }));
- out.push(D(screws.map(([x, y]) => at([x, y, 0])), { size: 0.5 }));
- const strip = { x: -110, y: PLATE.y + PLATE.d - 15, w: 230, d: 7, r: 1 };
- out.push(S(k.slabOf(strip, 0, 0.6, P, 3), { tone: "lo", crease: "none" }));
- const ticks = k.topTicks(strip.x + 4, strip.x + strip.w - 4, 4, 5, strip.y + 0.8, 0.6, [2, 3.6], P);
- out.push(L(ticks.minor, { tone: "lo" }), L(ticks.major, { tone: "mid" }));
- out.push(D([at([strip.x + 2, strip.y + 3.5, 0.6]), at([strip.x + strip.w - 2, strip.y + 3.5, 0.6])], { size: 0.45 }));
- return out.join("");
- }
- function rails() {
- const out = [];
- for (const y of [-20, 15]) {
- out.push(S(k.slabOf({ x: RAIL_FROM, y, w: RAIL_TO - RAIL_FROM, d: 5, r: 1 }, 0, 3, P, 3, 0.4), { tone: "mid" }));
- out.push(D([RAIL_FROM + 6, RAIL_TO - 6].map((x) => at([x, y + 2.5, 3])), { size: 0.4 }));
- }
- for (const x of [RAIL_FROM - 5, RAIL_TO]) {
- out.push(S(k.slabOf({ x, y: -23, w: 5, d: 46, r: 1.2 }, 0, 7, P, 3, 0.5), { tone: "mid" }));
- out.push(D([at([x + 2.5, -18, 7]), at([x + 2.5, 18, 7])], { size: 0.45 }));
- }
- return out.join("");
- }
- function fixture() {
- const out = [];
- out.push(S(k.slabOf(MAG, 0, 11, P, 6, 0.8), { tone: "mid" }));
- out.push(S(k.slabOf(k.insetPlan(MAG, 0.8), 11, MAG_H - 11, P, 6, 0.8), { tone: "mid" }));
- out.push(L(k.planOutline(k.insetPlan(MAG, 3), MAG_H, P), { tone: "faint" }));
- out.push(D([at([MAG.x + 5, MAG.y + 5, MAG_H]), at([MAG.x + 5, MAG.y + MAG.d - 5, MAG_H]), at([MAG.x + MAG.w - 5, MAG.y + 5, MAG_H])], { size: 0.45 }));
- const knob = [MAG.x + 32, MAG.y + 28];
- out.push(S(k.cylinder(knob[0], knob[1], 6, MAG_H, 4, P, 32), { tone: "mid" }));
- out.push(L(k.knurl(knob[0], knob[1], 6, MAG_H, MAG_H + 4, 36, P), { tone: "lo" }));
- out.push(L(k.lineOnTop([knob[0], knob[1]], [knob[0] + 6, knob[1]], MAG_H + 4, P), { tone: "hi" }));
- out.push(D([at([knob[0], knob[1], MAG_H + 4])], { size: 0.5, tone: "hi" }));
- out.push(S(k.cylinder(POST[0], POST[1], 5, MAG_H, 4, P, 28), { tone: "mid" }));
- const clamp = { x: POST[0] - 7, y: POST[1] - 7, w: 14, d: 14, r: 2.5 };
- out.push(S(k.cylinder(POST[0], POST[1], 3.2, MAG_H + 4, CLAMP_Z - 4 - MAG_H - 4, P, 28), { tone: "mid", crease: "none" }));
- out.push(L([60, 80].map((z) => k.sideArc(POST[0], POST[1], 3.2, z, P, 16)).join(""), { tone: "faint" }));
- out.push(S(k.slabOf(clamp, CLAMP_Z - 4, 12, P, 4, 0.6), { tone: "mid" }));
- out.push(S(k.cylinder(POST[0], POST[1], 3.2, CLAMP_Z + 8, POST_TOP - CLAMP_Z - 8, P, 28), { tone: "mid", crease: "none" }));
- out.push(S(k.cylinder(POST[0], POST[1], 3.8, POST_TOP, 2, P, 28), { tone: "mid" }));
- const arm = { x: POST[0] + 7, y: -3, w: DIAL[0] - DIAL_R - POST[0] - 7 + 2, d: 6, r: 2 };
- out.push(S(k.slabOf(arm, CLAMP_Z, 4, P, 4, 0.5), { tone: "mid" }));
- out.push(D([at([arm.x + 4, 0, CLAMP_Z + 4]), at([arm.x + arm.w - 5, 0, CLAMP_Z + 4])], { size: 0.45 }));
- const thumb = [clamp.x + 7, clamp.y + clamp.d + 3];
- out.push(S(k.slabOf({ x: thumb[0] - 1.4, y: clamp.y + clamp.d, w: 2.8, d: 3, r: 0.8 }, CLAMP_Z + 1, 4, P, 2), { tone: "mid", crease: "none" }));
- out.push(S(k.cylinder(thumb[0], thumb[1] + 1.4, 2.6, CLAMP_Z, 6, P, 20), { tone: "mid" }));
- out.push(L(k.knurl(thumb[0], thumb[1] + 1.4, 2.6, CLAMP_Z, CLAMP_Z + 6, 18, P), { tone: "lo" }));
- return out.join("");
- }
- function carriage() {
- const out = [`<path class="iso-halo" d="${k.haloOf(k.insetPlan(CARRIAGE, 3), CARRIAGE_Z, 0.1, P)}" style="filter:blur(4px)"/>`];
- out.push(S(k.slabOf(CARRIAGE, CARRIAGE_Z, CARRIAGE_TOP - CARRIAGE_Z, P, 5, 0.6), { tone: "mid" }));
- out.push(D([-22, -8, 8, 22].map((x) => at([x, CARRIAGE.y + CARRIAGE.d - 2.6, CARRIAGE_TOP])), { size: 0.4, tone: "lo" }));
- out.push(L(k.lineOnTop([CARRIAGE.x, -15.5], [CARRIAGE.x + CARRIAGE.w, -15.5], CARRIAGE_TOP, P), { tone: "faint" }));
- out.push(L(k.lineOnTop([CARRIAGE.x, 15.5], [CARRIAGE.x + CARRIAGE.w, 15.5], CARRIAGE_TOP, P), { tone: "faint" }));
- BLOCKS.forEach((height, index) => {
- const plan = { x: -24 + STEP * index, y: -12, w: BLOCK_W, d: 24, r: 1 };
- out.push(S(k.slabOf(plan, CARRIAGE_TOP, height, P, 3, 0.5), { tone: "hi", crease: "lo" }));
- out.push(L(k.planOutline(k.insetPlan(plan, 2), CARRIAGE_TOP + height, P, 3), { tone: "faint" }));
- });
- const handle = [CARRIAGE.x + CARRIAGE.w - 8, CARRIAGE.y + CARRIAGE.d + 3];
- out.push(S(k.slabOf({ x: handle[0] - 2, y: CARRIAGE.y + CARRIAGE.d - 1, w: 4, d: 4, r: 1 }, CARRIAGE_Z + 1, 3, P, 2), { tone: "mid", crease: "none" }));
- out.push(S(k.cylinder(handle[0], handle[1] + 1, 3, CARRIAGE_Z, 5, P, 20), { tone: "mid" }));
- out.push(L(k.knurl(handle[0], handle[1] + 1, 3, CARRIAGE_Z, CARRIAGE_Z + 5, 18, P), { tone: "lo" }));
- return out.join("");
- }
- function plunger() {
- const out = [];
- out.push(S(k.cylinder(DIAL[0], DIAL[1], TIP_R, 0, 2.4, P, 20), { tone: "hi", lit: true }));
- out.push(S(k.cylinder(DIAL[0], DIAL[1], 1.4, 2.4, 50, P, 16), { tone: "hi", crease: "none", lit: true }));
- return out.join("");
- }
- function dial() {
- const out = [];
- const [x, y] = DIAL;
- out.push(S(k.cylinder(x, y, 2.9, 54, DIAL_Z - 54, P, 20), { tone: "mid" }));
- out.push(L(k.sideArc(x, y, 2.9, 60, P, 12), { tone: "lo" }));
- out.push(S(k.slabOf({ x: x - DIAL_R - 6, y: -4, w: 8, d: 8, r: 1.5 }, DIAL_Z + 2, CLAMP_Z - DIAL_Z - 2, P, 3, 0.4), { tone: "mid" }));
- out.push(S(k.cylinder(x, y, DIAL_R, DIAL_Z, DIAL_H, P, 64, 0.8), { tone: "hi", crease: "lo" }));
- out.push(L(k.knurl(x, y, DIAL_R, DIAL_TOP - 3.4, DIAL_TOP, 84, P, 0.3), { tone: "lo" }));
- out.push(L(k.sideArc(x, y, DIAL_R, DIAL_TOP - 3.4, P, 40), { tone: "faint" }));
- out.push(L(k.ring(x, y, DIAL_R - 2.2, DIAL_TOP, P, 64), { tone: "lo" }));
- const ticks = k.radialTicks(x, y, DIAL_R - 3, 50, 5, DIAL_TOP, [1.3, 2.8], P);
- out.push(L(ticks.minor, { tone: "lo" }), L(ticks.major, { tone: "mid" }));
- out.push(S(k.slabOf({ x: x + DIAL_R - 1, y: -2.4, w: 5, d: 4.8, r: 1.2 }, DIAL_Z + 2, 5, P, 3), { tone: "mid" }));
- out.push(S(k.slabOf({ x: x + DIAL_R + 3.6, y: -3.2, w: 2.6, d: 6.4, r: 1.2 }, DIAL_Z + 1.2, 6.6, P, 3), { tone: "mid" }));
- out.push(L(k.ring(x, y, 1.6, DIAL_TOP, P, 16), { tone: "mid" }));
- out.push(k.faceTextSvg(k.topMatrix([x, y + 9], DIAL_TOP, P, "x"), "0.01", { size: 2.4, tone: "lo", anchor: "middle" }));
- return out.join("");
- }
- const needleAt = (turn) => {
- const angle = turn * Math.PI * 2;
- const [x, y] = DIAL;
- return k.lineOnTop([x - Math.sin(angle) * 3.4, y + Math.cos(angle) * 3.4], [x + Math.sin(angle) * (DIAL_R - 4), y - Math.cos(angle) * (DIAL_R - 4)], DIAL_TOP + 0.2, P);
- };
- const startX = STOPS[0];
- const svg = k.figureSvg({
- width: WIDTH,
- height: HEIGHT,
- label:
- "A dial indicator on a surface plate. A magnetic base holds a post and an arm; the arm carries a dial whose plunger rests on three gauge blocks, 18, 14 and 10 tall, on a carriage that slides along two rails. Wherever the carriage stops, the plunger rises to the block under it and the needle turns by the same amount.",
- body: [
- plate(),
- rails(),
- fixture(),
- `<g data-part="carriage" transform="${k.translateAlong(P, [startX, 0, 0])}">${carriage()}</g>`,
- `<g data-part="plunger" transform="${k.translateAlong(P, [0, 0, CARRIAGE_TOP + BLOCKS[0]])}">${plunger()}</g>`,
- dial(),
- `<path data-part="needle" class="iso-line" data-tone="lit" data-free="start" d="${needleAt(BLOCKS[0] / PER_TURN)}" style="stroke-width:1.1"/>`,
- ],
- });
- const live = `
- const P = ${JSON.stringify(P)};
- const BLOCKS = ${JSON.stringify(BLOCKS)};
- const DIAL = ${JSON.stringify(DIAL)};
- const DIAL_R = ${DIAL_R}, DIAL_TOP = ${DIAL_TOP}, PER_TURN = ${PER_TURN}, STEP = ${STEP}, BLOCK_W = ${BLOCK_W}, TIP_R = ${TIP_R}, BASE = ${CARRIAGE_TOP};
- const REACH = ${JSON.stringify(REACH)};
- const stage = document.querySelector(".iso-stage");
- const svg = stage.querySelector("svg");
- const carriage = svg.querySelector('[data-part="carriage"]');
- const plunger = svg.querySelector('[data-part="plunger"]');
- const needle = svg.querySelector('[data-part="needle"]');
- const readout = document.querySelector("[data-readout]");
- const still = matchMedia("(prefers-reduced-motion: reduce)");
- const stops = ${JSON.stringify(STOPS)};
- const clamp = (value, low, high) => Math.min(high, Math.max(low, value));
- const heightUnder = (cx) => {
- let top = BASE;
- BLOCKS.forEach((height, index) => {
- const from = cx - 24 + STEP * index, to = from + BLOCK_W;
- if (to > DIAL[0] - TIP_R && from < DIAL[0] + TIP_R) top = Math.max(top, BASE + height);
- });
- return top;
- };
- const blockUnder = (cx) => BLOCKS.findIndex((_, index) => { const from = cx - 24 + STEP * index; return DIAL[0] >= from && DIAL[0] <= from + BLOCK_W; });
- const needleAt = ${needleAt.toString().replace("k.lineOnTop", "lineOnTop")};
- let x = ${startX}, target = x, tip = { at: BASE + BLOCKS[0], rate: 0 };
- let frame = 0, last = 0, clock = 0, touring = true, visible = false, idleTimer = 0;
- const tourAt = (t) => stops[Math.floor(t / 2.2) % stops.length];
- function draw() {
- carriage.setAttribute("transform", translateAlong(P, [x, 0, 0]));
- plunger.setAttribute("transform", translateAlong(P, [0, 0, tip.at]));
- needle.setAttribute("d", needleAt((tip.at - BASE) / PER_TURN));
- const index = blockUnder(x);
- const text = (index < 0 ? "between blocks" : "block " + (index + 1)) + " · reads " + (tip.at - BASE).toFixed(1) + " px";
- if (readout.textContent !== text) readout.textContent = text;
- stage.setAttribute("aria-valuenow", String(Math.round(tip.at - BASE)));
- stage.setAttribute("aria-valuetext", text);
- }
- function tick(now) {
- const dt = last ? Math.min((now - last) / 1000, 1 / 30) : 1 / 60;
- last = now;
- const calm = still.matches;
- if (touring) { clock += dt; target = tourAt(clock); }
- x = calm ? target : x + (target - x) * (1 - Math.exp(-dt / 0.28));
- if (Math.abs(target - x) < 0.01) x = target;
- const want = heightUnder(x);
- const stiff = 420, drag = calm ? 2 * Math.sqrt(420) : 26;
- for (let i = 0; i < 4; i++) { tip.rate += ((want - tip.at) * stiff - tip.rate * drag) * dt / 4; tip.at += tip.rate * dt / 4; }
- if (Math.abs(want - tip.at) < 0.005 && Math.abs(tip.rate) < 0.005) { tip.at = want; tip.rate = 0; }
- draw();
- const settled = !touring && x === target && tip.rate === 0 && tip.at === want;
- frame = visible && !settled ? requestAnimationFrame(tick) : 0;
- }
- const run = () => { if (!frame && visible) { last = 0; frame = requestAnimationFrame(tick); } };
- const steer = (next) => { touring = false; target = clamp(next, REACH[0], REACH[1]); clearTimeout(idleTimer); idleTimer = setTimeout(() => { touring = !still.matches; clock = 0; run(); }, 3600); run(); };
- new IntersectionObserver((entries) => { visible = entries[entries.length - 1].isIntersecting; if (visible) run(); }, { rootMargin: "120px 0px" }).observe(stage);
- if (still.matches) touring = false;
- stage.addEventListener("pointermove", (event) => {
- const box = svg.getBoundingClientRect();
- const share = clamp(((event.clientX - box.left) / box.width - 0.25) / 0.5, 0, 1);
- steer(REACH[0] + share * (REACH[1] - REACH[0]));
- });
- stage.addEventListener("pointerleave", () => steer(target));
- stage.addEventListener("keydown", (event) => {
- const here = stops.reduce((best, stop, index) => (Math.abs(stop - target) < Math.abs(stops[best] - target) ? index : best), 0);
- let to = null;
- if (event.key === "ArrowRight" || event.key === "ArrowUp") to = Math.max(0, here - 1);
- if (event.key === "ArrowLeft" || event.key === "ArrowDown") to = Math.min(stops.length - 1, here + 1);
- if (event.key === "Home") to = 0;
- if (event.key === "End") to = stops.length - 1;
- if (to === null) return;
- event.preventDefault();
- steer(stops[to]);
- });
- draw();
- `;
- const body = k.plateHtml({
- fig: "Fig 1",
- title: "Dial indicator",
- hint: "Hover to slide the carriage",
- readout: `block 1 · reads ${BLOCKS[0].toFixed(1)} px`,
- keys: [
- { mark: "lit", label: "Plunger and needle: what moves when the height changes" },
- { mark: "raised", label: "Three gauge blocks, 18, 14 and 10 tall" },
- ],
- caption:
- "A dial indicator on a surface plate. Slide the carriage and the plunger rides up or down onto the block beneath it; one turn of the needle is 20 px of travel, so every block reads its own height.",
- body: `<div class="iso-stage" tabindex="0" role="slider" aria-label="Dial indicator: arrow keys move the carriage from block to block" aria-valuemin="0" aria-valuemax="18" aria-valuenow="18">${svg}</div>`,
- });
- writeFileSync(join(HERE, theme === "light" ? "dial-indicator-light.html" : "dial-indicator.html"), k.pageHtml({ title: "Dial indicator", theme, body, script: kitScript() + live }));
- console.log("wrote", theme === "light" ? "dial-indicator-light.html" : "dial-indicator.html");
|