diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a83884f..7b22f8c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -52,20 +52,21 @@ These are presentation compositions of the original catalogue SVGs, with unused The installed skill does not depend on the website. These paths are relative to the repository root. -| Responsibility | Source | -| ---------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- | -| Gallery SVG wrapper and role-style opt-in | `components/plotter/blueprint-drawing.tsx` | -| Catalogue compiler and SVG serializer | `scripts/build-plotter-drawings.py`, `scripts/plotter_svg.py` | -| Bundled catalogue SVGs and index | `skills/plotter/references/catalogue/`, generated by `scripts/build-plotter-catalogue.tsx` | -| AGT-01 helper-fragment recipe | `skills/plotter/assets/examples/render-registration.mjs` | -| AGT-01 host file adapter | `scripts/build-plotter-registration.mjs` | -| Static MEC-05 and OBJ-05 | `components/plotter/engineering-studies.tsx` | -| MEC-05 shared core hatching | `components/plotter/production-chain-hatching.tsx` | -| MEC-05 and OBJ-05 timelines | `components/plotter/production-chain-motion-drawing.tsx`, `components/plotter/newtons-cradle-motion-drawing.tsx` | -| Geneva and crank/piston stills and timelines | `components/plotter/geneva-mechanism-motion-drawing.tsx`, `components/plotter/crank-piston-motion-drawing.tsx` | -| Engineering playback, fallback, and visibility | `components/plotter/engineering-motion-preview.tsx` | -| Resolved pilot export adapter | `scripts/export-plotter-pilots.tsx` | -| Download artifact generator | `scripts/build-plotter-downloads.tsx` | +| Responsibility | Source | +| ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ | +| Gallery SVG wrapper and role-style opt-in | `components/plotter/blueprint-drawing.tsx` | +| Catalogue compiler and SVG serializer | `scripts/build-plotter-drawings.py`, `scripts/plotter_svg.py` | +| Bundled catalogue SVGs and index | `skills/plotter/references/catalogue/`, generated by `scripts/build-plotter-catalogue.tsx` | +| AGT-01 helper-fragment recipe | `skills/plotter/assets/examples/render-registration.mjs` | +| AGT-01 host file adapter | `scripts/build-plotter-registration.mjs` | +| Static MEC-05 and OBJ-05 | `components/plotter/engineering-studies.tsx` | +| MEC-05 shared core hatching | `components/plotter/production-chain-hatching.tsx` | +| MEC-05 and OBJ-05 timelines | `components/plotter/production-chain-motion-drawing.tsx`, `components/plotter/newtons-cradle-motion-drawing.tsx` | +| Geneva and crank/piston stills and timelines | `components/plotter/geneva-mechanism-motion-drawing.tsx`, `components/plotter/crank-piston-motion-drawing.tsx` | +| Slotted lever source and gallery adapter | `examples/slotted-lever/`, `scripts/build-plotter-slotted-lever.py`, `components/plotter/slotted-lever-motion.tsx` | +| Engineering playback, fallback, and visibility | `components/plotter/engineering-motion-preview.tsx` | +| Resolved pilot export adapter | `scripts/export-plotter-pilots.tsx` | +| Download artifact generator | `scripts/build-plotter-downloads.tsx` | `BlueprintDrawing` opts into shared roles with `roles`. The independent highlight-opacity token is used by TIM-01, TIM-03, and TIM-05 without changing their stroke-role adoption. The SVG-to-JSX builder preserves supported root styles, removes standalone fixed ink and the root font family so the gallery inherits its theme and loaded font, and gives repeated definitions local IDs. Font weight, optical sizing, and GEOM settings remain authored. Standalone SVGs retain their own font stack. diff --git a/app/animations/page.tsx b/app/animations/page.tsx index 92a7510..b658917 100644 --- a/app/animations/page.tsx +++ b/app/animations/page.tsx @@ -26,6 +26,7 @@ import { ParticlesMotion } from "@/components/plotter/particles-motion"; import { PrimitivesBlueprint } from "@/components/plotter/primitives-blueprint"; import { PrimitivesMotionPreview } from "@/components/plotter/primitives-motion-preview"; import { ProductionChainMotionDrawing } from "@/components/plotter/production-chain-motion-drawing"; +import { SlottedLeverDrawing } from "@/components/plotter/slotted-lever-drawing"; import { PageIntro, SheetBar } from "@/components/sheet"; import { SiteShell } from "@/components/site-shell"; @@ -145,6 +146,18 @@ const studies: { ), }, + { + id: "slotted-lever-animation", + label: "Motion only", + title: "A rotation becomes a return", + description: + "A rotating crank slides a pin through a rocking lever. A short link carries that motion to a carriage, turning each revolution into a journey out and back.", + preview: ( + }> + + + ), + }, ]; export const metadata: Metadata = { title: "Animations" }; diff --git a/biome.json b/biome.json index e28b392..88b7cc2 100644 --- a/biome.json +++ b/biome.json @@ -16,6 +16,7 @@ "!public", "!skills/plotter/references/catalogue", "!components/plotter/expanded-studies.tsx", + "!components/plotter/slotted-lever-drawing.tsx", "!components/plotter/visual-grammar-drawings.tsx", "!skills/plotter/assets/foundation/roles.css", "!skills/plotter/assets/foundation/standard.mjs", diff --git a/components/plotter/slotted-lever-drawing.tsx b/components/plotter/slotted-lever-drawing.tsx new file mode 100644 index 0000000..02b441a --- /dev/null +++ b/components/plotter/slotted-lever-drawing.tsx @@ -0,0 +1,121 @@ +// Generated by scripts/build-plotter-slotted-lever.py from the standalone SVG. +// Edit examples/slotted-lever/drawing.mjs, rebuild its SVG, then regenerate. +import { useId } from "react"; +import { BlueprintDrawing } from "./blueprint-drawing"; +import { SlottedLeverMotion } from "./slotted-lever-motion"; + +export function SlottedLeverDrawing({ animated = false }: { animated?: boolean }) { + const id = useId(); + return ( + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {"Linear guide"} + + + {"Fixed pivot"} + + + + + {"104"} + + + + + ); +} diff --git a/components/plotter/slotted-lever-motion.tsx b/components/plotter/slotted-lever-motion.tsx new file mode 100644 index 0000000..a37f897 --- /dev/null +++ b/components/plotter/slotted-lever-motion.tsx @@ -0,0 +1,61 @@ +import { + geometry, + poseAt, + timeline, +} from "../../examples/slotted-lever/geometry.mjs"; + +const steps = timeline.period * 120; +const number = (value: number) => Number(value.toFixed(6)); +const point = ({ x, y }: { x: number; y: number }) => + `${number(x)} ${number(y)}`; +const frames = Array.from({ length: steps + 1 }, (_, index) => { + const pose = poseAt((index / steps) * timeline.period); + return { + crank: `${number(pose.crankAngle)} ${point(geometry.center)}`, + lever: number(pose.leverAngle), + linkPosition: point(pose.tip), + linkRotation: number(pose.linkAngle), + carriage: point(pose.carriage), + pinX: number(pose.pin.x), + pinY: number(pose.pin.y), + tipX: number(pose.tip.x), + tipY: number(pose.tip.y), + carriageX: number(pose.carriage.x), + }; +}); +type MotionProperty = keyof (typeof frames)[number]; +const values = Object.fromEntries( + Object.keys(frames[0]).map((property) => [ + property, + frames.map((frame) => frame[property as MotionProperty]).join(";"), + ]), +); + +// Sample the standalone equations, retaining the site's native SVG clock. +// Equal intervals and linear interpolation preserve the constant-speed input. +export function SlottedLeverMotion({ + animated, + property, + attributeName, + type, +}: { + animated: boolean; + property: MotionProperty; + attributeName: string; + type?: "rotate" | "translate"; +}) { + if (!animated) return null; + const props = { + attributeName, + begin: "0s", + dur: `${timeline.period}s`, + calcMode: "linear" as const, + repeatCount: "indefinite", + values: values[property], + }; + return type ? ( + + ) : ( + + ); +} diff --git a/examples/slotted-lever/README.md b/examples/slotted-lever/README.md new file mode 100644 index 0000000..1b20989 --- /dev/null +++ b/examples/slotted-lever/README.md @@ -0,0 +1,47 @@ +# A rotation becomes a return + +A standalone Plotter adaptation of the supplied `Untitled.mov`. Open +`slotted-lever.html` directly; it contains the drawing, timeline, player, shared +role stylesheet, and Cal Sans font. No server or website build is needed. + +The rotating crank pin stays on a circular locus and inside the rocking lever’s +slot. A fixed-length connecting link drives a carriage along horizontal rails. +Hatching identifies the output carriage. This is an ideal geometric construction, +not a manufacturing or dynamics model. + +The composition borrows the shared center and generating locus from **Circle, +square, line**, and the constrained output relationship from **An offset becomes +a lift**. It retains the supplied clip’s linkage and three-spoke wheel, with a +six-second constant-speed crank revolution. The return emerges from the linkage; +there are no holds, easing effects, or resets at the cycle boundary. + +- `geometry.mjs`: dimensions and pose calculation, independent of playback. +- `drawing.mjs`: editable SVG construction using the canonical Plotter foundation. +- `player.js`: clock, controls, visibility suspension, and reduced-motion fallback. +- `build.mjs`: self-contained HTML and themeable SVG generation. +- `slotted-lever.svg`: editable themeable source, with embedded role CSS. +- `slotted-lever-export.svg`: resolved static export with embedded Cal Sans. +- `slotted-lever-light.png` and `slotted-lever-dark.png`: still previews. + +Rebuild from the repository root: + +```sh +node examples/slotted-lever/build.mjs +python3 skills/plotter/scripts/check.py examples/slotted-lever/slotted-lever.svg +python3 skills/plotter/scripts/export-svg.py examples/slotted-lever/slotted-lever.svg examples/slotted-lever/slotted-lever-export.svg --embed-font +python3 skills/plotter/scripts/render.py examples/slotted-lever/slotted-lever.svg --theme both +``` + +Inspected the selected still in light and dark at 560 × 440, three additional +quarter-cycle poses, and the HTML at desktop and narrow widths. The SVG checker +and source lint pass. Headless Chromium interaction checks passed for animation, +pause/resume, replay, still selection, scrubbing, and changing reduced-motion +preferences. Source review covers off-screen/document visibility suspension. +A 60,001-pose sweep found a maximum connecting-link length residual of +4.3 × 10⁻¹⁴ units; the carriage stays inside the rails and the crank pin inside +the slot. Full-cycle normal-speed rhythm has not been visually reviewed. + +The website includes this study at `/animations#slotted-lever-animation`. Its +generated React drawing comes from the standalone SVG, while the native SVG +timeline samples the same `geometry.mjs`. Run `bun run drawings` to regenerate +both outputs. The gallery uses the shared playback wrapper without controls. diff --git a/examples/slotted-lever/build.mjs b/examples/slotted-lever/build.mjs new file mode 100644 index 0000000..187ee72 --- /dev/null +++ b/examples/slotted-lever/build.mjs @@ -0,0 +1,47 @@ +import { readFileSync, writeFileSync } from "node:fs"; +import { fileURLToPath } from "node:url"; +import { drawingAt } from "./drawing.mjs"; + +const dir = new URL("./", import.meta.url); +const font = readFileSync( + new URL("../../skills/plotter/assets/fonts/CalSansVF.woff2", dir), +).toString("base64"); +const geometry = readFileSync(new URL("geometry.mjs", dir), "utf8").replaceAll( + "export ", + "", +); +const player = readFileSync(new URL("player.js", dir), "utf8"); +const svg = drawingAt(); +writeFileSync(new URL("slotted-lever.svg", dir), svg); +writeFileSync( + new URL("slotted-lever.html", dir), + ` + +A rotation becomes a return +
+${svg} +
+ +0.0 / 6.0 s +
+ + +
`, +); +console.log(`Wrote ${fileURLToPath(new URL("slotted-lever.html", dir))}`); diff --git a/examples/slotted-lever/drawing.mjs b/examples/slotted-lever/drawing.mjs new file mode 100644 index 0000000..eac789d --- /dev/null +++ b/examples/slotted-lever/drawing.mjs @@ -0,0 +1,121 @@ +import { + dimension, + hatch, + roleCss, +} from "../../skills/plotter/assets/foundation/index.mjs"; +import { geometry as g, poseAt } from "./geometry.mjs"; + +const n = (value) => Number(value.toFixed(6)); +const p = (point) => `${n(point.x)} ${n(point.y)}`; +const translate = (point) => `translate(${p(point)})`; +const centerDistance = g.pivot.y - g.center.y; +const axleOffset = dimension({ + axis: "y", + witnesses: [ + [g.center.x, g.center.y], + [g.pivot.x, g.pivot.y], + ], + position: 134, + extensions: { gap: 12, overshoot: 8 }, + label: { + text: String(centerDistance), + anchor: [134, 309], + clearance: { x: 120, y: 296, width: 28, height: 20 }, + }, +}); +const carriageHatch = hatch({ + region: { + kind: "rect", + x: -g.carriageWidth / 2, + y: -g.carriageHeight / 2, + width: g.carriageWidth, + height: g.carriageHeight, + }, + pattern: "H1", +}); + +export function drawingAt(seconds = 0) { + const pose = poseAt(seconds); + return ` +A rotation becomes a return +A crank pin slides along a lever pivoted below the wheel. The lever rocks, and a short link moves a hatched carriage along two horizontal rails. +Geometric construction adapted from the supplied Untitled.mov. Fixed crank center (250,254), pivot (250,358), crank radius 70, lever length 272, connecting link 80, carriage ordinate 86. Dimensions are SVG units. Ideal planar linkage only; no loads, interference, or manufacture are asserted. Hatching identifies the output carriage. Axes and the crank-pin locus cross transparent geometric forms to explain their constraints. Masks clear joints without painting a background. Reference: Circle, square, line (GEO-01), shared center and generating locus; An offset becomes a lift (MEC-03), rotation driving a constrained output. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Linear guide + Fixed pivot + + + ${centerDistance} + +`; +} diff --git a/examples/slotted-lever/geometry.mjs b/examples/slotted-lever/geometry.mjs new file mode 100644 index 0000000..c6dfe6d --- /dev/null +++ b/examples/slotted-lever/geometry.mjs @@ -0,0 +1,51 @@ +export const geometry = Object.freeze({ + center: { x: 250, y: 254 }, + pivot: { x: 250, y: 358 }, + crankRadius: 70, + wheelRadius: 78, + leverLength: 272, + linkLength: 80, + carriageY: 86, + carriageWidth: 64, + carriageHeight: 36, +}); + +export const timeline = Object.freeze({ + period: 6, + stillTime: 0, + startAngle: 38, +}); + +// A constant-speed crank locates the pin. The lever passes through that pin; +// the short link intersects the carriage's horizontal constraint on its right. +export function poseAt(seconds) { + const g = geometry; + const theta = + (timeline.startAngle * Math.PI) / 180 - + (seconds / timeline.period) * 2 * Math.PI; + const pin = { + x: g.center.x + g.crankRadius * Math.cos(theta), + y: g.center.y + g.crankRadius * Math.sin(theta), + }; + const dx = pin.x - g.pivot.x; + const dy = pin.y - g.pivot.y; + const distance = Math.hypot(dx, dy); + const tip = { + x: g.pivot.x + (g.leverLength * dx) / distance, + y: g.pivot.y + (g.leverLength * dy) / distance, + }; + const rise = g.carriageY - tip.y; + const carriage = { + x: tip.x + Math.sqrt(g.linkLength ** 2 - rise ** 2), + y: g.carriageY, + }; + return { + pin, + tip, + carriage, + distance, + crankAngle: (theta * 180) / Math.PI, + leverAngle: (Math.atan2(dx, -dy) * 180) / Math.PI, + linkAngle: (Math.atan2(rise, carriage.x - tip.x) * 180) / Math.PI, + }; +} diff --git a/examples/slotted-lever/player.js b/examples/slotted-lever/player.js new file mode 100644 index 0000000..33b6861 --- /dev/null +++ b/examples/slotted-lever/player.js @@ -0,0 +1,114 @@ +const svg = document.querySelector("svg"); +const play = document.querySelector("#play"); +const replay = document.querySelector("#replay"); +const still = document.querySelector("#still"); +const scrub = document.querySelector("#scrub"); +const timeLabel = document.querySelector("#time"); +const reducedMotion = matchMedia("(prefers-reduced-motion: reduce)"); +const parts = Object.fromEntries( + [ + "wheel", + "lever", + "link", + "carriage", + "pin", + "tip", + "pin-clear", + "tip-clear", + "carriage-clear", + ].map((name) => [name, document.getElementById(`sl-${name}`)]), +); +let elapsed = timeline.stillTime; +let userPaused = false; +let showingStill = false; +let visible = true; +let frame = null; +let last = null; + +function setPoint(element, point) { + element.setAttribute("cx", point.x); + element.setAttribute("cy", point.y); +} + +function paint(seconds) { + const pose = poseAt(seconds); + parts.wheel.setAttribute( + "transform", + `rotate(${pose.crankAngle} ${geometry.center.x} ${geometry.center.y})`, + ); + parts.lever.setAttribute("transform", `rotate(${pose.leverAngle})`); + parts.link.setAttribute( + "transform", + `translate(${pose.tip.x} ${pose.tip.y}) rotate(${pose.linkAngle})`, + ); + parts.carriage.setAttribute( + "transform", + `translate(${pose.carriage.x} ${pose.carriage.y})`, + ); + for (const name of ["pin", "tip"]) { + setPoint(parts[name], pose[name]); + setPoint(parts[`${name}-clear`], pose[name]); + } + setPoint(parts["carriage-clear"], pose.carriage); + scrub.value = seconds; + scrub.setAttribute( + "aria-valuetext", + `${seconds.toFixed(1)} of ${timeline.period} seconds`, + ); + timeLabel.value = `${seconds.toFixed(1)} / ${timeline.period.toFixed(1)} s`; +} + +function tick(now) { + if (last !== null) + elapsed = (elapsed + (now - last) / 1000) % timeline.period; + last = now; + paint(elapsed); + frame = requestAnimationFrame(tick); +} + +function synchronize() { + if (frame !== null) cancelAnimationFrame(frame); + frame = null; + last = null; + const isStatic = showingStill || reducedMotion.matches; + play.textContent = userPaused || showingStill ? "Play" : "Pause"; + still.setAttribute("aria-pressed", String(showingStill)); + for (const control of [play, replay, still, scrub]) + control.disabled = reducedMotion.matches; + document.querySelector("#motion-note").hidden = !reducedMotion.matches; + paint(isStatic ? timeline.stillTime : elapsed); + if (!isStatic && !userPaused && visible && !document.hidden) { + frame = requestAnimationFrame(tick); + } +} + +play.addEventListener("click", () => { + if (showingStill) { + showingStill = false; + userPaused = false; + } else userPaused = !userPaused; + synchronize(); +}); +replay.addEventListener("click", () => { + elapsed = 0; + userPaused = false; + showingStill = false; + synchronize(); +}); +still.addEventListener("click", () => { + showingStill = !showingStill; + synchronize(); +}); +scrub.addEventListener("input", () => { + elapsed = Number(scrub.value); + userPaused = true; + showingStill = false; + synchronize(); +}); +document.addEventListener("visibilitychange", synchronize); +reducedMotion.addEventListener("change", synchronize); +new IntersectionObserver(([entry]) => { + visible = entry.isIntersecting; + synchronize(); +}).observe(svg); +synchronize(); diff --git a/examples/slotted-lever/slotted-lever-dark.png b/examples/slotted-lever/slotted-lever-dark.png new file mode 100644 index 0000000..4062705 Binary files /dev/null and b/examples/slotted-lever/slotted-lever-dark.png differ diff --git a/examples/slotted-lever/slotted-lever-export.svg b/examples/slotted-lever/slotted-lever-export.svg new file mode 100644 index 0000000..f057663 --- /dev/null +++ b/examples/slotted-lever/slotted-lever-export.svg @@ -0,0 +1,81 @@ + +A rotation becomes a return +A crank pin slides along a lever pivoted below the wheel. The lever rocks, and a short link moves a hatched carriage along two horizontal rails. +Geometric construction adapted from the supplied Untitled.mov. Fixed crank center (250,254), pivot (250,358), crank radius 70, lever length 272, connecting link 80, carriage ordinate 86. Dimensions are SVG units. Ideal planar linkage only; no loads, interference, or manufacture are asserted. Hatching identifies the output carriage. Axes and the crank-pin locus cross transparent geometric forms to explain their constraints. Masks clear joints without painting a background. Reference: Circle, square, line (GEO-01), shared center and generating locus; An offset becomes a lift (MEC-03), rotation driving a constrained output. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Linear guide + Fixed pivot + + + 104 + + diff --git a/examples/slotted-lever/slotted-lever-light.png b/examples/slotted-lever/slotted-lever-light.png new file mode 100644 index 0000000..8c020cf Binary files /dev/null and b/examples/slotted-lever/slotted-lever-light.png differ diff --git a/examples/slotted-lever/slotted-lever.html b/examples/slotted-lever/slotted-lever.html new file mode 100644 index 0000000..75f7301 --- /dev/null +++ b/examples/slotted-lever/slotted-lever.html @@ -0,0 +1,362 @@ + + +A rotation becomes a return +
+ +A rotation becomes a return +A crank pin slides along a lever pivoted below the wheel. The lever rocks, and a short link moves a hatched carriage along two horizontal rails. +Geometric construction adapted from the supplied Untitled.mov. Fixed crank center (250,254), pivot (250,358), crank radius 70, lever length 272, connecting link 80, carriage ordinate 86. Dimensions are SVG units. Ideal planar linkage only; no loads, interference, or manufacture are asserted. Hatching identifies the output carriage. Axes and the crank-pin locus cross transparent geometric forms to explain their constraints. Masks clear joints without painting a background. Reference: Circle, square, line (GEO-01), shared center and generating locus; An offset becomes a lift (MEC-03), rotation driving a constrained output. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Linear guide + Fixed pivot + + + 104 + + +
+ +0.0 / 6.0 s +
+ + +
\ No newline at end of file diff --git a/examples/slotted-lever/slotted-lever.svg b/examples/slotted-lever/slotted-lever.svg new file mode 100644 index 0000000..9637873 --- /dev/null +++ b/examples/slotted-lever/slotted-lever.svg @@ -0,0 +1,169 @@ + +A rotation becomes a return +A crank pin slides along a lever pivoted below the wheel. The lever rocks, and a short link moves a hatched carriage along two horizontal rails. +Geometric construction adapted from the supplied Untitled.mov. Fixed crank center (250,254), pivot (250,358), crank radius 70, lever length 272, connecting link 80, carriage ordinate 86. Dimensions are SVG units. Ideal planar linkage only; no loads, interference, or manufacture are asserted. Hatching identifies the output carriage. Axes and the crank-pin locus cross transparent geometric forms to explain their constraints. Masks clear joints without painting a background. Reference: Circle, square, line (GEO-01), shared center and generating locus; An offset becomes a lift (MEC-03), rotation driving a constrained output. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Linear guide + Fixed pivot + + + 104 + + \ No newline at end of file diff --git a/package.json b/package.json index 0a92399..2c7db1b 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "build": "bun scripts/build-docs.ts && next build", "start": "next start --port 3000", "downloads": "bun scripts/build-plotter-catalogue.tsx && bun scripts/build-plotter-downloads.tsx && bun scripts/build-docs.ts", - "drawings": "node scripts/build-plotter-registration.mjs && python3 scripts/build-plotter-drawings.py", + "drawings": "node scripts/build-plotter-registration.mjs && python3 scripts/build-plotter-drawings.py && node examples/slotted-lever/build.mjs && python3 scripts/build-plotter-slotted-lever.py", "grammar": "python3 scripts/build-plotter-grammar.py", "format": "biome format --write .", "format:check": "biome format .", diff --git a/scripts/build-plotter-slotted-lever.py b/scripts/build-plotter-slotted-lever.py new file mode 100644 index 0000000..695789b --- /dev/null +++ b/scripts/build-plotter-slotted-lever.py @@ -0,0 +1,64 @@ +"""Compile the standalone slotted-lever SVG for the animations gallery.""" + +import re +import xml.etree.ElementTree as ET +from pathlib import Path + +from plotter_svg import element + +ROOT = Path(__file__).resolve().parents[1] +svg = ET.parse(ROOT / "examples/slotted-lever/slotted-lever.svg").getroot() +ids = [node.attrib["id"] for node in svg.iter() if "id" in node.attrib] +nodes = {node.attrib["id"]: node for node in svg.iter() if "id" in node.attrib} + + +def motion(node, property, attribute="transform", kind=None): + attrs = {"animated": "__animated__", "property": property, "attributeName": attribute} + if kind: + attrs["type"] = kind + ET.SubElement(node, "SlottedLeverMotion", attrs) + + +motion(nodes["sl-wheel"], "crank", kind="rotate") +motion(nodes["sl-lever"], "lever", kind="rotate") +motion(nodes["sl-carriage"], "carriage", kind="translate") +# SMIL animates one transform type per node; preserve the source transform order. +link = nodes["sl-link"] +translation, rotation = re.fullmatch(r"(translate\([^)]+\)) (rotate\([^)]+\))", link.attrib["transform"]).groups() +link.attrib["transform"] = translation +turn = ET.Element("g", {"transform": rotation}) +turn.extend(list(link)) +link[:] = [turn] +motion(link, "linkPosition", kind="translate") +motion(turn, "linkRotation", kind="rotate") +for name in ("pin", "tip"): + for suffix in ("", "-clear"): + node = nodes[f"sl-{name}{suffix}"] + motion(node, f"{name}X", "cx") + motion(node, f"{name}Y", "cy") +motion(nodes["sl-carriage-clear"], "carriageX", "cx") + +body = "\n".join( + element(node, ids, 4) for node in svg + if node.attrib.get("data-plotter-styles") != "true" +) +body = body.replace('animated={"__animated__"}', 'animated={animated}') +source = f'''// Generated by scripts/build-plotter-slotted-lever.py from the standalone SVG. +// Edit examples/slotted-lever/drawing.mjs, rebuild its SVG, then regenerate. +import {{ useId }} from "react"; +import {{ BlueprintDrawing }} from "./blueprint-drawing"; +import {{ SlottedLeverMotion }} from "./slotted-lever-motion"; + +export function SlottedLeverDrawing({{ animated = false }}: {{ animated?: boolean }}) {{ + const id = useId(); + return ( + + +{body} + + + ); +}} +''' +(ROOT / "components/plotter/slotted-lever-drawing.tsx").write_text(source) +print("Compiled slotted-lever gallery drawing.")