build.mjs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import { writeFileSync } from "node:fs";
  2. import { dirname, join } from "node:path";
  3. import { fileURLToPath } from "node:url";
  4. import * as k from "../../kit/iso-kit.mjs";
  5. import { kitScript } from "../../scripts/inline-kit.mjs";
  6. const HERE = dirname(fileURLToPath(import.meta.url));
  7. const theme = process.argv.includes("--light") ? "light" : "dark";
  8. const WIDTH = 600;
  9. const HEIGHT = 380;
  10. const PLATE = { x: -128, y: -64, w: 262, d: 128, r: 8 };
  11. const PLATE_H = 12;
  12. const FOOT_H = 5;
  13. const MAG = { x: -112, y: -20, w: 44, d: 40, r: 3 };
  14. const MAG_H = 24;
  15. const POST = [-98, 0];
  16. const POST_TOP = 122;
  17. const CLAMP_Z = 100;
  18. const DIAL = [58, 0];
  19. const DIAL_R = 20;
  20. const DIAL_Z = 82;
  21. const DIAL_H = 10;
  22. const DIAL_TOP = DIAL_Z + DIAL_H;
  23. const PER_TURN = 20;
  24. const RAIL_FROM = 2;
  25. const RAIL_TO = 124;
  26. const CARRIAGE = { x: -28, y: -24, w: 56, d: 48, r: 3 };
  27. const CARRIAGE_Z = 3;
  28. const CARRIAGE_TOP = 8;
  29. const BLOCKS = [18, 14, 10];
  30. const BLOCK_W = 15.6;
  31. const STEP = 16;
  32. const TIP_R = 2.6;
  33. const CENTRE = -24 + BLOCK_W / 2;
  34. const STOPS = BLOCKS.map((_, index) => DIAL[0] - CENTRE - STEP * index);
  35. const REACH = [STOPS[STOPS.length - 1] - 6, STOPS[0] + 6];
  36. const P = k.fitProjection(
  37. [...k.boxCorners(PLATE, -PLATE_H - FOOT_H, 0), [POST[0], POST[1], POST_TOP + 2], [DIAL[0] + DIAL_R, 0, DIAL_TOP]],
  38. WIDTH,
  39. HEIGHT,
  40. { pad: 30, azimuth: 42 },
  41. );
  42. const at = (point) => k.iso(point, P);
  43. const S = (paths, style) => k.solidSvg(paths, style);
  44. const L = (d, style) => k.lineSvg(d, style);
  45. const D = (points, style) => k.dotsSvg(points, style);
  46. const byDepth = (points) => [...points].sort((a, b) => k.depthOf([a[0], a[1], 0], P) - k.depthOf([b[0], b[1], 0], P));
  47. function plate() {
  48. const out = [`<path class="iso-halo" d="${k.haloOf(PLATE, -PLATE_H - FOOT_H, PLATE_H, P)}"/>`];
  49. for (const [x, y] of byDepth(k.corners(PLATE, 22))) {
  50. out.push(S(k.cylinder(x, y, 7, -PLATE_H - FOOT_H, 1.6, P, 28), { tone: "lo" }));
  51. out.push(S(k.cylinder(x, y, 5.4, -PLATE_H - FOOT_H + 1.6, FOOT_H - 1.6, P, 28), { tone: "mid" }));
  52. }
  53. out.push(S(k.slabOf(PLATE, -PLATE_H, PLATE_H, P, 8, 1.6), { tone: "mid" }));
  54. out.push(L(k.planOutline(k.insetPlan(PLATE, 5), 0, P), { tone: "lo" }));
  55. const screws = k.corners(PLATE, 10);
  56. out.push(L(screws.map(([x, y]) => k.ring(x, y, 2.2, 0, P, 16)).join(""), { tone: "lo" }));
  57. out.push(D(screws.map(([x, y]) => at([x, y, 0])), { size: 0.5 }));
  58. const strip = { x: -110, y: PLATE.y + PLATE.d - 15, w: 230, d: 7, r: 1 };
  59. out.push(S(k.slabOf(strip, 0, 0.6, P, 3), { tone: "lo", crease: "none" }));
  60. const ticks = k.topTicks(strip.x + 4, strip.x + strip.w - 4, 4, 5, strip.y + 0.8, 0.6, [2, 3.6], P);
  61. out.push(L(ticks.minor, { tone: "lo" }), L(ticks.major, { tone: "mid" }));
  62. 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 }));
  63. return out.join("");
  64. }
  65. function rails() {
  66. const out = [];
  67. for (const y of [-20, 15]) {
  68. 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" }));
  69. out.push(D([RAIL_FROM + 6, RAIL_TO - 6].map((x) => at([x, y + 2.5, 3])), { size: 0.4 }));
  70. }
  71. for (const x of [RAIL_FROM - 5, RAIL_TO]) {
  72. out.push(S(k.slabOf({ x, y: -23, w: 5, d: 46, r: 1.2 }, 0, 7, P, 3, 0.5), { tone: "mid" }));
  73. out.push(D([at([x + 2.5, -18, 7]), at([x + 2.5, 18, 7])], { size: 0.45 }));
  74. }
  75. return out.join("");
  76. }
  77. function fixture() {
  78. const out = [];
  79. out.push(S(k.slabOf(MAG, 0, 11, P, 6, 0.8), { tone: "mid" }));
  80. out.push(S(k.slabOf(k.insetPlan(MAG, 0.8), 11, MAG_H - 11, P, 6, 0.8), { tone: "mid" }));
  81. out.push(L(k.planOutline(k.insetPlan(MAG, 3), MAG_H, P), { tone: "faint" }));
  82. 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 }));
  83. const knob = [MAG.x + 32, MAG.y + 28];
  84. out.push(S(k.cylinder(knob[0], knob[1], 6, MAG_H, 4, P, 32), { tone: "mid" }));
  85. out.push(L(k.knurl(knob[0], knob[1], 6, MAG_H, MAG_H + 4, 36, P), { tone: "lo" }));
  86. out.push(L(k.lineOnTop([knob[0], knob[1]], [knob[0] + 6, knob[1]], MAG_H + 4, P), { tone: "hi" }));
  87. out.push(D([at([knob[0], knob[1], MAG_H + 4])], { size: 0.5, tone: "hi" }));
  88. out.push(S(k.cylinder(POST[0], POST[1], 5, MAG_H, 4, P, 28), { tone: "mid" }));
  89. const clamp = { x: POST[0] - 7, y: POST[1] - 7, w: 14, d: 14, r: 2.5 };
  90. 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" }));
  91. out.push(L([60, 80].map((z) => k.sideArc(POST[0], POST[1], 3.2, z, P, 16)).join(""), { tone: "faint" }));
  92. out.push(S(k.slabOf(clamp, CLAMP_Z - 4, 12, P, 4, 0.6), { tone: "mid" }));
  93. 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" }));
  94. out.push(S(k.cylinder(POST[0], POST[1], 3.8, POST_TOP, 2, P, 28), { tone: "mid" }));
  95. const arm = { x: POST[0] + 7, y: -3, w: DIAL[0] - DIAL_R - POST[0] - 7 + 2, d: 6, r: 2 };
  96. out.push(S(k.slabOf(arm, CLAMP_Z, 4, P, 4, 0.5), { tone: "mid" }));
  97. out.push(D([at([arm.x + 4, 0, CLAMP_Z + 4]), at([arm.x + arm.w - 5, 0, CLAMP_Z + 4])], { size: 0.45 }));
  98. const thumb = [clamp.x + 7, clamp.y + clamp.d + 3];
  99. 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" }));
  100. out.push(S(k.cylinder(thumb[0], thumb[1] + 1.4, 2.6, CLAMP_Z, 6, P, 20), { tone: "mid" }));
  101. out.push(L(k.knurl(thumb[0], thumb[1] + 1.4, 2.6, CLAMP_Z, CLAMP_Z + 6, 18, P), { tone: "lo" }));
  102. return out.join("");
  103. }
  104. function carriage() {
  105. const out = [`<path class="iso-halo" d="${k.haloOf(k.insetPlan(CARRIAGE, 3), CARRIAGE_Z, 0.1, P)}" style="filter:blur(4px)"/>`];
  106. out.push(S(k.slabOf(CARRIAGE, CARRIAGE_Z, CARRIAGE_TOP - CARRIAGE_Z, P, 5, 0.6), { tone: "mid" }));
  107. out.push(D([-22, -8, 8, 22].map((x) => at([x, CARRIAGE.y + CARRIAGE.d - 2.6, CARRIAGE_TOP])), { size: 0.4, tone: "lo" }));
  108. out.push(L(k.lineOnTop([CARRIAGE.x, -15.5], [CARRIAGE.x + CARRIAGE.w, -15.5], CARRIAGE_TOP, P), { tone: "faint" }));
  109. out.push(L(k.lineOnTop([CARRIAGE.x, 15.5], [CARRIAGE.x + CARRIAGE.w, 15.5], CARRIAGE_TOP, P), { tone: "faint" }));
  110. BLOCKS.forEach((height, index) => {
  111. const plan = { x: -24 + STEP * index, y: -12, w: BLOCK_W, d: 24, r: 1 };
  112. out.push(S(k.slabOf(plan, CARRIAGE_TOP, height, P, 3, 0.5), { tone: "hi", crease: "lo" }));
  113. out.push(L(k.planOutline(k.insetPlan(plan, 2), CARRIAGE_TOP + height, P, 3), { tone: "faint" }));
  114. });
  115. const handle = [CARRIAGE.x + CARRIAGE.w - 8, CARRIAGE.y + CARRIAGE.d + 3];
  116. 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" }));
  117. out.push(S(k.cylinder(handle[0], handle[1] + 1, 3, CARRIAGE_Z, 5, P, 20), { tone: "mid" }));
  118. out.push(L(k.knurl(handle[0], handle[1] + 1, 3, CARRIAGE_Z, CARRIAGE_Z + 5, 18, P), { tone: "lo" }));
  119. return out.join("");
  120. }
  121. function plunger() {
  122. const out = [];
  123. out.push(S(k.cylinder(DIAL[0], DIAL[1], TIP_R, 0, 2.4, P, 20), { tone: "hi", lit: true }));
  124. out.push(S(k.cylinder(DIAL[0], DIAL[1], 1.4, 2.4, 50, P, 16), { tone: "hi", crease: "none", lit: true }));
  125. return out.join("");
  126. }
  127. function dial() {
  128. const out = [];
  129. const [x, y] = DIAL;
  130. out.push(S(k.cylinder(x, y, 2.9, 54, DIAL_Z - 54, P, 20), { tone: "mid" }));
  131. out.push(L(k.sideArc(x, y, 2.9, 60, P, 12), { tone: "lo" }));
  132. 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" }));
  133. out.push(S(k.cylinder(x, y, DIAL_R, DIAL_Z, DIAL_H, P, 64, 0.8), { tone: "hi", crease: "lo" }));
  134. out.push(L(k.knurl(x, y, DIAL_R, DIAL_TOP - 3.4, DIAL_TOP, 84, P, 0.3), { tone: "lo" }));
  135. out.push(L(k.sideArc(x, y, DIAL_R, DIAL_TOP - 3.4, P, 40), { tone: "faint" }));
  136. out.push(L(k.ring(x, y, DIAL_R - 2.2, DIAL_TOP, P, 64), { tone: "lo" }));
  137. const ticks = k.radialTicks(x, y, DIAL_R - 3, 50, 5, DIAL_TOP, [1.3, 2.8], P);
  138. out.push(L(ticks.minor, { tone: "lo" }), L(ticks.major, { tone: "mid" }));
  139. 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" }));
  140. 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" }));
  141. out.push(L(k.ring(x, y, 1.6, DIAL_TOP, P, 16), { tone: "mid" }));
  142. out.push(k.faceTextSvg(k.topMatrix([x, y + 9], DIAL_TOP, P, "x"), "0.01", { size: 2.4, tone: "lo", anchor: "middle" }));
  143. return out.join("");
  144. }
  145. const needleAt = (turn) => {
  146. const angle = turn * Math.PI * 2;
  147. const [x, y] = DIAL;
  148. 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);
  149. };
  150. const startX = STOPS[0];
  151. const svg = k.figureSvg({
  152. width: WIDTH,
  153. height: HEIGHT,
  154. label:
  155. "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.",
  156. body: [
  157. plate(),
  158. rails(),
  159. fixture(),
  160. `<g data-part="carriage" transform="${k.translateAlong(P, [startX, 0, 0])}">${carriage()}</g>`,
  161. `<g data-part="plunger" transform="${k.translateAlong(P, [0, 0, CARRIAGE_TOP + BLOCKS[0]])}">${plunger()}</g>`,
  162. dial(),
  163. `<path data-part="needle" class="iso-line" data-tone="lit" data-free="start" d="${needleAt(BLOCKS[0] / PER_TURN)}" style="stroke-width:1.1"/>`,
  164. ],
  165. });
  166. const live = `
  167. const P = ${JSON.stringify(P)};
  168. const BLOCKS = ${JSON.stringify(BLOCKS)};
  169. const DIAL = ${JSON.stringify(DIAL)};
  170. 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};
  171. const REACH = ${JSON.stringify(REACH)};
  172. const stage = document.querySelector(".iso-stage");
  173. const svg = stage.querySelector("svg");
  174. const carriage = svg.querySelector('[data-part="carriage"]');
  175. const plunger = svg.querySelector('[data-part="plunger"]');
  176. const needle = svg.querySelector('[data-part="needle"]');
  177. const readout = document.querySelector("[data-readout]");
  178. const still = matchMedia("(prefers-reduced-motion: reduce)");
  179. const stops = ${JSON.stringify(STOPS)};
  180. const clamp = (value, low, high) => Math.min(high, Math.max(low, value));
  181. const heightUnder = (cx) => {
  182. let top = BASE;
  183. BLOCKS.forEach((height, index) => {
  184. const from = cx - 24 + STEP * index, to = from + BLOCK_W;
  185. if (to > DIAL[0] - TIP_R && from < DIAL[0] + TIP_R) top = Math.max(top, BASE + height);
  186. });
  187. return top;
  188. };
  189. const blockUnder = (cx) => BLOCKS.findIndex((_, index) => { const from = cx - 24 + STEP * index; return DIAL[0] >= from && DIAL[0] <= from + BLOCK_W; });
  190. const needleAt = ${needleAt.toString().replace("k.lineOnTop", "lineOnTop")};
  191. let x = ${startX}, target = x, tip = { at: BASE + BLOCKS[0], rate: 0 };
  192. let frame = 0, last = 0, clock = 0, touring = true, visible = false, idleTimer = 0;
  193. const tourAt = (t) => stops[Math.floor(t / 2.2) % stops.length];
  194. function draw() {
  195. carriage.setAttribute("transform", translateAlong(P, [x, 0, 0]));
  196. plunger.setAttribute("transform", translateAlong(P, [0, 0, tip.at]));
  197. needle.setAttribute("d", needleAt((tip.at - BASE) / PER_TURN));
  198. const index = blockUnder(x);
  199. const text = (index < 0 ? "between blocks" : "block " + (index + 1)) + " · reads " + (tip.at - BASE).toFixed(1) + " px";
  200. if (readout.textContent !== text) readout.textContent = text;
  201. stage.setAttribute("aria-valuenow", String(Math.round(tip.at - BASE)));
  202. stage.setAttribute("aria-valuetext", text);
  203. }
  204. function tick(now) {
  205. const dt = last ? Math.min((now - last) / 1000, 1 / 30) : 1 / 60;
  206. last = now;
  207. const calm = still.matches;
  208. if (touring) { clock += dt; target = tourAt(clock); }
  209. x = calm ? target : x + (target - x) * (1 - Math.exp(-dt / 0.28));
  210. if (Math.abs(target - x) < 0.01) x = target;
  211. const want = heightUnder(x);
  212. const stiff = 420, drag = calm ? 2 * Math.sqrt(420) : 26;
  213. for (let i = 0; i < 4; i++) { tip.rate += ((want - tip.at) * stiff - tip.rate * drag) * dt / 4; tip.at += tip.rate * dt / 4; }
  214. if (Math.abs(want - tip.at) < 0.005 && Math.abs(tip.rate) < 0.005) { tip.at = want; tip.rate = 0; }
  215. draw();
  216. const settled = !touring && x === target && tip.rate === 0 && tip.at === want;
  217. frame = visible && !settled ? requestAnimationFrame(tick) : 0;
  218. }
  219. const run = () => { if (!frame && visible) { last = 0; frame = requestAnimationFrame(tick); } };
  220. const steer = (next) => { touring = false; target = clamp(next, REACH[0], REACH[1]); clearTimeout(idleTimer); idleTimer = setTimeout(() => { touring = !still.matches; clock = 0; run(); }, 3600); run(); };
  221. new IntersectionObserver((entries) => { visible = entries[entries.length - 1].isIntersecting; if (visible) run(); }, { rootMargin: "120px 0px" }).observe(stage);
  222. if (still.matches) touring = false;
  223. stage.addEventListener("pointermove", (event) => {
  224. const box = svg.getBoundingClientRect();
  225. const share = clamp(((event.clientX - box.left) / box.width - 0.25) / 0.5, 0, 1);
  226. steer(REACH[0] + share * (REACH[1] - REACH[0]));
  227. });
  228. stage.addEventListener("pointerleave", () => steer(target));
  229. stage.addEventListener("keydown", (event) => {
  230. const here = stops.reduce((best, stop, index) => (Math.abs(stop - target) < Math.abs(stops[best] - target) ? index : best), 0);
  231. let to = null;
  232. if (event.key === "ArrowRight" || event.key === "ArrowUp") to = Math.max(0, here - 1);
  233. if (event.key === "ArrowLeft" || event.key === "ArrowDown") to = Math.min(stops.length - 1, here + 1);
  234. if (event.key === "Home") to = 0;
  235. if (event.key === "End") to = stops.length - 1;
  236. if (to === null) return;
  237. event.preventDefault();
  238. steer(stops[to]);
  239. });
  240. draw();
  241. `;
  242. const body = k.plateHtml({
  243. fig: "Fig 1",
  244. title: "Dial indicator",
  245. hint: "Hover to slide the carriage",
  246. readout: `block 1 · reads ${BLOCKS[0].toFixed(1)} px`,
  247. keys: [
  248. { mark: "lit", label: "Plunger and needle: what moves when the height changes" },
  249. { mark: "raised", label: "Three gauge blocks, 18, 14 and 10 tall" },
  250. ],
  251. caption:
  252. "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.",
  253. 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>`,
  254. });
  255. writeFileSync(join(HERE, theme === "light" ? "dial-indicator-light.html" : "dial-indicator.html"), k.pageHtml({ title: "Dial indicator", theme, body, script: kitScript() + live }));
  256. console.log("wrote", theme === "light" ? "dial-indicator-light.html" : "dial-indicator.html");