Isometric line figures that answer the pointer. For React and for anything with a DOM.
Riffle: no card names and no drawn hit bands; a card is read out by its number
Lucas Marquesfc5b7fb83152
diff --git a/packages/hairline/src/core/styles.ts b/packages/hairline/src/core/styles.tsindex 8ff5aa9..c1639a2 100644--- a/packages/hairline/src/core/styles.ts+++ b/packages/hairline/src/core/styles.ts@@ -47,10 +47,6 @@ export function css(lightDark: boolean): string {`${SVG} :where(.dot.m){fill:var(--hl-edge)}`,`${SVG} :where(.dot.off){fill:var(--hl-lo)}`,`${SVG} :where(.ghost path){fill:none;stroke:var(--hl-mid)}`,- `${SVG} :where(.bands){opacity:0;transition:opacity 240ms ${EASE};pointer-events:none}`,- `${SVG} :where(.bands.show){opacity:1}`,- `${SVG} :where(.bands path){fill:none;stroke:var(--hl-mid);stroke-dasharray:2 3}`,- `${SVG} :where(.bands path.on){stroke:var(--hl-hi);stroke-dasharray:none}`,].join("");}diff --git a/packages/hairline/src/figures/riffle.ts b/packages/hairline/src/figures/riffle.tsindex 37af295..5e3b7db 100644--- a/packages/hairline/src/figures/riffle.ts+++ b/packages/hairline/src/figures/riffle.ts@@ -1,4 +1,4 @@-import { clamp, poly, rad, type Vec2 } from "../core/iso";+import { clamp, rad, type Vec2 } from "../core/iso";import { tdone, tset, tval, tween, type Tween } from "../core/motion";import { BACK, FWD, G, H, LIFT, N, REST, W, card, pose, scene, tray } from "./riffle-geometry";import { disposer, mk, place, pointer, reflect, register, type FigureEls, type FigureHandle } from "../core/stage";@@ -10,21 +10,14 @@ import { disposer, mk, place, pointer, reflect, register, type FigureEls, type F** Selection never reads the posed cards: it comes from static oblique bands* along the resting top edges, so a card moving out from under the pointer- * can't flip the choice back and forth. `bands(true)` shows them. The stage- * is a focusable group; the arrow keys walk the cards and the read-out names- * the one pulled: its number, and its name when `labels` gave it one.+ * can't flip the choice back and forth. The bands are geometry only; nothing+ * draws them. The stage is a focusable group; the arrow keys walk the cards+ * and the read-out names the one pulled by its number.** The drawing itself — camera, tray, a card in any pose — is pure and lives in* riffle-geometry.ts; this file is the DOM, the tweens and the input.*/-export type RiffleHandle = FigureHandle & {- /** Shows or hides the hit bands. */- bands(on: boolean): void;- /** Names for the cards, card 01 first. A card without a name is read out by its number alone. */- labels(list: readonly string[]): void;-};-type Card = {n: number; t0: number; shape: Vec2[];back: SVGPathElement; face: SVGPathElement; head: SVGPathElement; rules: SVGPathElement;@@ -33,10 +26,9 @@ type Card = {a: Tween; z: Tween;};-export const mount = ({ stage, svg, read }: FigureEls, value: number): RiffleHandle => {+export const mount = ({ stage, svg, read }: FigureEls, value: number): FigureHandle => {const bag = disposer();let stag = value;- let names: readonly string[] = [];const { P, front, outer, inner } = scene();const paths = tray(P, front, outer, inner);@@ -64,9 +56,6 @@ export const mount = ({ stage, svg, read }: FigureEls, value: number): RiffleHanconst c0 = top(0), c1 = top(1), d = [c1[0] - c0[0], c1[1] - c0[1]];const px0 = P(0, 0, 0), px1 = P(1, 0, 0), ex = [px1[0] - px0[0], px1[1] - px0[1]];const HALF = W / 2 + 6, det = d[0] * ex[1] - d[1] * ex[0];- const bandG = mk("g", { class: "bands" }, g), bands: SVGPathElement[] = [];- const at = (s: number, r: number): Vec2 => [c0[0] + s * d[0] + r * ex[0], c0[1] + s * d[1] + r * ex[1]];- for (let i = 0; i < N; i++) bands.push(mk("path", { d: poly([at(i - 0.5, -HALF), at(i - 0.5, HALF), at(i + 0.5, HALF), at(i + 0.5, -HALF)]) }, bandG));/** The card whose band holds the point, in the band's own (s, r) coordinates; -1 outside. */function hit([x, y]: Vec2) {@@ -93,11 +82,7 @@ export const mount = ({ stage, svg, read }: FigureEls, value: number): RiffleHanbag.add(B.unregister);let act = -1;- const caption = (a: number) => {- if (a < 0) return "rest";- const n = N - a, name = names[n - 1];- return String(n).padStart(2, "0") + (name ? ` · ${name}` : "");- };+ const caption = (a: number) => (a < 0 ? "rest" : String(N - a).padStart(2, "0"));/** Pulls card a (-1 puts them all back). The stagger spreads out from the card pulled, or the one let go. */function setActive(a: number) {if (a === act) return;@@ -109,7 +94,6 @@ export const mount = ({ stage, svg, read }: FigureEls, value: number): RiffleHantset(cd.a, th, now, delay); tset(cd.z, a === i ? LIFT : 0, now, delay);cd.face.classList.toggle("hi", i === a); cd.head.classList.toggle("hi", i === a); cd.punch[cd.n - 1].classList.toggle("m", i !== a);});- bands.forEach((b, i) => b.classList.toggle("on", i === a));read.textContent = caption(a);B.wake();}@@ -126,8 +110,6 @@ export const mount = ({ stage, svg, read }: FigureEls, value: number): RiffleHanreturn {set: (v) => { stag = v; },- bands: (on) => { bandG.classList.toggle("show", on); },- labels: (list) => { names = list; if (act >= 0) read.textContent = caption(act); },destroy: bag.dispose,};};diff --git a/packages/hairline/test/mount.test.ts b/packages/hairline/test/mount.test.tsindex b16b15b..ea67b96 100644--- a/packages/hairline/test/mount.test.ts+++ b/packages/hairline/test/mount.test.ts@@ -2,6 +2,7 @@import { describe, expect, it, vi } from "vitest";import { frames, host, observers, pending } from "./dom";import { exploded, phosphor, riffle, slow, terrain, turntable } from "../src/index";+import { css } from "../src/core/styles";const ALL = { riffle, terrain, exploded, phosphor, slow, turntable };const key = (el: Element, k: string) => el.dispatchEvent(new KeyboardEvent("keydown", { key: k, bubbles: true, cancelable: true }));@@ -79,6 +80,13 @@ describe("mount", () => {expect(el.querySelector("[data-hairline-live]")).toBeNull();});+ it("draws no hit bands on Riffle, and the stylesheet has no rules for them", () => {+ const el = host();+ riffle(el);+ expect(el.querySelector(".bands")).toBeNull();+ expect(css(true) + css(false)).not.toMatch(/bands/);+ });+it("puts the stylesheet in the document once, however many figures mount", () => {riffle(host()); terrain(host()); slow(host());const sheets = document.adoptedStyleSheets?.length ?? 0;