Skip to content

Isometric line figures that answer the pointer. For React and for anything with a DOM.

Intensity: one number from 0 to 1, mapped onto each figure's own parameter

Lucas Marques448d0e595f1d
diff --git a/packages/hairline/src/intensity.ts b/packages/hairline/src/intensity.ts
new file mode 100644
index 0000000..e77389a
--- /dev/null
+++ b/packages/hairline/src/intensity.ts
@@ -0,0 +1,37 @@
+/**
+ * One option, `intensity`, for every figure. Each figure turns it into the
+ * one number its engine takes: two straight lines that meet at 0.5, where the
+ * number is the figure's default, with the figure's limits at 0 and 1.
+ *
+ * Internal: neither entry exports this. The site keeps a copy of TABLE for its
+ * copy and its tests hold the two together.
+ */
+
+export type FigureId = "riffle" | "terrain" | "exploded" | "phosphor" | "slow" | "turntable";
+
+/** Each figure's number at intensity 0, 0.5 and 1. Slow's falls: a slower clock is a stronger answer. */
+export const TABLE: Record<FigureId, readonly [number, number, number]> = {
+ riffle: [0, 40, 90], // stagger, ms
+ terrain: [1.5, 3, 5], // radius, cells
+ exploded: [12, 28, 40], // gap, viewBox units
+ phosphor: [150, 520, 1500], // afterglow, ms
+ slow: [0.6, 0.2, 0.05], // rate, × normal speed
+ turntable: [200, 650, 1500], // coast, ms
+};
+
+export const DEFAULT = 0.5;
+
+/** An intensity from anything: numeric strings are read, what is not a finite number is the default, the rest is clamped to 0…1. */
+export function intensity(value: unknown): number {
+ const n = typeof value === "string" && value.trim() !== "" ? Number(value) : value;
+ if (typeof n !== "number" || !Number.isFinite(n)) return DEFAULT;
+ return Math.min(1, Math.max(0, n));
+}
+
+/** The figure's own number for an intensity, rounded to three decimals so 0.7 gives Riffle 60 and not 59.99999999999999. */
+export function parameter(figure: FigureId, value: unknown): number {
+ const [lo, mid, hi] = TABLE[figure];
+ const i = intensity(value);
+ const v = i <= 0.5 ? lo + (i / 0.5) * (mid - lo) : mid + ((i - 0.5) / 0.5) * (hi - mid);
+ return Math.round(v * 1000) / 1000;
+}
diff --git a/packages/hairline/test/intensity.test.ts b/packages/hairline/test/intensity.test.ts
new file mode 100644
index 0000000..d299109
--- /dev/null
+++ b/packages/hairline/test/intensity.test.ts
@@ -0,0 +1,55 @@
+import { describe, expect, it } from "vitest";
+import { DEFAULT, TABLE, intensity, parameter, type FigureId } from "../src/intensity";
+import { INTENSITY, OPTION } from "./parity/scripts.mjs";
+
+const IDS = Object.keys(TABLE) as FigureId[];
+
+describe("intensity", () => {
+ it("keeps a number inside 0…1", () => {
+ expect(intensity(0.8)).toBe(0.8);
+ });
+ it("clamps to 0…1", () => {
+ expect(intensity(-3)).toBe(0);
+ expect(intensity(7)).toBe(1);
+ });
+ it("reads a numeric string, as an attribute or a form field would give it", () => {
+ expect(intensity("0.8")).toBe(0.8);
+ expect(intensity(" 7 ")).toBe(1);
+ });
+ it.each([undefined, null, NaN, Infinity, -Infinity, "", " ", "fast", {}, [], true])("falls back to 0.5 for %o", (v) => {
+ expect(intensity(v)).toBe(DEFAULT);
+ expect(DEFAULT).toBe(0.5);
+ });
+});
+
+describe("parameter", () => {
+ it.each(IDS)("gives %s its table's three values at 0, 0.5 and 1", (id) => {
+ expect([0, 0.5, 1].map((i) => parameter(id, i))).toEqual(TABLE[id]);
+ });
+
+ it.each(IDS)("moves %s one way only as intensity rises", (id) => {
+ const values = Array.from({ length: 21 }, (_, k) => parameter(id, k / 20));
+ const falls = id === "slow";
+ for (let k = 1; k < values.length; k++) {
+ if (falls) expect(values[k]).toBeLessThan(values[k - 1]);
+ else expect(values[k]).toBeGreaterThan(values[k - 1]);
+ }
+ });
+
+ it("rounds to three decimals", () => {
+ expect(parameter("riffle", 0.7)).toBe(60);
+ expect(parameter("riffle", 0.333)).toBe(26.64);
+ });
+
+ it("reads the intensity the way intensity() does", () => {
+ expect(parameter("terrain", "0.75")).toBe(4);
+ expect(parameter("terrain", 9)).toBe(5);
+ expect(parameter("terrain", "fast")).toBe(3);
+ });
+
+ /* the parity goldens were captured with raw values; these intensities must land on them exactly */
+ it.each(IDS)("lands %s on the golden's value at its parity intensity", (id) => {
+ const raw = OPTION[id][1];
+ expect(parameter(id, INTENSITY[id])).toBe(raw);
+ });
+});
diff --git a/packages/hairline/test/parity/scripts.mjs b/packages/hairline/test/parity/scripts.mjs
index 4d48239..0ae2e00 100644
--- a/packages/hairline/test/parity/scripts.mjs
+++ b/packages/hairline/test/parity/scripts.mjs
@@ -11,6 +11,12 @@ export const OPTION = {
phosphor: ["afterglow", 900], slow: ["rate", 0.4], turntable: ["coast", 1000],
};
+/** The intensity the package is set to at `set`: each maps exactly onto the value in OPTION (test/intensity.test.ts holds them together). */
+export const INTENSITY = {
+ riffle: 0.7, terrain: 0.75, exploded: 5 / 6,
+ phosphor: 0.5 + 190 / 980, slow: 0.25, turntable: 0.5 + 175 / 850,
+};
+
export const SCRIPTS = {
riffle: [
{ adv: 30 }, { cp: "rest" },