diff --git a/CHANGELOG.md b/CHANGELOG.md index 8770656..3ac9ad1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,9 @@ ### Fixed +- **Fixed: `direction` reached the state but not the CSS-rendered field.** Setting a direction updated the page and the copied snippet, and the field kept running the old way. The cause was not state management: a cell's traversal offset is written into its **inline style** when the CSS host is built, and `applyCssVars` only ever wrote the host's own variables — size, grid tracks, fill — so nothing could re-rank the cells. Direction was the first option to change the RANKING rather than a size, which is why the silhouette and the dials reached the CSS renderer and this did not. Changing `--botui-o` in place fixes it and, unlike replacing the DOM, does not restart the animations. `applyCssVars` now also reports whether the host still holds the cells these options call for, because a different lattice or silhouette is not patchable and was silently left stale for the same reason. +- **The test that should have caught it did not exist.** Every direction test forced the SVG renderer, where the field is a pure function of `(options, t)` and repaints every frame — so the SVG path passed while the site's **default** renderer stayed broken. There is now a test that stays on CSS, plus one that walks the live component rather than building a fresh host each time. The address a cell is matched by is now `data-cell` rather than a read-back of `grid-area`: the shorthand round-trips through the CSS parser, and the `grid-row-start` longhands it decomposes into are not implemented everywhere jsdom runs, which made the sync check report "rebuild" on every single update in a test DOM. + - **`chasing` had no gradient — it was three shelves, not a wave.** `chase` carried `steps: 3`, so the whole motion had exactly three brightness values (0.16, 0.58, 1.00) and every cell crossing a shelf jumped. Measured across a cycle, `chase` had **3** distinct brightness levels where `comet` had **78**. With `spread: 0.34` around a ring, several cells crossed a shelf in the same frame, which is what read as unsmooth. It is now a stop table with a fast rise and a six-stop decay: **173** levels, and the largest step between neighbouring samples fell from ~0.42 to **0.03**. The trailing lit tail behind the crest is new, and `chasing` now reads like the comet it always looked like it should be. - The `steps` mechanism is gone with it: the `steps?: number` field on `Envelope`, the `level()` branch that read it, the `data-timing="steps-N"` switch in `field.ts`, and the `steps(3, end)` rule in the stylesheet. `chase` was the only envelope that used it, so all four were a description of a deleted behaviour — the exact kind of dead code this repo's own rules call out. Every envelope is one shape again: a stop table or a gaussian. diff --git a/CHANGELOG.zh.md b/CHANGELOG.zh.md index 859459c..ea37058 100644 --- a/CHANGELOG.zh.md +++ b/CHANGELOG.zh.md @@ -51,6 +51,9 @@ ### 修复 +- **修复:`direction` 到达了状态,却到不了 CSS 渲染的场。** 改了方向后页面和复制片段都更新了,但场仍在按旧方式运行。原因不是状态管理:每一格的遍历偏移在构建 CSS host 时就写进了它的 **inline style**,而 `applyCssVars` 只写 host 自身的变量 —— 尺寸、grid 轨道、fill —— 没有任何东西能重新排名这些格子。方向是第一个改变**排名**而非尺寸的选项,所以剪影和各旋钮能到达 CSS 渲染器而它不能。就地改写 `--botui-o` 修好了这一点,而且不同于替换 DOM,它不会重启动画。`applyCssVars` 现在还会报告 host 是否仍持有这些选项所要求的格子,因为换格阵或换剪影无法就地修补,出于同样的原因此前也被静默地留在过期状态。 +- **本该抓住它的测试并不存在。** 所有方向测试都强制走 SVG 渲染器,而那里 field 是 `(options, t)` 的纯函数、每帧重绘 —— 于是 SVG 路径通过,而站点的**默认**渲染器一直是坏的。现在有一条测试留在 CSS 上,还有一条走真实组件而不是每次新建 host。格子匹配所用的地址改用 `data-cell`,而不是回读 `grid-area`:这个简写要经过 CSS 解析器往返,而它拆分出的 `grid-row-start` 长写并非在所有运行 jsdom 的环境都实现,这会让同步检查在测试 DOM 里每次更新都报"需要重建"。 + - **`chasing` 没有渐变 —— 它是三级台阶,不是波。** `chase` 带着 `steps: 3`,所以整个动效只有三个亮度值(0.16、0.58、1.00),每个格子跨过台阶都是跳变。实测一个周期内 `chase` 只有 **3** 个亮度层,而 `comet` 有 **78** 层。环上 `spread: 0.34` 时,同一帧里有多格跨过同一个台阶 —— 这就是它读起来不 smoother 的原因。现在是一个"快升 + 六段衰减"的 stop 表:**173** 层,相邻采样最大跳变从 ~0.42 降到 **0.03**。波峰后面那道仍然亮着的拖尾是新增的,`chasing` 现在终于读起来像它一直该像的 comet。 - `steps` 机制随之移除:`Envelope` 上的 `steps?: number` 字段、读它的 `level()` 分支、`field.ts` 里的 `data-timing="steps-N"` 切换,以及样式表里的 `steps(3, end)` 规则。`chase` 是唯一的使用者,所以这四处都在描述一个已删除的行为 —— 正是仓库自己的规则点名的死代码。现在每个 envelope 只有一种形态:stop 表或高斯。 diff --git a/apps/site/test/playground.test.tsx b/apps/site/test/playground.test.tsx index aa9bd53..0e27327 100644 --- a/apps/site/test/playground.test.tsx +++ b/apps/site/test/playground.test.tsx @@ -948,6 +948,22 @@ describe("direction", () => { expect(selected[0]!.value, "and it is the forward end").toBe("outsideIn"); }); + it("changes the CSS-rendered field, which is the one the site opens on", () => { + // The regression this file missed once: the direction tests forced the SVG renderer, + // where the field is a pure function of (options, t) and repaints every frame. The CSS + // renderer bakes each cell's traversal offset into its inline style when the host is + // BUILT, so a direction change reached the state and the copied snippet and left the + // field running the old way. This one stays on CSS on purpose. + const order = () => + [...container.querySelectorAll(".playground-stage [style*='--botui-o']")].map( + (c) => c.style.getPropertyValue("--botui-o"), + ); + const before = order(); + expect(before.length, "the field is CSS-rendered").toBeGreaterThan(0); + pick([...dirGroups()[1]!.querySelectorAll(".card")][1]!.querySelector("input")!); + expect(order(), "the cells were re-ranked").not.toEqual(before); + }); + it("changes the DRAWN field, not just a label", () => { useSvgRenderer(); const before = container.querySelector(".playground-stage svg")?.innerHTML; diff --git a/packages/core/src/dot-matrix/field.ts b/packages/core/src/dot-matrix/field.ts index e09430c..961d6a8 100644 --- a/packages/core/src/dot-matrix/field.ts +++ b/packages/core/src/dot-matrix/field.ts @@ -141,8 +141,28 @@ export function buildSvg(options: DotMatrixOptions, t: number): SVGSVGElement { return svg; } -/** push the resolved layout and motion onto a CSS host as custom properties */ -export function applyCssVars(host: HTMLElement, resolved: ResolvedField): void { +/** + * Push the resolved layout and motion onto a CSS host as custom properties. + * + * Returns whether the host's EXISTING cells are the right ones. False means the lattice + * itself changed — different `cols`/`rows`, or a different `silhouette`, so the cells have + * to be built again — and the caller must rebuild rather than patch. + * + * The per-cell traversal `--botui-o` is re-ranked HERE, in place, and that is the whole + * reason this function touches the cells rather than only the host. A cell's phase offset + * is baked into its inline style when the host is built, so an option that changes the + * RANKING — `direction`, or anything that renumbers the traversal — reaches the CSS + * renderer only by rewriting that one property. `direction` was added and appeared to do + * nothing on the site's default renderer: the playground's state updated, the copied + * snippet said `direction={["counterClockwise"]}`, and the field ran the old way, because + * the host-level variables are a small fixed set and the traversal was not among them. + * + * Rewriting the property does NOT restart the animation — `animation-delay` is recomputed + * and the running effect re-phases — so a direction change shifts the wave rather than + * stuttering it. Replacing the DOM would restart every animation, which is why this is a + * patch and not a rebuild. + */ +export function applyCssVars(host: HTMLElement, resolved: ResolvedField): boolean { const { options: o, env, spread, glyph, layout: L } = resolved; // the SAME layout numbers the SVG renderer used — in px, from the explicit // `size`, never from a measurement @@ -176,6 +196,33 @@ export function applyCssVars(host: HTMLElement, resolved: ResolvedField): void { host.style.setProperty("--botui-s", (1 + o.grow * 0.9).toFixed(3)); host.style.color = o.color; host.dataset.timing = "linear"; + return rankCells(host, resolved); +} + +/** + * Re-rank the host's cells against the current traversal, in place. + * + * The cells are addressed by their own `grid-area`, so this also VERIFIES that the host + * still holds the cells these options call for. Rebuilding is reserved for the case where + * they are not: appending to a host whose cells belong to another lattice leaves a field + * that is the right size and the wrong dots, which is worse than either renderer. + */ +function rankCells(host: HTMLElement, resolved: ResolvedField): boolean { + const { options: o, order } = resolved; + const want = cellsFor(o.cols, o.rows, o.silhouette); + const cells = host.children; + if (cells.length !== want.length) return false; + for (let i = 0; i < want.length; i++) { + const c = want[i]!; + const el = cells[i] as HTMLElement; + // The cell's own address, not a read-back of `grid-area`. The shorthand round-trips + // through the CSS parser, and the `grid-row-start` longhands it would decompose into + // are not implemented everywhere jsdom is used — so deriving the address back out of + // the style made this sync check report "rebuild" on every update in a test DOM. + if (el.dataset.cell !== `${c.row},${c.col}`) return false; + el.style.setProperty("--botui-o", order(c.col, c.row, o.cols, o.rows, o.direction).toFixed(3)); + } + return true; } /** @@ -205,6 +252,9 @@ export function buildCss(options: DotMatrixOptions): HTMLElement | null { // corner instead of a ring. Naming the cell keeps the holes as holes, which is // what makes the CSS layout match the SVG one. dot.style.gridArea = `${c.row + 1} / ${c.col + 1}`; + // the cell's own address, so a later update can verify it still holds this cell and + // re-rank it in place rather than rebuilding the host + dot.dataset.cell = `${c.row},${c.col}`; // the raw 0…1 traversal position, kept for the reduced-motion static ramp dot.style.setProperty("--botui-o", order(c.col, c.row, o.cols, o.rows, o.direction).toFixed(3)); host.appendChild(dot); diff --git a/packages/core/src/dot-matrix/matrix.ts b/packages/core/src/dot-matrix/matrix.ts index 71b7c48..0b0a327 100644 --- a/packages/core/src/dot-matrix/matrix.ts +++ b/packages/core/src/dot-matrix/matrix.ts @@ -68,8 +68,14 @@ export function createDotMatrix( if (!built) return; element.replaceChildren(built); host = built; - } else { - applyCssVars(host, resolved); + } else if (!applyCssVars(host, resolved)) { + // The cells are not the ones these options call for — a different lattice or a + // different silhouette. `applyCssVars` re-ranked what it could and said no; the + // field is only correct once the cells themselves are rebuilt. + const built = buildCss(live); + if (!built) return; + element.replaceChildren(built); + host = built; } if (reduced()) host.dataset.reduced = "true"; else delete host.dataset.reduced; diff --git a/packages/core/test/dom.test.ts b/packages/core/test/dom.test.ts index 19b927d..28a0946 100644 --- a/packages/core/test/dom.test.ts +++ b/packages/core/test/dom.test.ts @@ -4,6 +4,8 @@ import { ENVELOPES, PRESETS, PRESET_KEYS, + ORDERS, + applyCssVars, buildCss, buildSvg, createDotMatrix, @@ -12,6 +14,7 @@ import { keyframesFor, layout, maskFor, + resolveField, resolveOptions, softLevel, stylesheet, @@ -137,6 +140,50 @@ describe("the CSS renderer", () => { expect(areas).not.toContain("1 / 1"); // a corner, which a circle drops }); + it("re-ranks the cells IN PLACE when the direction changes on a live host", () => { + // This is the regression test for the bug the feature shipped with: direction reached + // the state and the copied snippet but not the field, because `applyCssVars` wrote + // only the host's own variables and each cell's traversal offset had been baked into + // its inline style when the host was BUILT. The earlier direction test called + // `buildCss` fresh each time, so it passed while the live path stayed broken. + const host = buildCss({ ...base, renderer: "css", direction: [] })!; + const before = [...host.children].map((c) => (c as HTMLElement).style.gridArea); + const nodeAt = (i: number) => host.children[i]; + + // the same host, updated the way a component update does it + expect( + applyCssVars( + host, + resolveField({ ...base, renderer: "css", direction: ["counterClockwise"] })!, + ), + "still in sync", + ).toBe(true); + + const after = [...host.children].map((c) => (c as HTMLElement).style.gridArea); + expect(after, "the same DOM nodes, not a rebuild").toEqual(before); + expect(nodeAt(0), "the leading cell really moved").toBe(host.children[0]); + // and the ranking changed + const o = [...host.children].map((c) => + Number((c as HTMLElement).style.getPropertyValue("--botui-o")), + ); + const cw = ORDERS.spiral!(0, 0, 5, 5, []); + expect(o[0]).not.toBeCloseTo(cw, 2); + }); + + it("reports the host out of sync when the LATTICE changed, so the cells get rebuilt", () => { + // A circle on 7×7 keeps 29 cells and a square keeps 49, so appending to a host built + // for the other would give a field the right size and the wrong dots. Rebuilding is + // the only correct answer, and it has to be the CALLER's answer — hence the boolean. + const host = buildCss({ ...base, cols: 7, rows: 7, silhouette: "circle", renderer: "css" })!; + expect( + applyCssVars( + host, + resolveField({ ...base, cols: 7, rows: 7, silhouette: "square", renderer: "css" })!, + ), + "a different cell set is not patchable", + ).toBe(false); + }); + it("re-ranks the per-cell order when the direction changes, with no CSS of its own", () => { // The claim direction was built on: the CSS renderer hands the motion to the browser // via `--botui-o` and a negative `animation-delay`, so re-ranking the order is all a