@snowcone-app/loaders 0.3.4 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { naturalSize } from './chunk-6IWUTOBS.js';
1
+ import { naturalSize } from './chunk-33ZDGDZM.js';
2
2
 
3
3
  // src/mask.ts
4
4
  var LEARN_SIDE = 192;
@@ -237,8 +237,78 @@ function rememberMask(key, w, h, bits) {
237
237
  }
238
238
  return next;
239
239
  }
240
+ var PRINT_MASK_FORMAT = "sc-print-mask/1";
241
+ function parsePrintMask(json) {
242
+ const bad = (why) => ({
243
+ state: "error",
244
+ code: "E_LOADER_MASK_INVALID",
245
+ message: `E_LOADER_MASK_INVALID: ${why}. A mask URL must serve an ${PRINT_MASK_FORMAT} file ({ format, w, h, bits }); pass the URL the catalog names (mockups[].printMask), or none.`
246
+ });
247
+ if (!json || typeof json !== "object") return bad("the answer is not a JSON object");
248
+ const f = json;
249
+ if (f.format !== PRINT_MASK_FORMAT) return bad(`format is ${JSON.stringify(f.format)}, not "${PRINT_MASK_FORMAT}"`);
250
+ const w = Number(f.w), h = Number(f.h);
251
+ if (!Number.isInteger(w) || !Number.isInteger(h) || w < 1 || h < 1 || w > 4 * LEARN_SIDE || h > 4 * LEARN_SIDE) {
252
+ return bad(`the grid is ${String(f.w)} x ${String(f.h)}`);
253
+ }
254
+ const bits = typeof f.bits === "string" ? unpackBits(f.bits, w * h) : null;
255
+ if (!bits) return bad(`bits does not hold ${w * h} cells`);
256
+ if (!bits.some((b) => b === 1)) return bad("no cell is set (an empty mask)");
257
+ return { state: "mask", mask: { w, h, bits } };
258
+ }
259
+ var loads = /* @__PURE__ */ new Map();
260
+ var loaded = /* @__PURE__ */ new Map();
261
+ function loadPrintMask(url) {
262
+ const hit = loads.get(url);
263
+ if (hit) return hit;
264
+ const p = (async () => {
265
+ if (typeof fetch !== "function") return { state: "error", code: "E_LOADER_MASK_FETCH", message: "E_LOADER_MASK_FETCH: no fetch here" };
266
+ let res;
267
+ try {
268
+ res = await fetch(url, { credentials: "omit" });
269
+ } catch (e) {
270
+ return {
271
+ state: "error",
272
+ code: "E_LOADER_MASK_FETCH",
273
+ message: `E_LOADER_MASK_FETCH: ${url} did not answer (${e instanceof Error ? e.message : String(e)}): its host must allow this site (CORS)`
274
+ };
275
+ }
276
+ if (res.status === 404) return { state: "absent" };
277
+ if (!res.ok) return { state: "error", code: "E_LOADER_MASK_FETCH", message: `E_LOADER_MASK_FETCH: ${url} answered ${res.status}` };
278
+ let json;
279
+ try {
280
+ json = await res.json();
281
+ } catch {
282
+ return parsePrintMask(null);
283
+ }
284
+ return parsePrintMask(json);
285
+ })().then((r) => {
286
+ loaded.set(url, r);
287
+ return r;
288
+ });
289
+ loads.set(url, p);
290
+ return p;
291
+ }
292
+ function peekPrintMask(url) {
293
+ return loaded.get(url);
294
+ }
295
+ function sameShape(w, h, pw, ph) {
296
+ if (!(w > 0 && h > 0 && pw > 0 && ph > 0)) return false;
297
+ return Math.abs(w / h / (pw / ph) - 1) <= 0.02;
298
+ }
299
+ function resampleBits(bits, w, h, W, H) {
300
+ if (w === W && h === H) return bits;
301
+ const out = new Uint8Array(W * H);
302
+ for (let y = 0; y < H; y++) {
303
+ const sy = Math.min(h - 1, Math.floor((y + 0.5) * h / H));
304
+ for (let x = 0; x < W; x++) out[y * W + x] = bits[sy * w + Math.min(w - 1, Math.floor((x + 0.5) * w / W))];
305
+ }
306
+ return out;
307
+ }
240
308
  function forgetMasks() {
241
309
  memory.clear();
310
+ loads.clear();
311
+ loaded.clear();
242
312
  const ls = storage();
243
313
  if (!ls) return;
244
314
  try {
@@ -249,6 +319,6 @@ function forgetMasks() {
249
319
  }
250
320
  }
251
321
 
252
- export { CHANGE_THRESHOLD, LEARN_SIDE, MAX_STORED_MASKS, UNRELIABLE_COVERAGE, alphaBox, alphaCanvas, changedBits, coverageOf, forgetMasks, gridSize, isLearnable, meanColour, meanLuma, orBits, packBits, recallMask, rememberMask, snapshotPicture, softAlpha, unpackBits };
253
- //# sourceMappingURL=chunk-JJVBRZTY.js.map
254
- //# sourceMappingURL=chunk-JJVBRZTY.js.map
322
+ export { CHANGE_THRESHOLD, LEARN_SIDE, MAX_STORED_MASKS, PRINT_MASK_FORMAT, UNRELIABLE_COVERAGE, alphaBox, alphaCanvas, changedBits, coverageOf, forgetMasks, gridSize, isLearnable, loadPrintMask, meanColour, meanLuma, orBits, packBits, parsePrintMask, peekPrintMask, recallMask, rememberMask, resampleBits, sameShape, snapshotPicture, softAlpha, unpackBits };
323
+ //# sourceMappingURL=chunk-PY3IOI5B.js.map
324
+ //# sourceMappingURL=chunk-PY3IOI5B.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/mask.ts"],"names":[],"mappings":";;;AAcO,IAAM,UAAA,GAAa;AAEnB,IAAM,gBAAA,GAAmB;AAKzB,IAAM,mBAAA,GAAsB;AAU5B,SAAS,QAAA,CAAS,IAAA,EAAc,IAAA,EAAc,IAAA,GAAO,UAAA,EAAsC;AAChG,EAAA,IAAI,EAAE,IAAA,GAAO,CAAA,CAAA,IAAM,EAAE,IAAA,GAAO,CAAA,CAAA,EAAI,OAAO,EAAE,CAAA,EAAG,CAAA,EAAG,CAAA,EAAG,CAAA,EAAE;AACpD,EAAA,OAAO,IAAA,IAAQ,IAAA,GACX,EAAE,CAAA,EAAG,IAAA,EAAM,CAAA,EAAG,IAAA,CAAK,GAAA,CAAI,CAAA,EAAG,IAAA,CAAK,KAAA,CAAO,IAAA,GAAO,OAAQ,IAAI,CAAC,CAAA,EAAE,GAC5D,EAAE,CAAA,EAAG,IAAA,CAAK,GAAA,CAAI,CAAA,EAAG,IAAA,CAAK,KAAA,CAAO,IAAA,GAAO,IAAA,GAAQ,IAAI,CAAC,CAAA,EAAG,GAAG,IAAA,EAAK;AAClE;AAEA,IAAI,OAAA,GAAoC,IAAA;AAKjC,SAAS,eAAA,CAAgB,EAAA,EAAuC,IAAA,GAAO,UAAA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AACT,EAAA,IAAI;AACF,IAAA,MAAM,QAAQ,IAAA,CAAK,KAAA,CAAM,GAAG,OAAA,CAAQ,QAAQ,KAAK,IAAI,CAAA;AACrD,IAAA,KAAA,MAAW,CAAA,IAAK,KAAA,EAAO,EAAA,CAAG,UAAA,CAAW,YAAY,CAAC,CAAA;AAClD,IAAA,EAAA,CAAG,WAAW,QAAQ,CAAA;AAAA,EACxB,CAAA,CAAA,MAAQ;AAAA,EAER;AACF","file":"chunk-PY3IOI5B.js","sourcesContent":["/**\n * Learned masks: where a product picture changes, measured from the picture itself.\n *\n * A loader snapshots the picture when a render starts, then compares each new frame with it. The pixels that\n * differ are \"what changed\" (the print, the colored body), so the loader can stay on that part instead of the\n * whole frame. What it learns is kept per scene (a mockup id) in memory and, packed small, in localStorage, so\n * the next edit of the same picture knows the area at once.\n *\n * Everything here works on small grids (at most {@link LEARN_SIDE} px on a side): a comparison is a few\n * thousand pixel reads, cheap enough to run several times a second while a render is on the way.\n */\nimport { naturalSize, type PictureElement } from \"./index\";\n\n/** The long side of the grid a picture is compared on. */\nexport const LEARN_SIDE = 192;\n/** A channel difference (0–255, after a light blur) above this is a change, not compression noise. */\nexport const CHANGE_THRESHOLD = 30;\n/**\n * A comparison that finds more than this share of the picture changed is not trusted: it is another picture (a\n * different crop, a first live frame against a differently encoded store picture), not an edit of this one.\n */\nexport const UNRELIABLE_COVERAGE = 0.6;\n\n/** A picture read down to a small grid. */\nexport interface Snapshot {\n w: number;\n h: number;\n rgba: Uint8ClampedArray;\n}\n\n/** The grid size for a picture of `srcW × srcH`: the long side is `side`, the aspect kept. */\nexport function gridSize(srcW: number, srcH: number, side = LEARN_SIDE): { w: number; h: number } {\n if (!(srcW > 0) || !(srcH > 0)) return { w: 0, h: 0 };\n return srcW >= srcH\n ? { w: side, h: Math.max(1, Math.round((side * srcH) / srcW)) }\n : { w: Math.max(1, Math.round((side * srcW) / srcH)), h: side };\n}\n\nlet scratch: HTMLCanvasElement | null = null;\n/**\n * Reads a picture down to its comparison grid. `null` when it cannot be read: not loaded yet, no 2D canvas, or a\n * cross-origin picture served without CORS (the canvas is tainted). A loader then just covers the whole frame.\n */\nexport function snapshotPicture(el: PictureElement | null | undefined, side = LEARN_SIDE): Snapshot | null {\n if (!el || typeof document === \"undefined\") return null;\n if (typeof HTMLImageElement !== \"undefined\" && el instanceof HTMLImageElement && !el.complete) return null;\n const { w: sw, h: sh } = naturalSize(el);\n const { w, h } = gridSize(sw, sh, side);\n if (!w || !h) return null;\n try {\n scratch ??= document.createElement(\"canvas\");\n scratch.width = w;\n scratch.height = h;\n const ctx = scratch.getContext(\"2d\", { willReadFrequently: true });\n if (!ctx) return null;\n ctx.clearRect(0, 0, w, h);\n ctx.drawImage(el as CanvasImageSource, 0, 0, w, h);\n return { w, h, rgba: ctx.getImageData(0, 0, w, h).data };\n } catch {\n return null; // tainted (no CORS) or a decode error: nothing to learn from\n }\n}\n\n/** A 3 × 3 box blur of each color channel: compression speckle averages out before pixels are compared. */\nfunction blurred(s: Snapshot): Float32Array {\n const { w, h, rgba } = s;\n const out = new Float32Array(w * h * 3);\n for (let y = 0; y < h; y++) {\n for (let x = 0; x < w; x++) {\n let r = 0, g = 0, b = 0, n = 0;\n for (let dy = -1; dy <= 1; dy++) {\n const yy = y + dy;\n if (yy < 0 || yy >= h) continue;\n for (let dx = -1; dx <= 1; dx++) {\n const xx = x + dx;\n if (xx < 0 || xx >= w) continue;\n const i = (yy * w + xx) * 4;\n r += rgba[i]; g += rgba[i + 1]; b += rgba[i + 2]; n++;\n }\n }\n const o = (y * w + x) * 3;\n out[o] = r / n; out[o + 1] = g / n; out[o + 2] = b / n;\n }\n }\n return out;\n}\n\n/**\n * Which grid cells changed between two snapshots of the same picture (1 = changed). `null` when they are not the\n * same grid (another picture, another aspect): there is nothing to compare.\n */\nexport function changedBits(a: Snapshot, b: Snapshot, threshold = CHANGE_THRESHOLD): Uint8Array | null {\n if (a.w !== b.w || a.h !== b.h || a.rgba.length !== b.rgba.length) return null;\n const A = blurred(a), B = blurred(b);\n const bits = new Uint8Array(a.w * a.h);\n for (let i = 0; i < bits.length; i++) {\n const o = i * 3;\n const d = Math.max(Math.abs(A[o] - B[o]), Math.abs(A[o + 1] - B[o + 1]), Math.abs(A[o + 2] - B[o + 2]));\n bits[i] = d > threshold ? 1 : 0;\n }\n return bits;\n}\n\n/** The share of cells set. */\nexport function coverageOf(bits: Uint8Array): number {\n let n = 0;\n for (let i = 0; i < bits.length; i++) n += bits[i];\n return bits.length ? n / bits.length : 0;\n}\n\n/** Whether a comparison is worth learning from: something changed, and not so much that it is another picture. */\nexport function isLearnable(bits: Uint8Array | null): bits is Uint8Array {\n if (!bits) return false;\n const c = coverageOf(bits);\n return c > 0 && c <= UNRELIABLE_COVERAGE;\n}\n\n/** Cells set in either. */\nexport function orBits(a: Uint8Array, b: Uint8Array): Uint8Array {\n const out = new Uint8Array(Math.max(a.length, b.length));\n for (let i = 0; i < out.length; i++) out[i] = (a[i] | b[i]) as 0 | 1;\n return out;\n}\n\nfunction boxPass(src: Float32Array, w: number, h: number, r: number, horizontal: boolean, op: \"max\" | \"mean\"): Float32Array {\n const out = new Float32Array(src.length);\n const len = horizontal ? w : h, lines = horizontal ? h : w;\n for (let l = 0; l < lines; l++) {\n for (let i = 0; i < len; i++) {\n let acc = op === \"max\" ? 0 : 0, n = 0;\n for (let k = -r; k <= r; k++) {\n const j = i + k;\n if (j < 0 || j >= len) continue;\n const v = src[horizontal ? l * w + j : j * w + l];\n if (op === \"max\") { if (v > acc) acc = v; } else { acc += v; n++; }\n }\n out[horizontal ? l * w + i : i * w + l] = op === \"max\" ? acc : acc / n;\n }\n }\n return out;\n}\n\n/**\n * A soft alpha (0–1) from changed cells: grown by `grow` cells (a stroke of a letter becomes the letter's area),\n * then blurred by `blur` cells, so the loader's edge fades out instead of stopping at a hard line. The inside stays\n * fully opaque.\n */\nexport function softAlpha(bits: Uint8Array, w: number, h: number, opts: { grow?: number; blur?: number } = {}): Float32Array {\n const grow = Math.max(0, Math.round(opts.grow ?? 2));\n const blur = Math.max(0, Math.round(opts.blur ?? 4));\n let a: Float32Array = Float32Array.from(bits);\n if (grow) a = boxPass(boxPass(a, w, h, grow, true, \"max\"), w, h, grow, false, \"max\");\n const r = Math.max(1, Math.round(blur / 2));\n if (blur) for (let p = 0; p < 3; p++) a = boxPass(boxPass(a, w, h, r, true, \"mean\"), w, h, r, false, \"mean\");\n for (let i = 0; i < a.length; i++) a[i] = Math.min(1, a[i] * 1.6);\n return a;\n}\n\n/** The box (0–1, picture space) holding every cell above `threshold`, or `null` when there is none. */\nexport function alphaBox(alpha: Float32Array, w: number, h: number, threshold = 0.2): { x: number; y: number; w: number; h: number } | null {\n let x0 = w, y0 = h, x1 = -1, y1 = -1;\n for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {\n if (alpha[y * w + x] <= threshold) continue;\n if (x < x0) x0 = x; if (x > x1) x1 = x; if (y < y0) y0 = y; if (y > y1) y1 = y;\n }\n if (x1 < 0) return null;\n return { x: x0 / w, y: y0 / h, w: (x1 - x0 + 1) / w, h: (y1 - y0 + 1) / h };\n}\n\n/** A soft alpha as a canvas (white, alpha = mask), ready to `drawImage` over a frame. `null` without a 2D canvas. */\nexport function alphaCanvas(alpha: Float32Array, w: number, h: number): HTMLCanvasElement | null {\n if (typeof document === \"undefined\") return null;\n const c = document.createElement(\"canvas\");\n c.width = w;\n c.height = h;\n const ctx = c.getContext(\"2d\");\n if (!ctx) return null;\n const img = ctx.createImageData(w, h);\n for (let i = 0; i < alpha.length; i++) {\n const o = i * 4;\n img.data[o] = img.data[o + 1] = img.data[o + 2] = 255;\n img.data[o + 3] = Math.round(alpha[i] * 255);\n }\n ctx.putImageData(img, 0, 0);\n return c;\n}\n\n/** The mean color of the cells set (from a snapshot), or `null` when none is. A loader tints its light with it. */\nexport function meanColour(s: Snapshot, bits: Uint8Array): [number, number, number] | null {\n let r = 0, g = 0, b = 0, n = 0;\n for (let i = 0; i < bits.length; i++) {\n if (!bits[i]) continue;\n const o = i * 4;\n r += s.rgba[o]; g += s.rgba[o + 1]; b += s.rgba[o + 2]; n++;\n }\n return n ? [r / n, g / n, b / n] : null;\n}\n\n/**\n * The mean brightness (0–1) of a snapshot, only where `alpha` is above `threshold` when given. A loader uses it to\n * know whether light can show on the surface (on white it can't).\n */\nexport function meanLuma(s: Snapshot, alpha?: Float32Array | null, threshold = 0.2): number {\n let sum = 0, n = 0;\n for (let i = 0; i < s.w * s.h; i++) {\n if (alpha && alpha[i] <= threshold) continue;\n const o = i * 4;\n sum += 0.2126 * s.rgba[o] + 0.7152 * s.rgba[o + 1] + 0.0722 * s.rgba[o + 2];\n n++;\n }\n return n ? sum / n / 255 : 0.5;\n}\n\n// ---------- what was learned, per scene ----------\n\n// v2 (2026-12-01): v1 masks could hold a whole cup learned across a color switch by the first beam (#6723), and a\n// remembered area is kept for good: every v1 entry is dropped once, on first use.\nconst LS_PREFIX = \"sc-loaders:mask:v2:\";\nconst LS_INDEX = \"sc-loaders:mask:v2:index\";\nconst RETIRED_PREFIXES = [\"sc-loaders:mask:v1:\"];\nlet retiredSwept = false;\nfunction sweepRetired(ls: Storage): void {\n if (retiredSwept) return;\n retiredSwept = true;\n try {\n for (let i = ls.length - 1; i >= 0; i--) {\n const k = ls.key(i);\n if (k && RETIRED_PREFIXES.some((p) => k.startsWith(p))) ls.removeItem(k);\n }\n } catch {\n // blocked storage: nothing to sweep\n }\n}\n/** At most this many scenes are kept in localStorage (oldest out first); a scene is ~1 KB. */\nexport const MAX_STORED_MASKS = 40;\nconst memory = new Map<string, Uint8Array>();\n\nconst slot = (key: string, w: number, h: number) => `${key}@${w}x${h}`;\n\n/** Bits to base64, 8 cells a byte. */\nexport function packBits(bits: Uint8Array): string {\n const bytes = new Uint8Array(Math.ceil(bits.length / 8));\n for (let i = 0; i < bits.length; i++) if (bits[i]) bytes[i >> 3] |= 1 << (i & 7);\n let s = \"\";\n for (let i = 0; i < bytes.length; i++) s += String.fromCharCode(bytes[i]);\n return btoa(s);\n}\n\n/** base64 back to `n` bits; `null` if it does not hold `n`. */\nexport function unpackBits(packed: string, n: number): Uint8Array | null {\n let s: string;\n try { s = atob(packed); } catch { return null; }\n if (s.length !== Math.ceil(n / 8)) return null;\n const bits = new Uint8Array(n);\n for (let i = 0; i < n; i++) bits[i] = (s.charCodeAt(i >> 3) >> (i & 7)) & 1;\n return bits;\n}\n\nfunction storage(): Storage | null {\n let ls: Storage | null = null;\n try { ls = typeof localStorage === \"undefined\" ? null : localStorage; } catch { return null; }\n if (ls) sweepRetired(ls);\n return ls;\n}\n\n/** What has been learned about a scene's picture on this grid, or `null`. */\nexport function recallMask(key: string, w: number, h: number): Uint8Array | null {\n const k = slot(key, w, h);\n const hit = memory.get(k);\n if (hit) return hit;\n const raw = storage()?.getItem(LS_PREFIX + k);\n const bits = raw ? unpackBits(raw, w * h) : null;\n if (bits) memory.set(k, bits);\n return bits;\n}\n\n/** Adds what changed this time to what a scene already knows; returns the union. */\nexport function rememberMask(key: string, w: number, h: number, bits: Uint8Array): Uint8Array {\n const k = slot(key, w, h);\n const prev = recallMask(key, w, h);\n const next = prev ? orBits(prev, bits) : bits;\n memory.set(k, next);\n const ls = storage();\n if (ls) {\n try {\n ls.setItem(LS_PREFIX + k, packBits(next));\n const index = (JSON.parse(ls.getItem(LS_INDEX) || \"[]\") as string[]).filter((x) => x !== k);\n index.push(k);\n while (index.length > MAX_STORED_MASKS) ls.removeItem(LS_PREFIX + index.shift()!);\n ls.setItem(LS_INDEX, JSON.stringify(index));\n } catch {\n // full or blocked storage: memory still has it for this visit\n }\n }\n return next;\n}\n\n// ---------- given masks: made on the server, fetched by URL ----------\n\n/** The print-mask file's format tag (Snowcone's catalog: `mockups[].printMask`). */\nexport const PRINT_MASK_FORMAT = \"sc-print-mask/1\";\n/** A mask on a grid (1 = the part that changes). */\nexport interface GridMask {\n w: number;\n h: number;\n bits: Uint8Array;\n}\n\n/**\n * What became of a mask URL: `mask` (read), `absent` (404: not made yet, learn from the frames), or an error a host\n * can act on: `E_LOADER_MASK_FETCH` (no answer, or an answer other than 200 / 404) and `E_LOADER_MASK_INVALID` (not\n * an `sc-print-mask/1` file), each with what to fix.\n */\nexport type MaskLoad =\n | { state: \"mask\"; mask: GridMask }\n | { state: \"absent\" }\n | { state: \"error\"; code: \"E_LOADER_MASK_FETCH\" | \"E_LOADER_MASK_INVALID\"; message: string };\n\n/**\n * Reads an `sc-print-mask/1` file: `{ format, w, h, bits }`, `bits` = {@link packBits} of `w × h` cells. Its grid is\n * the beam's: the long side {@link LEARN_SIDE} cells, so it lays over the whole picture it was made from.\n */\nexport function parsePrintMask(json: unknown): MaskLoad {\n const bad = (why: string): MaskLoad => ({\n state: \"error\",\n code: \"E_LOADER_MASK_INVALID\",\n message: `E_LOADER_MASK_INVALID: ${why}. A mask URL must serve an ${PRINT_MASK_FORMAT} file ({ format, w, h, bits }); pass the URL the catalog names (mockups[].printMask), or none.`,\n });\n if (!json || typeof json !== \"object\") return bad(\"the answer is not a JSON object\");\n const f = json as { format?: unknown; w?: unknown; h?: unknown; bits?: unknown };\n if (f.format !== PRINT_MASK_FORMAT) return bad(`format is ${JSON.stringify(f.format)}, not \"${PRINT_MASK_FORMAT}\"`);\n const w = Number(f.w), h = Number(f.h);\n if (!Number.isInteger(w) || !Number.isInteger(h) || w < 1 || h < 1 || w > 4 * LEARN_SIDE || h > 4 * LEARN_SIDE) {\n return bad(`the grid is ${String(f.w)} x ${String(f.h)}`);\n }\n const bits = typeof f.bits === \"string\" ? unpackBits(f.bits, w * h) : null;\n if (!bits) return bad(`bits does not hold ${w * h} cells`);\n if (!bits.some((b) => b === 1)) return bad(\"no cell is set (an empty mask)\");\n return { state: \"mask\", mask: { w, h, bits } };\n}\n\nconst loads = new Map<string, Promise<MaskLoad>>();\nconst loaded = new Map<string, MaskLoad>();\n\n/**\n * Fetches a mask file once per URL per page (the promise is shared; the HTTP cache keeps it across visits: a mask URL\n * is immutable). A 404 is `absent`, never an error: the mask is not made yet.\n */\nexport function loadPrintMask(url: string): Promise<MaskLoad> {\n const hit = loads.get(url);\n if (hit) return hit;\n const p = (async (): Promise<MaskLoad> => {\n if (typeof fetch !== \"function\") return { state: \"error\", code: \"E_LOADER_MASK_FETCH\", message: \"E_LOADER_MASK_FETCH: no fetch here\" };\n let res: Response;\n try {\n res = await fetch(url, { credentials: \"omit\" });\n } catch (e) {\n return {\n state: \"error\",\n code: \"E_LOADER_MASK_FETCH\",\n message: `E_LOADER_MASK_FETCH: ${url} did not answer (${e instanceof Error ? e.message : String(e)}): its host must allow this site (CORS)`,\n };\n }\n if (res.status === 404) return { state: \"absent\" };\n if (!res.ok) return { state: \"error\", code: \"E_LOADER_MASK_FETCH\", message: `E_LOADER_MASK_FETCH: ${url} answered ${res.status}` };\n let json: unknown;\n try {\n json = await res.json();\n } catch {\n return parsePrintMask(null);\n }\n return parsePrintMask(json);\n })().then((r) => {\n loaded.set(url, r);\n return r;\n });\n loads.set(url, p);\n return p;\n}\n\n/** A mask URL's answer if it has already come back on this page (a loader starts on it at once), else `undefined`. */\nexport function peekPrintMask(url: string): MaskLoad | undefined {\n return loaded.get(url);\n}\n\n/** Whether a `w × h` grid and a picture of `pw × ph` have the same shape (within 2 %): a mask of the whole picture. */\nexport function sameShape(w: number, h: number, pw: number, ph: number): boolean {\n if (!(w > 0 && h > 0 && pw > 0 && ph > 0)) return false;\n return Math.abs(w / h / (pw / ph) - 1) <= 0.02;\n}\n\n/** A grid's bits read onto another grid of the same shape (nearest cell). */\nexport function resampleBits(bits: Uint8Array, w: number, h: number, W: number, H: number): Uint8Array {\n if (w === W && h === H) return bits;\n const out = new Uint8Array(W * H);\n for (let y = 0; y < H; y++) {\n const sy = Math.min(h - 1, Math.floor(((y + 0.5) * h) / H));\n for (let x = 0; x < W; x++) out[y * W + x] = bits[sy * w + Math.min(w - 1, Math.floor(((x + 0.5) * w) / W))];\n }\n return out;\n}\n\n/** Forgets everything learned (tests; a \"reset\" in a demo). */\nexport function forgetMasks(): void {\n memory.clear();\n loads.clear();\n loaded.clear();\n const ls = storage();\n if (!ls) return;\n try {\n const index = JSON.parse(ls.getItem(LS_INDEX) || \"[]\") as string[];\n for (const k of index) ls.removeItem(LS_PREFIX + k);\n ls.removeItem(LS_INDEX);\n } catch {\n // nothing to clear\n }\n}\n"]}
@@ -15,5 +15,5 @@ function naturalSize(el) {
15
15
 
16
16
  exports.fitRect = fitRect;
17
17
  exports.naturalSize = naturalSize;
18
- //# sourceMappingURL=chunk-NCXRWPGN.cjs.map
19
- //# sourceMappingURL=chunk-NCXRWPGN.cjs.map
18
+ //# sourceMappingURL=chunk-WNBSD6UY.cjs.map
19
+ //# sourceMappingURL=chunk-WNBSD6UY.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AA4EO,SAAS,QAAQ,QAAA,EAAkB,QAAA,EAAkB,IAAA,EAAc,IAAA,EAAc,MAAkB,OAAA,EAAe;AACvH,EAAA,IAAI,EAAE,WAAW,CAAA,CAAA,IAAM,EAAE,WAAW,CAAA,CAAA,IAAM,EAAE,OAAO,CAAA,CAAA,IAAM,EAAE,OAAO,CAAA,CAAA,EAAI,OAAO,EAAE,CAAA,EAAG,CAAA,EAAG,GAAG,CAAA,EAAG,CAAA,EAAG,IAAA,EAAM,CAAA,EAAG,IAAA,EAAK;AAC5G,EAAA,MAAM,CAAA,GAAI,GAAA,KAAQ,OAAA,GAAU,IAAA,CAAK,IAAI,IAAA,GAAO,QAAA,EAAU,IAAA,GAAO,QAAQ,IAAI,IAAA,CAAK,GAAA,CAAI,IAAA,GAAO,QAAA,EAAU,OAAO,QAAQ,CAAA;AAClH,EAAA,MAAM,IAAI,QAAA,GAAW,CAAA;AACrB,EAAA,MAAM,IAAI,QAAA,GAAW,CAAA;AACrB,EAAA,OAAO,EAAE,CAAA,EAAA,CAAI,IAAA,GAAO,CAAA,IAAK,CAAA,EAAG,IAAI,IAAA,GAAO,CAAA,IAAK,CAAA,EAAG,CAAA,EAAG,CAAA,EAAE;AACtD;AAGO,SAAS,YAAY,EAAA,EAA8C;AACxE,EAAA,IAAI,OAAO,gBAAA,KAAqB,WAAA,IAAe,EAAA,YAAc,gBAAA,EAAkB,OAAO,EAAE,CAAA,EAAG,EAAA,CAAG,YAAA,EAAc,CAAA,EAAG,EAAA,CAAG,aAAA,EAAc;AAChI,EAAA,OAAO,EAAE,CAAA,EAAI,EAAA,CAAuC,KAAA,EAAO,CAAA,EAAI,GAAuC,MAAA,EAAO;AAC/G","file":"chunk-WNBSD6UY.cjs","sourcesContent":["/**\n * @snowcone-app/loaders — the loader contract.\n *\n * A loader paints OVER a live product picture while its next render is on the way, and goes away once the picture\n * has caught up. This entry has no loader in it on purpose: each loader is its own import\n * (`@snowcone-app/loaders/beam`), so a site only downloads the loaders it uses. Write your own against\n * {@link LoaderProps} and any Snowcone surface can host it.\n */\n\n/** How the picture fills its frame: the `object-fit` of the `<img>` the loader paints over. */\nexport type PictureFit = \"cover\" | \"contain\";\n\n/** A picture element a loader can read: an `<img>`, a `<canvas>`, an `ImageBitmap`. */\nexport type PictureElement = HTMLImageElement | HTMLCanvasElement | ImageBitmap;\n\n/** What every Snowcone loader takes from its host. */\nexport interface LoaderProps {\n /**\n * `true` while the picture is behind (a render is in flight). Going `false` means caught up: the loader plays its\n * exit, then calls {@link LoaderProps.onExited}. Going `true` again before that turns it back, never a fresh start.\n */\n visible: boolean;\n /** Called once the exit has finished: unmount the loader then. */\n onExited?: () => void;\n /**\n * The picture on screen now (the topmost painted `<img>`). Called as often as the loader likes, so return the\n * current element, not a cached one. Lets a loader see what changes; without it the loader covers the whole frame.\n */\n picture?: () => PictureElement | null;\n /** A stable id of the scene (a mockup id). Whatever a loader learns about the picture is kept per key. */\n maskKey?: string;\n /** The `object-fit` of the picture's `<img>`. Defaults to `\"cover\"`. */\n fit?: PictureFit;\n /**\n * A known mask of the part that changes (alpha = changes), in the picture's own pixel space. Wins over anything\n * learned. Leave it out and loaders that need one learn it from the frames.\n */\n mask?: PictureElement | null;\n /**\n * The URL of this scene's print mask: where its picture shows the print, made once on the server from a white and a\n * black render (an `sc-print-mask/1` file; Snowcone's catalog names one on every mockup: `mockups[].printMask`).\n * Pass it with the `maskKey` it belongs to. A loader fetches it once per page and stays on that area from the FIRST\n * edit of a scene it has never seen (`data-sc-loader-mask=\"given\"`); without it, or until the file exists (404), it\n * learns the area from the frames as before. `mask` wins over it.\n */\n maskUrl?: string | null;\n className?: string;\n style?: import(\"react\").CSSProperties;\n}\n\n/** Where a loader is in its life, stamped as `data-sc-loader-phase` (agents assert on it, never on pixels). */\nexport type LoaderPhase = \"loading\" | \"finishing\";\n\n/** Where the loader's area came from, stamped as `data-sc-loader-mask`. */\nexport type LoaderMaskSource = \"whole\" | \"learned\" | \"given\";\n\n/**\n * What became of `maskUrl`, stamped as `data-sc-loader-mask-url` (absent without one): `loading`; `used` (its area is\n * on screen); `absent` (404: not made yet, the loader learns); `other-shape` (the picture on screen is a crop, not the\n * whole picture the mask was made from: the loader learns); or the error code `E_LOADER_MASK_FETCH` /\n * `E_LOADER_MASK_INVALID` (the loader learns; fix the URL or the file).\n */\nexport type LoaderMaskUrlState = \"loading\" | \"used\" | \"absent\" | \"other-shape\" | \"E_LOADER_MASK_FETCH\" | \"E_LOADER_MASK_INVALID\";\n\n/** A rectangle in frame pixels. */\nexport interface Rect {\n x: number;\n y: number;\n w: number;\n h: number;\n}\n\n/**\n * Where a picture of `naturalW × naturalH` lands in a `boxW × boxH` frame under `object-fit` (centred, like the\n * browser's default `object-position`). Loaders use it to lay anything in picture space (a mask) over the frame.\n */\nexport function fitRect(naturalW: number, naturalH: number, boxW: number, boxH: number, fit: PictureFit = \"cover\"): Rect {\n if (!(naturalW > 0) || !(naturalH > 0) || !(boxW > 0) || !(boxH > 0)) return { x: 0, y: 0, w: boxW, h: boxH };\n const s = fit === \"cover\" ? Math.max(boxW / naturalW, boxH / naturalH) : Math.min(boxW / naturalW, boxH / naturalH);\n const w = naturalW * s;\n const h = naturalH * s;\n return { x: (boxW - w) / 2, y: (boxH - h) / 2, w, h };\n}\n\n/** The natural size of a picture element (0 × 0 while an `<img>` has not loaded). */\nexport function naturalSize(el: PictureElement): { w: number; h: number } {\n if (typeof HTMLImageElement !== \"undefined\" && el instanceof HTMLImageElement) return { w: el.naturalWidth, h: el.naturalHeight };\n return { w: (el as HTMLCanvasElement | ImageBitmap).width, h: (el as HTMLCanvasElement | ImageBitmap).height };\n}\n"]}
package/dist/index.cjs CHANGED
@@ -1,16 +1,16 @@
1
1
  'use strict';
2
2
 
3
- var chunkNCXRWPGN_cjs = require('./chunk-NCXRWPGN.cjs');
3
+ var chunkWNBSD6UY_cjs = require('./chunk-WNBSD6UY.cjs');
4
4
 
5
5
 
6
6
 
7
7
  Object.defineProperty(exports, "fitRect", {
8
8
  enumerable: true,
9
- get: function () { return chunkNCXRWPGN_cjs.fitRect; }
9
+ get: function () { return chunkWNBSD6UY_cjs.fitRect; }
10
10
  });
11
11
  Object.defineProperty(exports, "naturalSize", {
12
12
  enumerable: true,
13
- get: function () { return chunkNCXRWPGN_cjs.naturalSize; }
13
+ get: function () { return chunkWNBSD6UY_cjs.naturalSize; }
14
14
  });
15
15
  //# sourceMappingURL=index.cjs.map
16
16
  //# sourceMappingURL=index.cjs.map
package/dist/index.d.cts CHANGED
@@ -35,6 +35,14 @@ interface LoaderProps {
35
35
  * learned. Leave it out and loaders that need one learn it from the frames.
36
36
  */
37
37
  mask?: PictureElement | null;
38
+ /**
39
+ * The URL of this scene's print mask: where its picture shows the print, made once on the server from a white and a
40
+ * black render (an `sc-print-mask/1` file; Snowcone's catalog names one on every mockup: `mockups[].printMask`).
41
+ * Pass it with the `maskKey` it belongs to. A loader fetches it once per page and stays on that area from the FIRST
42
+ * edit of a scene it has never seen (`data-sc-loader-mask="given"`); without it, or until the file exists (404), it
43
+ * learns the area from the frames as before. `mask` wins over it.
44
+ */
45
+ maskUrl?: string | null;
38
46
  className?: string;
39
47
  style?: react.CSSProperties;
40
48
  }
@@ -42,6 +50,13 @@ interface LoaderProps {
42
50
  type LoaderPhase = "loading" | "finishing";
43
51
  /** Where the loader's area came from, stamped as `data-sc-loader-mask`. */
44
52
  type LoaderMaskSource = "whole" | "learned" | "given";
53
+ /**
54
+ * What became of `maskUrl`, stamped as `data-sc-loader-mask-url` (absent without one): `loading`; `used` (its area is
55
+ * on screen); `absent` (404: not made yet, the loader learns); `other-shape` (the picture on screen is a crop, not the
56
+ * whole picture the mask was made from: the loader learns); or the error code `E_LOADER_MASK_FETCH` /
57
+ * `E_LOADER_MASK_INVALID` (the loader learns; fix the URL or the file).
58
+ */
59
+ type LoaderMaskUrlState = "loading" | "used" | "absent" | "other-shape" | "E_LOADER_MASK_FETCH" | "E_LOADER_MASK_INVALID";
45
60
  /** A rectangle in frame pixels. */
46
61
  interface Rect {
47
62
  x: number;
@@ -60,4 +75,4 @@ declare function naturalSize(el: PictureElement): {
60
75
  h: number;
61
76
  };
62
77
 
63
- export { type LoaderMaskSource, type LoaderPhase, type LoaderProps, type PictureElement, type PictureFit, type Rect, fitRect, naturalSize };
78
+ export { type LoaderMaskSource, type LoaderMaskUrlState, type LoaderPhase, type LoaderProps, type PictureElement, type PictureFit, type Rect, fitRect, naturalSize };
package/dist/index.d.ts CHANGED
@@ -35,6 +35,14 @@ interface LoaderProps {
35
35
  * learned. Leave it out and loaders that need one learn it from the frames.
36
36
  */
37
37
  mask?: PictureElement | null;
38
+ /**
39
+ * The URL of this scene's print mask: where its picture shows the print, made once on the server from a white and a
40
+ * black render (an `sc-print-mask/1` file; Snowcone's catalog names one on every mockup: `mockups[].printMask`).
41
+ * Pass it with the `maskKey` it belongs to. A loader fetches it once per page and stays on that area from the FIRST
42
+ * edit of a scene it has never seen (`data-sc-loader-mask="given"`); without it, or until the file exists (404), it
43
+ * learns the area from the frames as before. `mask` wins over it.
44
+ */
45
+ maskUrl?: string | null;
38
46
  className?: string;
39
47
  style?: react.CSSProperties;
40
48
  }
@@ -42,6 +50,13 @@ interface LoaderProps {
42
50
  type LoaderPhase = "loading" | "finishing";
43
51
  /** Where the loader's area came from, stamped as `data-sc-loader-mask`. */
44
52
  type LoaderMaskSource = "whole" | "learned" | "given";
53
+ /**
54
+ * What became of `maskUrl`, stamped as `data-sc-loader-mask-url` (absent without one): `loading`; `used` (its area is
55
+ * on screen); `absent` (404: not made yet, the loader learns); `other-shape` (the picture on screen is a crop, not the
56
+ * whole picture the mask was made from: the loader learns); or the error code `E_LOADER_MASK_FETCH` /
57
+ * `E_LOADER_MASK_INVALID` (the loader learns; fix the URL or the file).
58
+ */
59
+ type LoaderMaskUrlState = "loading" | "used" | "absent" | "other-shape" | "E_LOADER_MASK_FETCH" | "E_LOADER_MASK_INVALID";
45
60
  /** A rectangle in frame pixels. */
46
61
  interface Rect {
47
62
  x: number;
@@ -60,4 +75,4 @@ declare function naturalSize(el: PictureElement): {
60
75
  h: number;
61
76
  };
62
77
 
63
- export { type LoaderMaskSource, type LoaderPhase, type LoaderProps, type PictureElement, type PictureFit, type Rect, fitRect, naturalSize };
78
+ export { type LoaderMaskSource, type LoaderMaskUrlState, type LoaderPhase, type LoaderProps, type PictureElement, type PictureFit, type Rect, fitRect, naturalSize };
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export { fitRect, naturalSize } from './chunk-6IWUTOBS.js';
1
+ export { fitRect, naturalSize } from './chunk-33ZDGDZM.js';
2
2
  //# sourceMappingURL=index.js.map
3
3
  //# sourceMappingURL=index.js.map
package/dist/mask.cjs CHANGED
@@ -1,89 +1,113 @@
1
1
  'use strict';
2
2
 
3
- var chunk66FMKFHB_cjs = require('./chunk-66FMKFHB.cjs');
4
- require('./chunk-NCXRWPGN.cjs');
3
+ var chunk3ZCOS2RW_cjs = require('./chunk-3ZCOS2RW.cjs');
4
+ require('./chunk-WNBSD6UY.cjs');
5
5
 
6
6
 
7
7
 
8
8
  Object.defineProperty(exports, "CHANGE_THRESHOLD", {
9
9
  enumerable: true,
10
- get: function () { return chunk66FMKFHB_cjs.CHANGE_THRESHOLD; }
10
+ get: function () { return chunk3ZCOS2RW_cjs.CHANGE_THRESHOLD; }
11
11
  });
12
12
  Object.defineProperty(exports, "LEARN_SIDE", {
13
13
  enumerable: true,
14
- get: function () { return chunk66FMKFHB_cjs.LEARN_SIDE; }
14
+ get: function () { return chunk3ZCOS2RW_cjs.LEARN_SIDE; }
15
15
  });
16
16
  Object.defineProperty(exports, "MAX_STORED_MASKS", {
17
17
  enumerable: true,
18
- get: function () { return chunk66FMKFHB_cjs.MAX_STORED_MASKS; }
18
+ get: function () { return chunk3ZCOS2RW_cjs.MAX_STORED_MASKS; }
19
+ });
20
+ Object.defineProperty(exports, "PRINT_MASK_FORMAT", {
21
+ enumerable: true,
22
+ get: function () { return chunk3ZCOS2RW_cjs.PRINT_MASK_FORMAT; }
19
23
  });
20
24
  Object.defineProperty(exports, "UNRELIABLE_COVERAGE", {
21
25
  enumerable: true,
22
- get: function () { return chunk66FMKFHB_cjs.UNRELIABLE_COVERAGE; }
26
+ get: function () { return chunk3ZCOS2RW_cjs.UNRELIABLE_COVERAGE; }
23
27
  });
24
28
  Object.defineProperty(exports, "alphaBox", {
25
29
  enumerable: true,
26
- get: function () { return chunk66FMKFHB_cjs.alphaBox; }
30
+ get: function () { return chunk3ZCOS2RW_cjs.alphaBox; }
27
31
  });
28
32
  Object.defineProperty(exports, "alphaCanvas", {
29
33
  enumerable: true,
30
- get: function () { return chunk66FMKFHB_cjs.alphaCanvas; }
34
+ get: function () { return chunk3ZCOS2RW_cjs.alphaCanvas; }
31
35
  });
32
36
  Object.defineProperty(exports, "changedBits", {
33
37
  enumerable: true,
34
- get: function () { return chunk66FMKFHB_cjs.changedBits; }
38
+ get: function () { return chunk3ZCOS2RW_cjs.changedBits; }
35
39
  });
36
40
  Object.defineProperty(exports, "coverageOf", {
37
41
  enumerable: true,
38
- get: function () { return chunk66FMKFHB_cjs.coverageOf; }
42
+ get: function () { return chunk3ZCOS2RW_cjs.coverageOf; }
39
43
  });
40
44
  Object.defineProperty(exports, "forgetMasks", {
41
45
  enumerable: true,
42
- get: function () { return chunk66FMKFHB_cjs.forgetMasks; }
46
+ get: function () { return chunk3ZCOS2RW_cjs.forgetMasks; }
43
47
  });
44
48
  Object.defineProperty(exports, "gridSize", {
45
49
  enumerable: true,
46
- get: function () { return chunk66FMKFHB_cjs.gridSize; }
50
+ get: function () { return chunk3ZCOS2RW_cjs.gridSize; }
47
51
  });
48
52
  Object.defineProperty(exports, "isLearnable", {
49
53
  enumerable: true,
50
- get: function () { return chunk66FMKFHB_cjs.isLearnable; }
54
+ get: function () { return chunk3ZCOS2RW_cjs.isLearnable; }
55
+ });
56
+ Object.defineProperty(exports, "loadPrintMask", {
57
+ enumerable: true,
58
+ get: function () { return chunk3ZCOS2RW_cjs.loadPrintMask; }
51
59
  });
52
60
  Object.defineProperty(exports, "meanColour", {
53
61
  enumerable: true,
54
- get: function () { return chunk66FMKFHB_cjs.meanColour; }
62
+ get: function () { return chunk3ZCOS2RW_cjs.meanColour; }
55
63
  });
56
64
  Object.defineProperty(exports, "meanLuma", {
57
65
  enumerable: true,
58
- get: function () { return chunk66FMKFHB_cjs.meanLuma; }
66
+ get: function () { return chunk3ZCOS2RW_cjs.meanLuma; }
59
67
  });
60
68
  Object.defineProperty(exports, "orBits", {
61
69
  enumerable: true,
62
- get: function () { return chunk66FMKFHB_cjs.orBits; }
70
+ get: function () { return chunk3ZCOS2RW_cjs.orBits; }
63
71
  });
64
72
  Object.defineProperty(exports, "packBits", {
65
73
  enumerable: true,
66
- get: function () { return chunk66FMKFHB_cjs.packBits; }
74
+ get: function () { return chunk3ZCOS2RW_cjs.packBits; }
75
+ });
76
+ Object.defineProperty(exports, "parsePrintMask", {
77
+ enumerable: true,
78
+ get: function () { return chunk3ZCOS2RW_cjs.parsePrintMask; }
79
+ });
80
+ Object.defineProperty(exports, "peekPrintMask", {
81
+ enumerable: true,
82
+ get: function () { return chunk3ZCOS2RW_cjs.peekPrintMask; }
67
83
  });
68
84
  Object.defineProperty(exports, "recallMask", {
69
85
  enumerable: true,
70
- get: function () { return chunk66FMKFHB_cjs.recallMask; }
86
+ get: function () { return chunk3ZCOS2RW_cjs.recallMask; }
71
87
  });
72
88
  Object.defineProperty(exports, "rememberMask", {
73
89
  enumerable: true,
74
- get: function () { return chunk66FMKFHB_cjs.rememberMask; }
90
+ get: function () { return chunk3ZCOS2RW_cjs.rememberMask; }
91
+ });
92
+ Object.defineProperty(exports, "resampleBits", {
93
+ enumerable: true,
94
+ get: function () { return chunk3ZCOS2RW_cjs.resampleBits; }
95
+ });
96
+ Object.defineProperty(exports, "sameShape", {
97
+ enumerable: true,
98
+ get: function () { return chunk3ZCOS2RW_cjs.sameShape; }
75
99
  });
76
100
  Object.defineProperty(exports, "snapshotPicture", {
77
101
  enumerable: true,
78
- get: function () { return chunk66FMKFHB_cjs.snapshotPicture; }
102
+ get: function () { return chunk3ZCOS2RW_cjs.snapshotPicture; }
79
103
  });
80
104
  Object.defineProperty(exports, "softAlpha", {
81
105
  enumerable: true,
82
- get: function () { return chunk66FMKFHB_cjs.softAlpha; }
106
+ get: function () { return chunk3ZCOS2RW_cjs.softAlpha; }
83
107
  });
84
108
  Object.defineProperty(exports, "unpackBits", {
85
109
  enumerable: true,
86
- get: function () { return chunk66FMKFHB_cjs.unpackBits; }
110
+ get: function () { return chunk3ZCOS2RW_cjs.unpackBits; }
87
111
  });
88
112
  //# sourceMappingURL=mask.cjs.map
89
113
  //# sourceMappingURL=mask.cjs.map
package/dist/mask.d.cts CHANGED
@@ -84,7 +84,46 @@ declare function unpackBits(packed: string, n: number): Uint8Array | null;
84
84
  declare function recallMask(key: string, w: number, h: number): Uint8Array | null;
85
85
  /** Adds what changed this time to what a scene already knows; returns the union. */
86
86
  declare function rememberMask(key: string, w: number, h: number, bits: Uint8Array): Uint8Array;
87
+ /** The print-mask file's format tag (Snowcone's catalog: `mockups[].printMask`). */
88
+ declare const PRINT_MASK_FORMAT = "sc-print-mask/1";
89
+ /** A mask on a grid (1 = the part that changes). */
90
+ interface GridMask {
91
+ w: number;
92
+ h: number;
93
+ bits: Uint8Array;
94
+ }
95
+ /**
96
+ * What became of a mask URL: `mask` (read), `absent` (404: not made yet, learn from the frames), or an error a host
97
+ * can act on: `E_LOADER_MASK_FETCH` (no answer, or an answer other than 200 / 404) and `E_LOADER_MASK_INVALID` (not
98
+ * an `sc-print-mask/1` file), each with what to fix.
99
+ */
100
+ type MaskLoad = {
101
+ state: "mask";
102
+ mask: GridMask;
103
+ } | {
104
+ state: "absent";
105
+ } | {
106
+ state: "error";
107
+ code: "E_LOADER_MASK_FETCH" | "E_LOADER_MASK_INVALID";
108
+ message: string;
109
+ };
110
+ /**
111
+ * Reads an `sc-print-mask/1` file: `{ format, w, h, bits }`, `bits` = {@link packBits} of `w × h` cells. Its grid is
112
+ * the beam's: the long side {@link LEARN_SIDE} cells, so it lays over the whole picture it was made from.
113
+ */
114
+ declare function parsePrintMask(json: unknown): MaskLoad;
115
+ /**
116
+ * Fetches a mask file once per URL per page (the promise is shared; the HTTP cache keeps it across visits: a mask URL
117
+ * is immutable). A 404 is `absent`, never an error: the mask is not made yet.
118
+ */
119
+ declare function loadPrintMask(url: string): Promise<MaskLoad>;
120
+ /** A mask URL's answer if it has already come back on this page (a loader starts on it at once), else `undefined`. */
121
+ declare function peekPrintMask(url: string): MaskLoad | undefined;
122
+ /** Whether a `w × h` grid and a picture of `pw × ph` have the same shape (within 2 %): a mask of the whole picture. */
123
+ declare function sameShape(w: number, h: number, pw: number, ph: number): boolean;
124
+ /** A grid's bits read onto another grid of the same shape (nearest cell). */
125
+ declare function resampleBits(bits: Uint8Array, w: number, h: number, W: number, H: number): Uint8Array;
87
126
  /** Forgets everything learned (tests; a "reset" in a demo). */
88
127
  declare function forgetMasks(): void;
89
128
 
90
- export { CHANGE_THRESHOLD, LEARN_SIDE, MAX_STORED_MASKS, type Snapshot, UNRELIABLE_COVERAGE, alphaBox, alphaCanvas, changedBits, coverageOf, forgetMasks, gridSize, isLearnable, meanColour, meanLuma, orBits, packBits, recallMask, rememberMask, snapshotPicture, softAlpha, unpackBits };
129
+ export { CHANGE_THRESHOLD, type GridMask, LEARN_SIDE, MAX_STORED_MASKS, type MaskLoad, PRINT_MASK_FORMAT, type Snapshot, UNRELIABLE_COVERAGE, alphaBox, alphaCanvas, changedBits, coverageOf, forgetMasks, gridSize, isLearnable, loadPrintMask, meanColour, meanLuma, orBits, packBits, parsePrintMask, peekPrintMask, recallMask, rememberMask, resampleBits, sameShape, snapshotPicture, softAlpha, unpackBits };
package/dist/mask.d.ts CHANGED
@@ -84,7 +84,46 @@ declare function unpackBits(packed: string, n: number): Uint8Array | null;
84
84
  declare function recallMask(key: string, w: number, h: number): Uint8Array | null;
85
85
  /** Adds what changed this time to what a scene already knows; returns the union. */
86
86
  declare function rememberMask(key: string, w: number, h: number, bits: Uint8Array): Uint8Array;
87
+ /** The print-mask file's format tag (Snowcone's catalog: `mockups[].printMask`). */
88
+ declare const PRINT_MASK_FORMAT = "sc-print-mask/1";
89
+ /** A mask on a grid (1 = the part that changes). */
90
+ interface GridMask {
91
+ w: number;
92
+ h: number;
93
+ bits: Uint8Array;
94
+ }
95
+ /**
96
+ * What became of a mask URL: `mask` (read), `absent` (404: not made yet, learn from the frames), or an error a host
97
+ * can act on: `E_LOADER_MASK_FETCH` (no answer, or an answer other than 200 / 404) and `E_LOADER_MASK_INVALID` (not
98
+ * an `sc-print-mask/1` file), each with what to fix.
99
+ */
100
+ type MaskLoad = {
101
+ state: "mask";
102
+ mask: GridMask;
103
+ } | {
104
+ state: "absent";
105
+ } | {
106
+ state: "error";
107
+ code: "E_LOADER_MASK_FETCH" | "E_LOADER_MASK_INVALID";
108
+ message: string;
109
+ };
110
+ /**
111
+ * Reads an `sc-print-mask/1` file: `{ format, w, h, bits }`, `bits` = {@link packBits} of `w × h` cells. Its grid is
112
+ * the beam's: the long side {@link LEARN_SIDE} cells, so it lays over the whole picture it was made from.
113
+ */
114
+ declare function parsePrintMask(json: unknown): MaskLoad;
115
+ /**
116
+ * Fetches a mask file once per URL per page (the promise is shared; the HTTP cache keeps it across visits: a mask URL
117
+ * is immutable). A 404 is `absent`, never an error: the mask is not made yet.
118
+ */
119
+ declare function loadPrintMask(url: string): Promise<MaskLoad>;
120
+ /** A mask URL's answer if it has already come back on this page (a loader starts on it at once), else `undefined`. */
121
+ declare function peekPrintMask(url: string): MaskLoad | undefined;
122
+ /** Whether a `w × h` grid and a picture of `pw × ph` have the same shape (within 2 %): a mask of the whole picture. */
123
+ declare function sameShape(w: number, h: number, pw: number, ph: number): boolean;
124
+ /** A grid's bits read onto another grid of the same shape (nearest cell). */
125
+ declare function resampleBits(bits: Uint8Array, w: number, h: number, W: number, H: number): Uint8Array;
87
126
  /** Forgets everything learned (tests; a "reset" in a demo). */
88
127
  declare function forgetMasks(): void;
89
128
 
90
- export { CHANGE_THRESHOLD, LEARN_SIDE, MAX_STORED_MASKS, type Snapshot, UNRELIABLE_COVERAGE, alphaBox, alphaCanvas, changedBits, coverageOf, forgetMasks, gridSize, isLearnable, meanColour, meanLuma, orBits, packBits, recallMask, rememberMask, snapshotPicture, softAlpha, unpackBits };
129
+ export { CHANGE_THRESHOLD, type GridMask, LEARN_SIDE, MAX_STORED_MASKS, type MaskLoad, PRINT_MASK_FORMAT, type Snapshot, UNRELIABLE_COVERAGE, alphaBox, alphaCanvas, changedBits, coverageOf, forgetMasks, gridSize, isLearnable, loadPrintMask, meanColour, meanLuma, orBits, packBits, parsePrintMask, peekPrintMask, recallMask, rememberMask, resampleBits, sameShape, snapshotPicture, softAlpha, unpackBits };
package/dist/mask.js CHANGED
@@ -1,4 +1,4 @@
1
- export { CHANGE_THRESHOLD, LEARN_SIDE, MAX_STORED_MASKS, UNRELIABLE_COVERAGE, alphaBox, alphaCanvas, changedBits, coverageOf, forgetMasks, gridSize, isLearnable, meanColour, meanLuma, orBits, packBits, recallMask, rememberMask, snapshotPicture, softAlpha, unpackBits } from './chunk-JJVBRZTY.js';
2
- import './chunk-6IWUTOBS.js';
1
+ export { CHANGE_THRESHOLD, LEARN_SIDE, MAX_STORED_MASKS, PRINT_MASK_FORMAT, UNRELIABLE_COVERAGE, alphaBox, alphaCanvas, changedBits, coverageOf, forgetMasks, gridSize, isLearnable, loadPrintMask, meanColour, meanLuma, orBits, packBits, parsePrintMask, peekPrintMask, recallMask, rememberMask, resampleBits, sameShape, snapshotPicture, softAlpha, unpackBits } from './chunk-PY3IOI5B.js';
2
+ import './chunk-33ZDGDZM.js';
3
3
  //# sourceMappingURL=mask.js.map
4
4
  //# sourceMappingURL=mask.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowcone-app/loaders",
3
- "version": "0.3.4",
3
+ "version": "0.4.0",
4
4
  "description": "Render-in-flight loaders for live product mockups. Each loader is its own import (`@snowcone-app/loaders/beam`): a site downloads only the loaders it uses.",
5
5
  "keywords": [
6
6
  "snowcone",
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/mask.ts"],"names":["naturalSize"],"mappings":";;;;;AAcO,IAAM,UAAA,GAAa;AAEnB,IAAM,gBAAA,GAAmB;AAKzB,IAAM,mBAAA,GAAsB;AAU5B,SAAS,QAAA,CAAS,IAAA,EAAc,IAAA,EAAc,IAAA,GAAO,UAAA,EAAsC;AAChG,EAAA,IAAI,EAAE,IAAA,GAAO,CAAA,CAAA,IAAM,EAAE,IAAA,GAAO,CAAA,CAAA,EAAI,OAAO,EAAE,CAAA,EAAG,CAAA,EAAG,CAAA,EAAG,CAAA,EAAE;AACpD,EAAA,OAAO,IAAA,IAAQ,IAAA,GACX,EAAE,CAAA,EAAG,IAAA,EAAM,CAAA,EAAG,IAAA,CAAK,GAAA,CAAI,CAAA,EAAG,IAAA,CAAK,KAAA,CAAO,IAAA,GAAO,OAAQ,IAAI,CAAC,CAAA,EAAE,GAC5D,EAAE,CAAA,EAAG,IAAA,CAAK,GAAA,CAAI,CAAA,EAAG,IAAA,CAAK,KAAA,CAAO,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AI,EAAA,EAAI;AACN,IAAA,IAAI;AACF,MAAA,EAAA,CAAG,OAAA,CAAQ,SAAA,GAAY,CAAA,EAAG,QAAA,CAAS,IAAI,CAAC,CAAA;AACxC,MAAA,MAAM,KAAA,GAAS,IAAA,CAAK,KAAA,CAAM,EAAA,CAAG,OAAA,CAAQ,QAAQ,CAAA,IAAK,IAAI,CAAA,CAAe,MAAA,CAAO,CAAC,CAAA,KAAM,MAAM,CAAC,CAAA;AAC1F,MAAA,KAAA,CAAM,KAAK,CAAC,CAAA;AACZ,MAAA,OAAO,KAAA,CAAM,SAAS,gBAAA,EAAkB,EAAA,CAAG,WAAW,SAAA,GAAY,KAAA,CAAM,OAAQ,CAAA;AAChF,MAAA,EAAA,CAAG,OAAA,CAAQ,QAAA,EAAU,IAAA,CAAK,SAAA,CAAU,KAAK,CAAC,CAAA;AAAA,IAC5C,CAAA,CAAA,MAAQ;AAAA,IAER;AAAA,EACF;AACA,EAAA,OAAO,IAAA;AACT;AAGO,SAAS,WAAA,GAAoB;AAClC,EAAA,MAAA,CAAO,KAAA,EAAM;AACb,EAAA,MAAM,KAAK,OAAA,EAAQ;AACnB,EAAA,IAAI,CAAC,EAAA,EAAI;AACT,EAAA,IAAI;AACF,IAAA,MAAM,QAAQ,IAAA,CAAK,KAAA,CAAM,GAAG,OAAA,CAAQ,QAAQ,KAAK,IAAI,CAAA;AACrD,IAAA,KAAA,MAAW,CAAA,IAAK,KAAA,EAAO,EAAA,CAAG,UAAA,CAAW,YAAY,CAAC,CAAA;AAClD,IAAA,EAAA,CAAG,WAAW,QAAQ,CAAA;AAAA,EACxB,CAAA,CAAA,MAAQ;AAAA,EAER;AACF","file":"chunk-66FMKFHB.cjs","sourcesContent":["/**\n * Learned masks: where a product picture changes, measured from the picture itself.\n *\n * A loader snapshots the picture when a render starts, then compares each new frame with it. The pixels that\n * differ are \"what changed\" (the print, the colored body), so the loader can stay on that part instead of the\n * whole frame. What it learns is kept per scene (a mockup id) in memory and, packed small, in localStorage, so\n * the next edit of the same picture knows the area at once.\n *\n * Everything here works on small grids (at most {@link LEARN_SIDE} px on a side): a comparison is a few\n * thousand pixel reads, cheap enough to run several times a second while a render is on the way.\n */\nimport { naturalSize, type PictureElement } from \"./index\";\n\n/** The long side of the grid a picture is compared on. */\nexport const LEARN_SIDE = 192;\n/** A channel difference (0–255, after a light blur) above this is a change, not compression noise. */\nexport const CHANGE_THRESHOLD = 30;\n/**\n * A comparison that finds more than this share of the picture changed is not trusted: it is another picture (a\n * different crop, a first live frame against a differently encoded store picture), not an edit of this one.\n */\nexport const UNRELIABLE_COVERAGE = 0.6;\n\n/** A picture read down to a small grid. */\nexport interface Snapshot {\n w: number;\n h: number;\n rgba: Uint8ClampedArray;\n}\n\n/** The grid size for a picture of `srcW × srcH`: the long side is `side`, the aspect kept. */\nexport function gridSize(srcW: number, srcH: number, side = LEARN_SIDE): { w: number; h: number } {\n if (!(srcW > 0) || !(srcH > 0)) return { w: 0, h: 0 };\n return srcW >= srcH\n ? { w: side, h: Math.max(1, Math.round((side * srcH) / srcW)) }\n : { w: Math.max(1, Math.round((side * srcW) / srcH)), h: side };\n}\n\nlet scratch: HTMLCanvasElement | null = null;\n/**\n * Reads a picture down to its comparison grid. `null` when it cannot be read: not loaded yet, no 2D canvas, or a\n * cross-origin picture served without CORS (the canvas is tainted). A loader then just covers the whole frame.\n */\nexport function snapshotPicture(el: PictureElement | null | undefined, side = LEARN_SIDE): Snapshot | null {\n if (!el || typeof document === \"undefined\") return null;\n if (typeof HTMLImageElement !== \"undefined\" && el instanceof HTMLImageElement && !el.complete) return null;\n const { w: sw, h: sh } = naturalSize(el);\n const { w, h } = gridSize(sw, sh, side);\n if (!w || !h) return null;\n try {\n scratch ??= document.createElement(\"canvas\");\n scratch.width = w;\n scratch.height = h;\n const ctx = scratch.getContext(\"2d\", { willReadFrequently: true });\n if (!ctx) return null;\n ctx.clearRect(0, 0, w, h);\n ctx.drawImage(el as CanvasImageSource, 0, 0, w, h);\n return { w, h, rgba: ctx.getImageData(0, 0, w, h).data };\n } catch {\n return null; // tainted (no CORS) or a decode error: nothing to learn from\n }\n}\n\n/** A 3 × 3 box blur of each color channel: compression speckle averages out before pixels are compared. */\nfunction blurred(s: Snapshot): Float32Array {\n const { w, h, rgba } = s;\n const out = new Float32Array(w * h * 3);\n for (let y = 0; y < h; y++) {\n for (let x = 0; x < w; x++) {\n let r = 0, g = 0, b = 0, n = 0;\n for (let dy = -1; dy <= 1; dy++) {\n const yy = y + dy;\n if (yy < 0 || yy >= h) continue;\n for (let dx = -1; dx <= 1; dx++) {\n const xx = x + dx;\n if (xx < 0 || xx >= w) continue;\n const i = (yy * w + xx) * 4;\n r += rgba[i]; g += rgba[i + 1]; b += rgba[i + 2]; n++;\n }\n }\n const o = (y * w + x) * 3;\n out[o] = r / n; out[o + 1] = g / n; out[o + 2] = b / n;\n }\n }\n return out;\n}\n\n/**\n * Which grid cells changed between two snapshots of the same picture (1 = changed). `null` when they are not the\n * same grid (another picture, another aspect): there is nothing to compare.\n */\nexport function changedBits(a: Snapshot, b: Snapshot, threshold = CHANGE_THRESHOLD): Uint8Array | null {\n if (a.w !== b.w || a.h !== b.h || a.rgba.length !== b.rgba.length) return null;\n const A = blurred(a), B = blurred(b);\n const bits = new Uint8Array(a.w * a.h);\n for (let i = 0; i < bits.length; i++) {\n const o = i * 3;\n const d = Math.max(Math.abs(A[o] - B[o]), Math.abs(A[o + 1] - B[o + 1]), Math.abs(A[o + 2] - B[o + 2]));\n bits[i] = d > threshold ? 1 : 0;\n }\n return bits;\n}\n\n/** The share of cells set. */\nexport function coverageOf(bits: Uint8Array): number {\n let n = 0;\n for (let i = 0; i < bits.length; i++) n += bits[i];\n return bits.length ? n / bits.length : 0;\n}\n\n/** Whether a comparison is worth learning from: something changed, and not so much that it is another picture. */\nexport function isLearnable(bits: Uint8Array | null): bits is Uint8Array {\n if (!bits) return false;\n const c = coverageOf(bits);\n return c > 0 && c <= UNRELIABLE_COVERAGE;\n}\n\n/** Cells set in either. */\nexport function orBits(a: Uint8Array, b: Uint8Array): Uint8Array {\n const out = new Uint8Array(Math.max(a.length, b.length));\n for (let i = 0; i < out.length; i++) out[i] = (a[i] | b[i]) as 0 | 1;\n return out;\n}\n\nfunction boxPass(src: Float32Array, w: number, h: number, r: number, horizontal: boolean, op: \"max\" | \"mean\"): Float32Array {\n const out = new Float32Array(src.length);\n const len = horizontal ? w : h, lines = horizontal ? h : w;\n for (let l = 0; l < lines; l++) {\n for (let i = 0; i < len; i++) {\n let acc = op === \"max\" ? 0 : 0, n = 0;\n for (let k = -r; k <= r; k++) {\n const j = i + k;\n if (j < 0 || j >= len) continue;\n const v = src[horizontal ? l * w + j : j * w + l];\n if (op === \"max\") { if (v > acc) acc = v; } else { acc += v; n++; }\n }\n out[horizontal ? l * w + i : i * w + l] = op === \"max\" ? acc : acc / n;\n }\n }\n return out;\n}\n\n/**\n * A soft alpha (0–1) from changed cells: grown by `grow` cells (a stroke of a letter becomes the letter's area),\n * then blurred by `blur` cells, so the loader's edge fades out instead of stopping at a hard line. The inside stays\n * fully opaque.\n */\nexport function softAlpha(bits: Uint8Array, w: number, h: number, opts: { grow?: number; blur?: number } = {}): Float32Array {\n const grow = Math.max(0, Math.round(opts.grow ?? 2));\n const blur = Math.max(0, Math.round(opts.blur ?? 4));\n let a: Float32Array = Float32Array.from(bits);\n if (grow) a = boxPass(boxPass(a, w, h, grow, true, \"max\"), w, h, grow, false, \"max\");\n const r = Math.max(1, Math.round(blur / 2));\n if (blur) for (let p = 0; p < 3; p++) a = boxPass(boxPass(a, w, h, r, true, \"mean\"), w, h, r, false, \"mean\");\n for (let i = 0; i < a.length; i++) a[i] = Math.min(1, a[i] * 1.6);\n return a;\n}\n\n/** The box (0–1, picture space) holding every cell above `threshold`, or `null` when there is none. */\nexport function alphaBox(alpha: Float32Array, w: number, h: number, threshold = 0.2): { x: number; y: number; w: number; h: number } | null {\n let x0 = w, y0 = h, x1 = -1, y1 = -1;\n for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {\n if (alpha[y * w + x] <= threshold) continue;\n if (x < x0) x0 = x; if (x > x1) x1 = x; if (y < y0) y0 = y; if (y > y1) y1 = y;\n }\n if (x1 < 0) return null;\n return { x: x0 / w, y: y0 / h, w: (x1 - x0 + 1) / w, h: (y1 - y0 + 1) / h };\n}\n\n/** A soft alpha as a canvas (white, alpha = mask), ready to `drawImage` over a frame. `null` without a 2D canvas. */\nexport function alphaCanvas(alpha: Float32Array, w: number, h: number): HTMLCanvasElement | null {\n if (typeof document === \"undefined\") return null;\n const c = document.createElement(\"canvas\");\n c.width = w;\n c.height = h;\n const ctx = c.getContext(\"2d\");\n if (!ctx) return null;\n const img = ctx.createImageData(w, h);\n for (let i = 0; i < alpha.length; i++) {\n const o = i * 4;\n img.data[o] = img.data[o + 1] = img.data[o + 2] = 255;\n img.data[o + 3] = Math.round(alpha[i] * 255);\n }\n ctx.putImageData(img, 0, 0);\n return c;\n}\n\n/** The mean color of the cells set (from a snapshot), or `null` when none is. A loader tints its light with it. */\nexport function meanColour(s: Snapshot, bits: Uint8Array): [number, number, number] | null {\n let r = 0, g = 0, b = 0, n = 0;\n for (let i = 0; i < bits.length; i++) {\n if (!bits[i]) continue;\n const o = i * 4;\n r += s.rgba[o]; g += s.rgba[o + 1]; b += s.rgba[o + 2]; n++;\n }\n return n ? [r / n, g / n, b / n] : null;\n}\n\n/**\n * The mean brightness (0–1) of a snapshot, only where `alpha` is above `threshold` when given. A loader uses it to\n * know whether light can show on the surface (on white it can't).\n */\nexport function meanLuma(s: Snapshot, alpha?: Float32Array | null, threshold = 0.2): number {\n let sum = 0, n = 0;\n for (let i = 0; i < s.w * s.h; i++) {\n if (alpha && alpha[i] <= threshold) continue;\n const o = i * 4;\n sum += 0.2126 * s.rgba[o] + 0.7152 * s.rgba[o + 1] + 0.0722 * s.rgba[o + 2];\n n++;\n }\n return n ? sum / n / 255 : 0.5;\n}\n\n// ---------- what was learned, per scene ----------\n\n// v2 (2026-12-01): v1 masks could hold a whole cup learned across a color switch by the first beam (#6723), and a\n// remembered area is kept for good: every v1 entry is dropped once, on first use.\nconst LS_PREFIX = \"sc-loaders:mask:v2:\";\nconst LS_INDEX = \"sc-loaders:mask:v2:index\";\nconst RETIRED_PREFIXES = [\"sc-loaders:mask:v1:\"];\nlet retiredSwept = false;\nfunction sweepRetired(ls: Storage): void {\n if (retiredSwept) return;\n retiredSwept = true;\n try {\n for (let i = ls.length - 1; i >= 0; i--) {\n const k = ls.key(i);\n if (k && RETIRED_PREFIXES.some((p) => k.startsWith(p))) ls.removeItem(k);\n }\n } catch {\n // blocked storage: nothing to sweep\n }\n}\n/** At most this many scenes are kept in localStorage (oldest out first); a scene is ~1 KB. */\nexport const MAX_STORED_MASKS = 40;\nconst memory = new Map<string, Uint8Array>();\n\nconst slot = (key: string, w: number, h: number) => `${key}@${w}x${h}`;\n\n/** Bits to base64, 8 cells a byte. */\nexport function packBits(bits: Uint8Array): string {\n const bytes = new Uint8Array(Math.ceil(bits.length / 8));\n for (let i = 0; i < bits.length; i++) if (bits[i]) bytes[i >> 3] |= 1 << (i & 7);\n let s = \"\";\n for (let i = 0; i < bytes.length; i++) s += String.fromCharCode(bytes[i]);\n return btoa(s);\n}\n\n/** base64 back to `n` bits; `null` if it does not hold `n`. */\nexport function unpackBits(packed: string, n: number): Uint8Array | null {\n let s: string;\n try { s = atob(packed); } catch { return null; }\n if (s.length !== Math.ceil(n / 8)) return null;\n const bits = new Uint8Array(n);\n for (let i = 0; i < n; i++) bits[i] = (s.charCodeAt(i >> 3) >> (i & 7)) & 1;\n return bits;\n}\n\nfunction storage(): Storage | null {\n let ls: Storage | null = null;\n try { ls = typeof localStorage === \"undefined\" ? null : localStorage; } catch { return null; }\n if (ls) sweepRetired(ls);\n return ls;\n}\n\n/** What has been learned about a scene's picture on this grid, or `null`. */\nexport function recallMask(key: string, w: number, h: number): Uint8Array | null {\n const k = slot(key, w, h);\n const hit = memory.get(k);\n if (hit) return hit;\n const raw = storage()?.getItem(LS_PREFIX + k);\n const bits = raw ? unpackBits(raw, w * h) : null;\n if (bits) memory.set(k, bits);\n return bits;\n}\n\n/** Adds what changed this time to what a scene already knows; returns the union. */\nexport function rememberMask(key: string, w: number, h: number, bits: Uint8Array): Uint8Array {\n const k = slot(key, w, h);\n const prev = recallMask(key, w, h);\n const next = prev ? orBits(prev, bits) : bits;\n memory.set(k, next);\n const ls = storage();\n if (ls) {\n try {\n ls.setItem(LS_PREFIX + k, packBits(next));\n const index = (JSON.parse(ls.getItem(LS_INDEX) || \"[]\") as string[]).filter((x) => x !== k);\n index.push(k);\n while (index.length > MAX_STORED_MASKS) ls.removeItem(LS_PREFIX + index.shift()!);\n ls.setItem(LS_INDEX, JSON.stringify(index));\n } catch {\n // full or blocked storage: memory still has it for this visit\n }\n }\n return next;\n}\n\n/** Forgets everything learned (tests; a \"reset\" in a demo). */\nexport function forgetMasks(): void {\n memory.clear();\n const ls = storage();\n if (!ls) return;\n try {\n const index = JSON.parse(ls.getItem(LS_INDEX) || \"[]\") as string[];\n for (const k of index) ls.removeItem(LS_PREFIX + k);\n ls.removeItem(LS_INDEX);\n } catch {\n // nothing to clear\n }\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AA4DO,SAAS,QAAQ,QAAA,EAAkB,QAAA,EAAkB,IAAA,EAAc,IAAA,EAAc,MAAkB,OAAA,EAAe;AACvH,EAAA,IAAI,EAAE,WAAW,CAAA,CAAA,IAAM,EAAE,WAAW,CAAA,CAAA,IAAM,EAAE,OAAO,CAAA,CAAA,IAAM,EAAE,OAAO,CAAA,CAAA,EAAI,OAAO,EAAE,CAAA,EAAG,CAAA,EAAG,GAAG,CAAA,EAAG,CAAA,EAAG,IAAA,EAAM,CAAA,EAAG,IAAA,EAAK;AAC5G,EAAA,MAAM,CAAA,GAAI,GAAA,KAAQ,OAAA,GAAU,IAAA,CAAK,IAAI,IAAA,GAAO,QAAA,EAAU,IAAA,GAAO,QAAQ,IAAI,IAAA,CAAK,GAAA,CAAI,IAAA,GAAO,QAAA,EAAU,OAAO,QAAQ,CAAA;AAClH,EAAA,MAAM,IAAI,QAAA,GAAW,CAAA;AACrB,EAAA,MAAM,IAAI,QAAA,GAAW,CAAA;AACrB,EAAA,OAAO,EAAE,CAAA,EAAA,CAAI,IAAA,GAAO,CAAA,IAAK,CAAA,EAAG,IAAI,IAAA,GAAO,CAAA,IAAK,CAAA,EAAG,CAAA,EAAG,CAAA,EAAE;AACtD;AAGO,SAAS,YAAY,EAAA,EAA8C;AACxE,EAAA,IAAI,OAAO,gBAAA,KAAqB,WAAA,IAAe,EAAA,YAAc,gBAAA,EAAkB,OAAO,EAAE,CAAA,EAAG,EAAA,CAAG,YAAA,EAAc,CAAA,EAAG,EAAA,CAAG,aAAA,EAAc;AAChI,EAAA,OAAO,EAAE,CAAA,EAAI,EAAA,CAAuC,KAAA,EAAO,CAAA,EAAI,GAAuC,MAAA,EAAO;AAC/G","file":"chunk-6IWUTOBS.js","sourcesContent":["/**\n * @snowcone-app/loaders — the loader contract.\n *\n * A loader paints OVER a live product picture while its next render is on the way, and goes away once the picture\n * has caught up. This entry has no loader in it on purpose: each loader is its own import\n * (`@snowcone-app/loaders/beam`), so a site only downloads the loaders it uses. Write your own against\n * {@link LoaderProps} and any Snowcone surface can host it.\n */\n\n/** How the picture fills its frame: the `object-fit` of the `<img>` the loader paints over. */\nexport type PictureFit = \"cover\" | \"contain\";\n\n/** A picture element a loader can read: an `<img>`, a `<canvas>`, an `ImageBitmap`. */\nexport type PictureElement = HTMLImageElement | HTMLCanvasElement | ImageBitmap;\n\n/** What every Snowcone loader takes from its host. */\nexport interface LoaderProps {\n /**\n * `true` while the picture is behind (a render is in flight). Going `false` means caught up: the loader plays its\n * exit, then calls {@link LoaderProps.onExited}. Going `true` again before that turns it back, never a fresh start.\n */\n visible: boolean;\n /** Called once the exit has finished: unmount the loader then. */\n onExited?: () => void;\n /**\n * The picture on screen now (the topmost painted `<img>`). Called as often as the loader likes, so return the\n * current element, not a cached one. Lets a loader see what changes; without it the loader covers the whole frame.\n */\n picture?: () => PictureElement | null;\n /** A stable id of the scene (a mockup id). Whatever a loader learns about the picture is kept per key. */\n maskKey?: string;\n /** The `object-fit` of the picture's `<img>`. Defaults to `\"cover\"`. */\n fit?: PictureFit;\n /**\n * A known mask of the part that changes (alpha = changes), in the picture's own pixel space. Wins over anything\n * learned. Leave it out and loaders that need one learn it from the frames.\n */\n mask?: PictureElement | null;\n className?: string;\n style?: import(\"react\").CSSProperties;\n}\n\n/** Where a loader is in its life, stamped as `data-sc-loader-phase` (agents assert on it, never on pixels). */\nexport type LoaderPhase = \"loading\" | \"finishing\";\n\n/** Where the loader's area came from, stamped as `data-sc-loader-mask`. */\nexport type LoaderMaskSource = \"whole\" | \"learned\" | \"given\";\n\n/** A rectangle in frame pixels. */\nexport interface Rect {\n x: number;\n y: number;\n w: number;\n h: number;\n}\n\n/**\n * Where a picture of `naturalW × naturalH` lands in a `boxW × boxH` frame under `object-fit` (centred, like the\n * browser's default `object-position`). Loaders use it to lay anything in picture space (a mask) over the frame.\n */\nexport function fitRect(naturalW: number, naturalH: number, boxW: number, boxH: number, fit: PictureFit = \"cover\"): Rect {\n if (!(naturalW > 0) || !(naturalH > 0) || !(boxW > 0) || !(boxH > 0)) return { x: 0, y: 0, w: boxW, h: boxH };\n const s = fit === \"cover\" ? Math.max(boxW / naturalW, boxH / naturalH) : Math.min(boxW / naturalW, boxH / naturalH);\n const w = naturalW * s;\n const h = naturalH * s;\n return { x: (boxW - w) / 2, y: (boxH - h) / 2, w, h };\n}\n\n/** The natural size of a picture element (0 × 0 while an `<img>` has not loaded). */\nexport function naturalSize(el: PictureElement): { w: number; h: number } {\n if (typeof HTMLImageElement !== \"undefined\" && el instanceof HTMLImageElement) return { w: el.naturalWidth, h: el.naturalHeight };\n return { w: (el as HTMLCanvasElement | ImageBitmap).width, h: (el as HTMLCanvasElement | ImageBitmap).height };\n}\n"]}