@weasel-js/hud 1.4.0 → 1.4.2

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.
package/dist/host.d.ts CHANGED
@@ -1,9 +1,14 @@
1
1
  import type { RenderLayer } from '@weasel-js/core';
2
2
  /** What a HUD needs from whatever is hosting it: somewhere to register its
3
- * render layer, and a way to ask for a repaint. `attachHud` supplies this
4
- * from a canvas; a test or a headless renderer can supply its own. */
3
+ * render layer, a way to ask for a repaint, and a way to be told when one
4
+ * landed. `attachHud` supplies this from a canvas; a test or a headless
5
+ * renderer can supply its own. */
5
6
  export interface HudHost {
6
7
  requestRedraw(): void;
7
8
  registerLayer(layer: RenderLayer<unknown>): () => void;
9
+ /** Run `fn` after every paint, on the frame that painted. Chrome that reads
10
+ * back pixels has to sample here: anywhere else the drawing buffer still
11
+ * holds the previous frame. Returns an unsubscribe. */
12
+ subscribeFrame(fn: () => void): () => void;
8
13
  }
9
14
  //# sourceMappingURL=host.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../src/host.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEnD;;uEAEuE;AACvE,MAAM,WAAW,OAAO;IACtB,aAAa,IAAI,IAAI,CAAC;IACtB,aAAa,CAAC,KAAK,EAAE,WAAW,CAAC,OAAO,CAAC,GAAG,MAAM,IAAI,CAAC;CACxD"}
1
+ {"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../src/host.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEnD;;;mCAGmC;AACnC,MAAM,WAAW,OAAO;IACtB,aAAa,IAAI,IAAI,CAAC;IACtB,aAAa,CAAC,KAAK,EAAE,WAAW,CAAC,OAAO,CAAC,GAAG,MAAM,IAAI,CAAC;IACvD;;4DAEwD;IACxD,cAAc,CAAC,EAAE,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC;CAC5C"}
package/dist/hud.d.ts CHANGED
@@ -22,6 +22,10 @@ export interface Hud {
22
22
  markDirty(): void;
23
23
  bind(host: HudHost): void;
24
24
  unbind(): void;
25
+ /** Run `fn` after every paint the host lands. Survives bind/unbind: a
26
+ * subscription taken before the HUD is bound starts firing when it is.
27
+ * Returns an unsubscribe. */
28
+ subscribeFrame(fn: () => void): () => void;
25
29
  /** True after bind() and before unbind(). */
26
30
  readonly attached: boolean;
27
31
  /** Create a rect widget, add it to the HUD, and wire onChange → markDirty. */
package/dist/hud.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"hud.d.ts","sourceRoot":"","sources":["../src/hud.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AACvC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAc,KAAK,WAAW,EAAE,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC/E,OAAO,EAAc,KAAK,WAAW,EAAE,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC/E,OAAO,EAAe,KAAK,YAAY,EAAE,KAAK,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACnF,OAAO,EAAe,KAAK,YAAY,EAAE,KAAK,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACnF,OAAO,EAAgB,KAAK,aAAa,EAAE,KAAK,YAAY,EAAE,MAAM,kBAAkB,CAAC;AACvF,OAAO,EAAgB,KAAK,aAAa,EAAE,KAAK,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAE9F;;;;;;;;GAQG;AACH,MAAM,WAAW,GAAG;IAClB,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,OAAO,IAAI,SAAS,MAAM,EAAE,CAA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KAAK,aAAa,EAAE,KAAK,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAE9F;;;;;;;;GAQG;AACH,MAAM,WAAW,GAAG;IAClB,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,OAAO,IAAI,SAAS,MAAM,EAAE,CAAC;IAC7B,SAAS,IAAI,IAAI,CAAC;IAClB,IAAI,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,CAAC;IAC1B,MAAM,IAAI,IAAI,CAAC;IACf;;kCAE8B;IAC9B,cAAc,CAAC,EAAE,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC;IAC3C,6CAA6C;IAC7C,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAC3B,8EAA8E;IAC9E,IAAI,CAAC,IAAI,EAAE,WAAW,GAAG,UAAU,CAAC;IACpC,8EAA8E;IAC9E,IAAI,CAAC,IAAI,EAAE,WAAW,GAAG,UAAU,CAAC;IACpC,gFAAgF;IAChF,KAAK,CAAC,IAAI,EAAE,YAAY,GAAG,WAAW,CAAC;IACvC,+EAA+E;IAC/E,KAAK,CAAC,IAAI,EAAE,YAAY,GAAG,WAAW,CAAC;IACvC,gFAAgF;IAChF,MAAM,CAAC,IAAI,EAAE,aAAa,GAAG,YAAY,CAAC;IAC1C,gFAAgF;IAChF,MAAM,CAAC,IAAI,EAAE,aAAa,GAAG,YAAY,CAAC;CAC3C;AAED,oCAAoC;AACpC,wBAAgB,SAAS,IAAI,GAAG,CA8H/B"}
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, t as c, u as l } from "./tool-B6ZliNf3.js";
1
+ import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, t as c, u as l } from "./tool-sq7ioZyd.js";
2
2
  import { createViewportLayer as u, screenToWorld as d, viewToTransform as f } from "@weasel-js/core";
3
3
  import { createLoupeModel as p, loupeInnerView as m, loupeInnerView as h } from "@weasel-js/loupe";
4
4
  //#region src/loupe/readback.ts
@@ -23,13 +23,13 @@ function v(e) {
23
23
  y: 24,
24
24
  w: 220,
25
25
  h: 200
26
- }, _ = (e) => w.mode === "vector" ? y(e) : b(e), v = (t) => e.background ?? t.tokens["--wzl-surface"], y = (e) => {
27
- let t = d(w.aim.x, w.aim.y, f(e.view)), n = h({
26
+ }, _ = (e) => T.mode === "vector" ? y(e) : b(e), v = (t) => e.background ?? t.tokens["--wzl-surface"], y = (e) => {
27
+ let t = d(T.aim.x, T.aim.y, f(e.view)), n = h({
28
28
  x: t[0],
29
29
  y: t[1]
30
- }, e.view, e.rect, w.factor);
30
+ }, e.view, e.rect, T.factor);
31
31
  return u({
32
- id: `${C.id}:lens`,
32
+ id: `${w.id}:lens`,
33
33
  label: "Loupe content",
34
34
  source: r,
35
35
  view: n,
@@ -61,29 +61,27 @@ function v(e) {
61
61
  sampling: "nearest"
62
62
  }), t;
63
63
  }, x = () => {
64
- if (l || w.mode !== "pixel") return;
65
- if (s) {
66
- c = !0;
67
- return;
68
- }
64
+ l || T.mode !== "pixel" || (c = !0);
65
+ }, S = () => {
66
+ if (l || T.mode !== "pixel") return;
69
67
  let e = n.getContext("webgl2");
70
68
  if (!e) return;
71
69
  let t = n.getBoundingClientRect();
72
70
  if (t.width === 0) return;
73
- let r = n.width / t.width, a = C.contentRect, u = Math.max(1, Math.round(a.w * r / w.factor)), d = Math.max(1, Math.round(a.h * r / w.factor)), f = g(e, {
71
+ let r = n.width / t.width, a = w.contentRect, c = Math.max(1, Math.round(a.w * r / T.factor)), u = Math.max(1, Math.round(a.h * r / T.factor)), d = g(e, {
74
72
  width: n.width,
75
73
  height: n.height
76
- }, w.aim, r, u, d);
77
- s = !0, c = !1, createImageBitmap(f).then((e) => {
74
+ }, T.aim, r, c, u);
75
+ s = !0, createImageBitmap(d).then((e) => {
78
76
  if (l) {
79
77
  e.close();
80
78
  return;
81
79
  }
82
- o?.close(), o = e, s = !1, i(), c && x();
80
+ o?.close(), o = e, s = !1, i();
83
81
  }).catch(() => {
84
- s = !1, c && x();
82
+ s = !1, i();
85
83
  });
86
- }, S = (e) => {
84
+ }, C = (e) => {
87
85
  let t = n.getContext("webgl2");
88
86
  if (!t) return null;
89
87
  let r = n.getBoundingClientRect();
@@ -97,7 +95,7 @@ function v(e) {
97
95
  a.data[1],
98
96
  a.data[2]
99
97
  ].map((e) => e.toString(16).padStart(2, "0")).join("");
100
- }, C = t.window({
98
+ }, w = t.window({
101
99
  id: "weasel-loupe",
102
100
  x: m.x,
103
101
  y: m.y,
@@ -110,16 +108,16 @@ function v(e) {
110
108
  x();
111
109
  },
112
110
  onContentClick: (e) => {
113
- w.pick(e);
111
+ T.pick(e);
114
112
  },
115
113
  ...e.onClose ? { onClose: e.onClose } : {}
116
- }), w = p({
114
+ }), T = p({
117
115
  surface: {
118
- lens: () => C.contentRect,
119
- covers: (e) => C.hitTest(e.x, e.y),
120
- sample: S,
121
- hidden: () => C.hidden,
122
- gone: () => C.disposed === !0,
116
+ lens: () => w.contentRect,
117
+ covers: (e) => w.hitTest(e.x, e.y),
118
+ sample: C,
119
+ hidden: () => w.hidden,
120
+ gone: () => w.disposed === !0,
123
121
  changed: () => {
124
122
  x(), i();
125
123
  }
@@ -128,36 +126,38 @@ function v(e) {
128
126
  ...e.factor === void 0 ? {} : { factor: e.factor },
129
127
  ...e.onColorChange ? { onColorChange: e.onColorChange } : {},
130
128
  ...e.onPick ? { onPick: e.onPick } : {},
131
- onDispose: () => E()
132
- }), T = (e) => {
129
+ onDispose: () => O()
130
+ }), E = (e) => {
133
131
  let t = a.getBoundingClientRect();
134
- w.aimAt({
132
+ T.aimAt({
135
133
  x: e.clientX - t.left,
136
134
  y: e.clientY - t.top
137
135
  });
138
- }, E = () => {
139
- l = !0, a.removeEventListener("pointermove", T), o?.close(), o = null;
136
+ }, D = t.subscribeFrame(() => {
137
+ !c || s || (c = !1, S());
138
+ }), O = () => {
139
+ l = !0, D(), a.removeEventListener("pointermove", E), o?.close(), o = null;
140
140
  };
141
- return a.addEventListener("pointermove", T), {
142
- window: C,
141
+ return a.addEventListener("pointermove", E), {
142
+ window: w,
143
143
  get mode() {
144
- return w.mode;
144
+ return T.mode;
145
145
  },
146
146
  get factor() {
147
- return w.factor;
147
+ return T.factor;
148
148
  },
149
149
  get aim() {
150
- return w.aim;
150
+ return T.aim;
151
151
  },
152
152
  get color() {
153
- return w.color;
153
+ return T.color;
154
154
  },
155
- setMode: w.setMode,
156
- setFactor: w.setFactor,
157
- aimAt: w.aimAt,
158
- pick: w.pick,
155
+ setMode: T.setMode,
156
+ setFactor: T.setFactor,
157
+ aimAt: T.aimAt,
158
+ pick: T.pick,
159
159
  dispose() {
160
- w.dispose(), C.dispose();
160
+ T.dispose(), w.dispose();
161
161
  }
162
162
  };
163
163
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/loupe/readback.ts","../src/loupe/createLoupe.ts"],"sourcesContent":["/**\n * Read a `rw × rh` device-pixel region of the drawing buffer centered on\n * `pointer` (CSS px relative to the canvas), returned top-down.\n *\n * GL reports rows bottom-up; every consumer here wants top-down, so both the\n * read origin and the returned rows are flipped.\n */\nexport function readbackRegion(\n gl: WebGL2RenderingContext,\n buffer: { width: number; height: number },\n pointer: { x: number; y: number },\n dpr: number,\n rw: number,\n rh: number,\n): ImageData {\n const cx = Math.round(pointer.x * dpr);\n const cy = Math.round(pointer.y * dpr);\n const gx = clamp(cx - Math.floor(rw / 2), 0, Math.max(0, buffer.width - rw));\n const gyTop = clamp(cy - Math.floor(rh / 2), 0, Math.max(0, buffer.height - rh));\n const gy = buffer.height - gyTop - rh;\n\n const raw = new Uint8Array(rw * rh * 4);\n gl.readPixels(gx, gy, rw, rh, gl.RGBA, gl.UNSIGNED_BYTE, raw);\n\n const flipped = new Uint8ClampedArray(rw * rh * 4);\n const stride = rw * 4;\n for (let row = 0; row < rh; row++) {\n const src = (rh - 1 - row) * stride;\n flipped.set(raw.subarray(src, src + stride), row * stride);\n }\n return new ImageData(flipped, rw, rh);\n}\n\nfunction clamp(v: number, lo: number, hi: number): number {\n return v < lo ? lo : v > hi ? hi : v;\n}\n","import {\n createViewportLayer, screenToWorld, viewToTransform,\n type RenderLayer, type View,\n} from '@weasel-js/core';\nimport type { DrawCommand } from '@weasel-js/core/renderer';\nimport {\n createLoupeModel, loupeInnerView,\n type LoupeMode, type LoupeModel, type LoupeSurface,\n} from '@weasel-js/loupe';\nimport type { Hud } from '../hud';\nimport type { WindowWidget } from '../widgets/window/window';\nimport type { HudContentCtx, WidgetBounds } from '../widget';\nimport { readbackRegion } from './readback';\n\nexport type { LoupeMode };\n\n/** Options for `createLoupe`. */\nexport interface LoupeOptions {\n hud: Hud;\n /** The canvas pixel mode reads back from. Supplies the GL context and the\n * drawing-buffer dimensions. */\n canvas: HTMLCanvasElement;\n /** Where the aim comes from: the element pointer events are taken from and\n * the rect client coords are measured against. Defaults to `canvas`, which\n * is right whenever the canvas is also the thing under the pointer. */\n input?: HTMLElement;\n /** Layers re-rendered through the magnified inner view in vector mode. */\n source: RenderLayer<unknown>[];\n requestRedraw: () => void;\n mode?: LoupeMode;\n factor?: number;\n bounds?: WidgetBounds;\n title?: string;\n /** Draw the window's titlebar and close box. Default `true`. Pass `false`\n * for a bare lens; the interior then doubles as the move handle. */\n titlebar?: boolean;\n /** Called with the hex color under the aim point whenever it changes.\n * Sampled from the framebuffer in both modes — vector content is a\n * re-render whose edge colors differ from the screen's, so reading the\n * color off it would report something the user cannot see. */\n onColorChange?: (hex: string) => void;\n /** Fired when a click inside the lens picks a color — an eyedropper on the\n * magnified surface. Where the color goes is the consumer's: the loupe\n * reports the pick and nothing else. */\n onPick?: (hex: string) => void;\n /** Fired by the window's close box. The window does not hide itself —\n * whoever owns the loupe's visibility decides what closing means. */\n onClose?: () => void;\n /** Backdrop painted behind the magnified content in both modes, so the\n * outer canvas does not show through where the lens is empty. Defaults to\n * the theme's surface color. */\n background?: string;\n}\n\n/** Control surface for a live loupe: where it is aimed, how it magnifies,\n * what color it is over, and the window it lives in. */\nexport interface LoupeHandle {\n readonly window: WindowWidget;\n readonly mode: LoupeMode;\n readonly factor: number;\n /** Last aimed point, in screen-space CSS px relative to the canvas. */\n readonly aim: { x: number; y: number };\n /** Hex color under the aim point, read off the framebuffer. `null` before\n * the first aim, or when the GL context is unavailable. */\n readonly color: string | null;\n setMode(mode: LoupeMode): void;\n setFactor(factor: number): void;\n /** Aim at a screen point. Ignored while the point is over the window —\n * that is the freeze rule that makes the borders reachable. */\n aimAt(p: { x: number; y: number }): void;\n /** Sample the color the lens is showing at `p` — a screen point inside the\n * lens — and report it to `onPick`. Omit `p` to pick at the aim point.\n * Returns the hex, or `null` when the framebuffer cannot answer for that\n * point. Leaves the aim, and `color`, alone. */\n pick(p?: { x: number; y: number }): string | null;\n dispose(): void;\n}\n\n/**\n * Build a magnifier: a HUD window whose interior shows the canvas around an\n * aim point, enlarged. Adds the window to the HUD and starts following the\n * pointer; `dispose` removes it again.\n */\nexport function createLoupe(opts: LoupeOptions): LoupeHandle {\n const { hud, canvas: element, source, requestRedraw } = opts;\n const input = opts.input ?? element;\n let pixels: ImageBitmap | null = null;\n let pixelsPending = false;\n let pixelsStale = false;\n let disposed = false;\n\n const b = opts.bounds ?? { x: 24, y: 24, w: 220, h: 200 };\n\n const content = (ctx: HudContentCtx): DrawCommand[] =>\n model.mode === 'vector' ? drawVector(ctx) : drawPixels(ctx);\n\n const backdropColor = (ctx: HudContentCtx): string =>\n opts.background ?? ctx.tokens['--wzl-surface'];\n\n const drawVector = (ctx: HudContentCtx): DrawCommand[] => {\n const world = screenToWorld(model.aim.x, model.aim.y, viewToTransform(ctx.view));\n const inner: View = loupeInnerView(\n { x: world[0], y: world[1] }, ctx.view, ctx.rect, model.factor,\n );\n // A fresh lens per frame: CreateViewportLayerOpts.view is static, and the\n // inner view moves with the pointer.\n const lens = createViewportLayer<unknown>({\n id: `${win.id}:lens`,\n label: 'Loupe content',\n source,\n view: inner,\n bounds: () => ctx.rect,\n background: backdropColor(ctx),\n });\n return lens.draw(ctx.data, ctx.view, ctx.dims);\n };\n\n const drawPixels = (ctx: HudContentCtx): DrawCommand[] => {\n // The backdrop paints even with a bitmap in hand: a readback of a\n // transparent framebuffer is transparent, and before the first one\n // settles there is nothing at all, either of which leaves the lens a\n // hole onto the unmagnified canvas.\n const out: DrawCommand[] = [{\n kind: 'path',\n path: { kind: 'rect', x: ctx.rect.x, y: ctx.rect.y, width: ctx.rect.w, height: ctx.rect.h },\n fill: { fill: 'solid', color: backdropColor(ctx) },\n }];\n if (pixels) {\n out.push({\n kind: 'image',\n image: pixels,\n x: ctx.rect.x, y: ctx.rect.y, w: ctx.rect.w, h: ctx.rect.h,\n sampling: 'nearest',\n });\n }\n return out;\n };\n\n const refreshPixels = () => {\n if (disposed || model.mode !== 'pixel') return;\n // A readback requested mid-flight is remembered rather than dropped: the\n // aim that arrives during a fast drag is the one the user ends on.\n if (pixelsPending) { pixelsStale = true; return; }\n const gl = element.getContext('webgl2');\n if (!gl) return;\n const cssRect = element.getBoundingClientRect();\n if (cssRect.width === 0) return;\n const dpr = element.width / cssRect.width;\n const rect = win.contentRect;\n const rw = Math.max(1, Math.round((rect.w * dpr) / model.factor));\n const rh = Math.max(1, Math.round((rect.h * dpr) / model.factor));\n const data = readbackRegion(\n gl, { width: element.width, height: element.height }, model.aim, dpr, rw, rh,\n );\n pixelsPending = true;\n pixelsStale = false;\n createImageBitmap(data)\n .then((bmp) => {\n if (disposed) { bmp.close(); return; }\n pixels?.close();\n pixels = bmp;\n pixelsPending = false;\n requestRedraw();\n if (pixelsStale) refreshPixels();\n })\n .catch(() => {\n pixelsPending = false;\n if (pixelsStale) refreshPixels();\n });\n };\n\n const readHex = (at: { x: number; y: number }): string | null => {\n const gl = element.getContext('webgl2');\n if (!gl) return null;\n const cssRect = element.getBoundingClientRect();\n if (cssRect.width === 0) return null;\n const dpr = element.width / cssRect.width;\n const px = readbackRegion(\n gl, { width: element.width, height: element.height }, at, dpr, 1, 1,\n );\n return '#' + [px.data[0], px.data[1], px.data[2]]\n .map((c) => c.toString(16).padStart(2, '0')).join('');\n };\n\n const win = hud.window({\n id: 'weasel-loupe',\n x: b.x, y: b.y, w: b.w, h: b.h,\n title: opts.title ?? 'Loupe',\n titlebar: opts.titlebar,\n content,\n onResize: () => { refreshPixels(); },\n onContentClick: (p) => { model.pick(p); },\n ...(opts.onClose ? { onClose: opts.onClose } : {}),\n });\n\n const surface: LoupeSurface = {\n lens: () => win.contentRect,\n covers: (p) => win.hitTest(p.x, p.y),\n sample: readHex,\n hidden: () => win.hidden,\n gone: () => win.disposed === true,\n changed: () => { refreshPixels(); requestRedraw(); },\n };\n\n const model: LoupeModel = createLoupeModel({\n surface,\n ...(opts.mode ? { mode: opts.mode } : {}),\n ...(opts.factor !== undefined ? { factor: opts.factor } : {}),\n ...(opts.onColorChange ? { onColorChange: opts.onColorChange } : {}),\n ...(opts.onPick ? { onPick: opts.onPick } : {}),\n onDispose: () => teardown(),\n });\n\n const onPointerMove = (evt: PointerEvent) => {\n const r = input.getBoundingClientRect();\n model.aimAt({ x: evt.clientX - r.left, y: evt.clientY - r.top });\n };\n\n const teardown = () => {\n disposed = true;\n input.removeEventListener('pointermove', onPointerMove);\n pixels?.close();\n pixels = null;\n };\n\n input.addEventListener('pointermove', onPointerMove);\n\n return {\n window: win,\n get mode() { return model.mode; },\n get factor() { return model.factor; },\n get aim() { return model.aim; },\n get color() { return model.color; },\n setMode: model.setMode,\n setFactor: model.setFactor,\n aimAt: model.aimAt,\n pick: model.pick,\n dispose() {\n model.dispose();\n win.dispose();\n },\n };\n}\n"],"mappings":";;;;AAOA,SAAgB,EACd,GACA,GACA,GACA,GACA,GACA,GACW;CACX,IAAM,IAAK,KAAK,MAAM,EAAQ,IAAI,EAAI,EAChC,IAAK,KAAK,MAAM,EAAQ,IAAI,EAAI,EA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E;EACxC,GAAI,EAAK,WAAW,KAAA,IAAsC,EAAE,GAA5B,EAAE,QAAQ,EAAK,QAAQ;EACvD,GAAI,EAAK,gBAAgB,EAAE,eAAe,EAAK,eAAe,GAAG,EAAE;EACnE,GAAI,EAAK,SAAS,EAAE,QAAQ,EAAK,QAAQ,GAAG,EAAE;EAC9C,iBAAiB,GAAU;EAC5B,CAAC,EAEI,KAAiB,MAAsB;EAC3C,IAAM,IAAI,EAAM,uBAAuB;AACvC,IAAM,MAAM;GAAE,GAAG,EAAI,UAAU,EAAE;GAAM,GAAG,EAAI,UAAU,EAAE;GAAK,CAAC;IAG5D,UAAiB;AAIrB,EAHA,IAAW,IACX,EAAM,oBAAoB,eAAe,EAAc,EACvD,GAAQ,OAAO,EACf,IAAS;;AAKX,QAFA,EAAM,iBAAiB,eAAe,EAAc,EAE7C;EACL,QAAQ;EACR,IAAI,OAAO;AAAE,UAAO,EAAM;;EAC1B,IAAI,SAAS;AAAE,UAAO,EAAM;;EAC5B,IAAI,MAAM;AAAE,UAAO,EAAM;;EACzB,IAAI,QAAQ;AAAE,UAAO,EAAM;;EAC3B,SAAS,EAAM;EACf,WAAW,EAAM;EACjB,OAAO,EAAM;EACb,MAAM,EAAM;EACZ,UAAU;AAER,GADA,EAAM,SAAS,EACf,EAAI,SAAS;;EAEhB"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/loupe/readback.ts","../src/loupe/createLoupe.ts"],"sourcesContent":["/**\n * Read a `rw × rh` device-pixel region of the drawing buffer centered on\n * `pointer` (CSS px relative to the canvas), returned top-down.\n *\n * GL reports rows bottom-up; every consumer here wants top-down, so both the\n * read origin and the returned rows are flipped.\n */\nexport function readbackRegion(\n gl: WebGL2RenderingContext,\n buffer: { width: number; height: number },\n pointer: { x: number; y: number },\n dpr: number,\n rw: number,\n rh: number,\n): ImageData {\n const cx = Math.round(pointer.x * dpr);\n const cy = Math.round(pointer.y * dpr);\n const gx = clamp(cx - Math.floor(rw / 2), 0, Math.max(0, buffer.width - rw));\n const gyTop = clamp(cy - Math.floor(rh / 2), 0, Math.max(0, buffer.height - rh));\n const gy = buffer.height - gyTop - rh;\n\n const raw = new Uint8Array(rw * rh * 4);\n gl.readPixels(gx, gy, rw, rh, gl.RGBA, gl.UNSIGNED_BYTE, raw);\n\n const flipped = new Uint8ClampedArray(rw * rh * 4);\n const stride = rw * 4;\n for (let row = 0; row < rh; row++) {\n const src = (rh - 1 - row) * stride;\n flipped.set(raw.subarray(src, src + stride), row * stride);\n }\n return new ImageData(flipped, rw, rh);\n}\n\nfunction clamp(v: number, lo: number, hi: number): number {\n return v < lo ? lo : v > hi ? hi : v;\n}\n","import {\n createViewportLayer, screenToWorld, viewToTransform,\n type RenderLayer, type View,\n} from '@weasel-js/core';\nimport type { DrawCommand } from '@weasel-js/core/renderer';\nimport {\n createLoupeModel, loupeInnerView,\n type LoupeMode, type LoupeModel, type LoupeSurface,\n} from '@weasel-js/loupe';\nimport type { Hud } from '../hud';\nimport type { WindowWidget } from '../widgets/window/window';\nimport type { HudContentCtx, WidgetBounds } from '../widget';\nimport { readbackRegion } from './readback';\n\nexport type { LoupeMode };\n\n/** Options for `createLoupe`. */\nexport interface LoupeOptions {\n hud: Hud;\n /** The canvas pixel mode reads back from. Supplies the GL context and the\n * drawing-buffer dimensions. */\n canvas: HTMLCanvasElement;\n /** Where the aim comes from: the element pointer events are taken from and\n * the rect client coords are measured against. Defaults to `canvas`, which\n * is right whenever the canvas is also the thing under the pointer. */\n input?: HTMLElement;\n /** Layers re-rendered through the magnified inner view in vector mode. */\n source: RenderLayer<unknown>[];\n requestRedraw: () => void;\n mode?: LoupeMode;\n factor?: number;\n bounds?: WidgetBounds;\n title?: string;\n /** Draw the window's titlebar and close box. Default `true`. Pass `false`\n * for a bare lens; the interior then doubles as the move handle. */\n titlebar?: boolean;\n /** Called with the hex color under the aim point whenever it changes.\n * Sampled from the framebuffer in both modes — vector content is a\n * re-render whose edge colors differ from the screen's, so reading the\n * color off it would report something the user cannot see. */\n onColorChange?: (hex: string) => void;\n /** Fired when a click inside the lens picks a color — an eyedropper on the\n * magnified surface. Where the color goes is the consumer's: the loupe\n * reports the pick and nothing else. */\n onPick?: (hex: string) => void;\n /** Fired by the window's close box. The window does not hide itself —\n * whoever owns the loupe's visibility decides what closing means. */\n onClose?: () => void;\n /** Backdrop painted behind the magnified content in both modes, so the\n * outer canvas does not show through where the lens is empty. Defaults to\n * the theme's surface color. */\n background?: string;\n}\n\n/** Control surface for a live loupe: where it is aimed, how it magnifies,\n * what color it is over, and the window it lives in. */\nexport interface LoupeHandle {\n readonly window: WindowWidget;\n readonly mode: LoupeMode;\n readonly factor: number;\n /** Last aimed point, in screen-space CSS px relative to the canvas. */\n readonly aim: { x: number; y: number };\n /** Hex color under the aim point, read off the framebuffer. `null` before\n * the first aim, or when the GL context is unavailable. */\n readonly color: string | null;\n setMode(mode: LoupeMode): void;\n setFactor(factor: number): void;\n /** Aim at a screen point. Ignored while the point is over the window —\n * that is the freeze rule that makes the borders reachable. */\n aimAt(p: { x: number; y: number }): void;\n /** Sample the color the lens is showing at `p` — a screen point inside the\n * lens — and report it to `onPick`. Omit `p` to pick at the aim point.\n * Returns the hex, or `null` when the framebuffer cannot answer for that\n * point. Leaves the aim, and `color`, alone. */\n pick(p?: { x: number; y: number }): string | null;\n dispose(): void;\n}\n\n/**\n * Build a magnifier: a HUD window whose interior shows the canvas around an\n * aim point, enlarged. Adds the window to the HUD and starts following the\n * pointer; `dispose` removes it again.\n */\nexport function createLoupe(opts: LoupeOptions): LoupeHandle {\n const { hud, canvas: element, source, requestRedraw } = opts;\n const input = opts.input ?? element;\n let pixels: ImageBitmap | null = null;\n let pixelsPending = false;\n let refreshWanted = false;\n let disposed = false;\n\n const b = opts.bounds ?? { x: 24, y: 24, w: 220, h: 200 };\n\n const content = (ctx: HudContentCtx): DrawCommand[] =>\n model.mode === 'vector' ? drawVector(ctx) : drawPixels(ctx);\n\n const backdropColor = (ctx: HudContentCtx): string =>\n opts.background ?? ctx.tokens['--wzl-surface'];\n\n const drawVector = (ctx: HudContentCtx): DrawCommand[] => {\n const world = screenToWorld(model.aim.x, model.aim.y, viewToTransform(ctx.view));\n const inner: View = loupeInnerView(\n { x: world[0], y: world[1] }, ctx.view, ctx.rect, model.factor,\n );\n // A fresh lens per frame: CreateViewportLayerOpts.view is static, and the\n // inner view moves with the pointer.\n const lens = createViewportLayer<unknown>({\n id: `${win.id}:lens`,\n label: 'Loupe content',\n source,\n view: inner,\n bounds: () => ctx.rect,\n background: backdropColor(ctx),\n });\n return lens.draw(ctx.data, ctx.view, ctx.dims);\n };\n\n const drawPixels = (ctx: HudContentCtx): DrawCommand[] => {\n // The backdrop paints even with a bitmap in hand: a readback of a\n // transparent framebuffer is transparent, and before the first one\n // settles there is nothing at all, either of which leaves the lens a\n // hole onto the unmagnified canvas.\n const out: DrawCommand[] = [{\n kind: 'path',\n path: { kind: 'rect', x: ctx.rect.x, y: ctx.rect.y, width: ctx.rect.w, height: ctx.rect.h },\n fill: { fill: 'solid', color: backdropColor(ctx) },\n }];\n if (pixels) {\n out.push({\n kind: 'image',\n image: pixels,\n x: ctx.rect.x, y: ctx.rect.y, w: ctx.rect.w, h: ctx.rect.h,\n sampling: 'nearest',\n });\n }\n return out;\n };\n\n // `gl.readPixels` outside a landed paint returns the *previous* frame, so an\n // aim only marks the lens dirty; the read itself happens in the frame\n // subscriber below, on the frame that painted.\n const scheduleRefresh = () => {\n if (disposed || model.mode !== 'pixel') return;\n refreshWanted = true;\n };\n\n const refreshPixels = () => {\n if (disposed || model.mode !== 'pixel') return;\n const gl = element.getContext('webgl2');\n if (!gl) return;\n const cssRect = element.getBoundingClientRect();\n if (cssRect.width === 0) return;\n const dpr = element.width / cssRect.width;\n const rect = win.contentRect;\n const rw = Math.max(1, Math.round((rect.w * dpr) / model.factor));\n const rh = Math.max(1, Math.round((rect.h * dpr) / model.factor));\n const data = readbackRegion(\n gl, { width: element.width, height: element.height }, model.aim, dpr, rw, rh,\n );\n pixelsPending = true;\n createImageBitmap(data)\n .then((bmp) => {\n if (disposed) { bmp.close(); return; }\n pixels?.close();\n pixels = bmp;\n pixelsPending = false;\n requestRedraw();\n })\n .catch(() => {\n pixelsPending = false;\n requestRedraw();\n });\n };\n\n const readHex = (at: { x: number; y: number }): string | null => {\n const gl = element.getContext('webgl2');\n if (!gl) return null;\n const cssRect = element.getBoundingClientRect();\n if (cssRect.width === 0) return null;\n const dpr = element.width / cssRect.width;\n const px = readbackRegion(\n gl, { width: element.width, height: element.height }, at, dpr, 1, 1,\n );\n return '#' + [px.data[0], px.data[1], px.data[2]]\n .map((c) => c.toString(16).padStart(2, '0')).join('');\n };\n\n const win = hud.window({\n id: 'weasel-loupe',\n x: b.x, y: b.y, w: b.w, h: b.h,\n title: opts.title ?? 'Loupe',\n titlebar: opts.titlebar,\n content,\n onResize: () => { scheduleRefresh(); },\n onContentClick: (p) => { model.pick(p); },\n ...(opts.onClose ? { onClose: opts.onClose } : {}),\n });\n\n const surface: LoupeSurface = {\n lens: () => win.contentRect,\n covers: (p) => win.hitTest(p.x, p.y),\n sample: readHex,\n hidden: () => win.hidden,\n gone: () => win.disposed === true,\n changed: () => { scheduleRefresh(); requestRedraw(); },\n };\n\n const model: LoupeModel = createLoupeModel({\n surface,\n ...(opts.mode ? { mode: opts.mode } : {}),\n ...(opts.factor !== undefined ? { factor: opts.factor } : {}),\n ...(opts.onColorChange ? { onColorChange: opts.onColorChange } : {}),\n ...(opts.onPick ? { onPick: opts.onPick } : {}),\n onDispose: () => teardown(),\n });\n\n const onPointerMove = (evt: PointerEvent) => {\n const r = input.getBoundingClientRect();\n model.aimAt({ x: evt.clientX - r.left, y: evt.clientY - r.top });\n };\n\n const unsubscribeFrame = hud.subscribeFrame(() => {\n // A refresh wanted while a bitmap is in flight is remembered rather than\n // dropped: the flag survives until a frame finds it settled, so the aim\n // the user ends a fast drag on is the one that gets read.\n if (!refreshWanted || pixelsPending) return;\n refreshWanted = false;\n refreshPixels();\n });\n\n const teardown = () => {\n disposed = true;\n unsubscribeFrame();\n input.removeEventListener('pointermove', onPointerMove);\n pixels?.close();\n pixels = null;\n };\n\n input.addEventListener('pointermove', onPointerMove);\n\n return {\n window: win,\n get mode() { return model.mode; },\n get factor() { return model.factor; },\n get aim() { return model.aim; },\n get color() { return model.color; },\n setMode: model.setMode,\n setFactor: model.setFactor,\n aimAt: model.aimAt,\n pick: model.pick,\n dispose() {\n model.dispose();\n win.dispose();\n },\n };\n}\n"],"mappings":";;;;AAOA,SAAgB,EACd,GACA,GACA,GACA,GACA,GACA,GACW;CACX,IAAM,IAAK,KAAK,MAAM,EAAQ,IAAI,EAAI,EAChC,IAAK,KAAK,MAAM,EAAQ,IAAI,EAAI,EAChC,IAAK,EAAM,IAAK,KAAK,MAAM,IAAK,EAAE,EAAE,GAAG,KAAK,IAAI,GAAG,EAAO,QAAQ,EAAG,CAAC,EACtE,IAAQ,EAAM,IAAK,KAAK,MAAM,IAAK,EAAE,EAAE,GAAG,KAAK,IAAI,GAAG,EAAO,SAAS,EAAG,CAAC,EAC1E,IAAK,EAAO,SAAS,IAAQ,GAE7B,IAAM,IAAI,WAAW,IAAK,IAAK,EAAE;AACvC,GAAG,WAAW,GAAI,GAAI,GAAI,GAAI,EAAG,MAAM,EAAG,eAAe,EAAI;CAE7D,IAAM,IAAU,IAAI,kBAAkB,IAAK,IAAK,EAAE,EAC5C,IAAS,IAAK;AACpB,MAAK,IAAI,IAAM,GAAG,IAAM,GAAI,KAAO;EACjC,IAAM,KAAO,IAAK,IAAI,KAAO;AAC7B,IAAQ,IAAI,EAAI,SAAS,GAAK,IAAM,EAAO,EAAE,IAAM,EAAO;;AAE5D,QAAO,IAAI,UAAU,GAAS,GAAI,EAAG;;AAGvC,SAAS,EAAM,GAAW,GAAY,GAAoB;AACxD,QAAO,IAAI,IAAK,IAAK,IAAI,IAAK,IAAK;;;;ACiDrC,SAAgB,EAAY,GAAiC;CAC3D,IAAM,EAAE,QAAK,QAAQ,GAAS,WAA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AC;IAC/B,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC;oEACgE;IAChE,KAAK,CAAC,CAAC,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;IACzC;;;qDAGiD;IACjD,IAAI,CAAC,CAAC,CAAC,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,GAAG,IAAI,CAAC;IAClD,OAAO,IAAI,IAAI,CAAC;CACjB;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,YAAY,GAAG,WAAW,CA4K3D"}
@@ -1,4 +1,4 @@
1
- import { c as e, n as t, r as n } from "../tool-B6ZliNf3.js";
1
+ import { c as e, n as t, r as n } from "../tool-sq7ioZyd.js";
2
2
  import { resolveTheme as r, weaselTheme as i } from "@weasel-js/theme";
3
3
  import { useEffect as a, useMemo as o, useState as s } from "react";
4
4
  import { useThemeOptional as c } from "@weasel-js/theme/react";
@@ -545,26 +545,30 @@ function y(n) {
545
545
  //#endregion
546
546
  //#region src/hud.ts
547
547
  function b() {
548
- let e = [], t = null, n = !1, r = () => {
548
+ let e = [], t = null, n = !1, r = /* @__PURE__ */ new Set(), i = null, a = () => {
549
549
  t?.requestRedraw();
550
- }, i = (t) => () => {
550
+ }, o = () => {
551
+ i || !t || r.size === 0 || (i = t.subscribeFrame(() => {
552
+ for (let e of [...r]) e();
553
+ }));
554
+ }, s = (t) => () => {
551
555
  let n = t();
552
556
  if (!n) return;
553
- let i = e.indexOf(n);
554
- i !== -1 && (e.splice(i, 1), r());
555
- }, a = (t, i) => {
557
+ let r = e.indexOf(n);
558
+ r !== -1 && (e.splice(r, 1), a());
559
+ }, p = (t, r) => {
556
560
  if (n) {
557
- console.warn(`weasel-hud: ${i} called on a detached HUD; ignored.`);
561
+ console.warn(`weasel-hud: ${r} called on a detached HUD; ignored.`);
558
562
  return;
559
563
  }
560
- e.push(t), r();
564
+ e.push(t), a();
561
565
  };
562
566
  return {
563
567
  get attached() {
564
568
  return t !== null;
565
569
  },
566
570
  add(e) {
567
- a(e, "add()");
571
+ p(e, "add()");
568
572
  },
569
573
  remove(t) {
570
574
  let n = e.indexOf(t);
@@ -575,69 +579,74 @@ function b() {
575
579
  } catch (e) {
576
580
  console.error("weasel-hud: widget.dispose threw", e);
577
581
  }
578
- r();
582
+ a();
579
583
  }
580
584
  },
581
585
  widgets() {
582
586
  return e;
583
587
  },
584
588
  markDirty() {
585
- r();
589
+ a();
586
590
  },
587
- bind(i) {
591
+ bind(r) {
588
592
  if (t) throw Error("weasel-hud: HUD is already bound to a host.");
589
- t = i, n = !1, e.length > 0 && r();
593
+ t = r, n = !1, o(), e.length > 0 && a();
590
594
  },
591
595
  unbind() {
592
- t = null, n = !0;
596
+ i?.(), i = null, t = null, n = !0;
597
+ },
598
+ subscribeFrame(e) {
599
+ return r.add(e), o(), () => {
600
+ r.delete(e), r.size === 0 && (i?.(), i = null);
601
+ };
593
602
  },
594
603
  rect(e) {
595
- let t = null, n = i(() => t);
604
+ let t = null, n = s(() => t);
596
605
  return t = c({
597
606
  ...e,
598
- onChange: () => r(),
607
+ onChange: () => a(),
599
608
  removeFromHud: n
600
- }), a(t, "rect()"), t;
609
+ }), p(t, "rect()"), t;
601
610
  },
602
611
  text(e) {
603
- let t = null, n = i(() => t);
612
+ let t = null, n = s(() => t);
604
613
  return t = l({
605
614
  ...e,
606
- onChange: () => r(),
615
+ onChange: () => a(),
607
616
  removeFromHud: n
608
- }), a(t, "text()"), t;
617
+ }), p(t, "text()"), t;
609
618
  },
610
619
  image(e) {
611
- let t = null, n = i(() => t);
620
+ let t = null, n = s(() => t);
612
621
  return t = u({
613
622
  ...e,
614
- onChange: () => r(),
623
+ onChange: () => a(),
615
624
  removeFromHud: n
616
- }), a(t, "image()"), t;
625
+ }), p(t, "image()"), t;
617
626
  },
618
627
  label(e) {
619
- let t = null, n = i(() => t);
628
+ let t = null, n = s(() => t);
620
629
  return t = d({
621
630
  ...e,
622
- onChange: () => r(),
631
+ onChange: () => a(),
623
632
  removeFromHud: n
624
- }), a(t, "label()"), t;
633
+ }), p(t, "label()"), t;
625
634
  },
626
635
  button(e) {
627
- let t = null, n = i(() => t);
636
+ let t = null, n = s(() => t);
628
637
  return t = f({
629
638
  ...e,
630
- onChange: () => r(),
639
+ onChange: () => a(),
631
640
  removeFromHud: n
632
- }), a(t, "button()"), t;
641
+ }), p(t, "button()"), t;
633
642
  },
634
643
  window(e) {
635
- let t = null, n = i(() => t);
644
+ let t = null, n = s(() => t);
636
645
  return t = y({
637
646
  ...e,
638
- onChange: () => r(),
647
+ onChange: () => a(),
639
648
  removeFromHud: n
640
- }), a(t, "window()"), t;
649
+ }), p(t, "window()"), t;
641
650
  }
642
651
  };
643
652
  }
@@ -745,7 +754,8 @@ function O(e, t, i = {}) {
745
754
  });
746
755
  return t.bind({
747
756
  requestRedraw: e.requestRedraw,
748
- registerLayer: () => () => {}
757
+ registerLayer: () => () => {},
758
+ subscribeFrame: e.subscribeFrame
749
759
  }), () => {
750
760
  c &&= (c.onPointer({
751
761
  type: "hoverleave",
@@ -979,4 +989,4 @@ function B() {
979
989
  //#endregion
980
990
  export { E as a, b as c, T as i, p as l, B as n, C as o, O as r, w as s, k as t, _ as u };
981
991
 
982
- //# sourceMappingURL=tool-B6ZliNf3.js.map
992
+ //# sourceMappingURL=tool-sq7ioZyd.js.map