@hackbuild/ai-views 0.1.13 → 0.1.14

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.
@@ -10915,31 +10915,7 @@ function mountSessionMenu(host, list, o = {}) {
10915
10915
  l.add(list.subscribe(() => draw()));
10916
10916
  return { dispose: () => l.off() };
10917
10917
  }
10918
- // packages/ai-views/src/mount-mask.ts
10919
- /**
10920
- * MaskStudio: drop an image, press a thing
10921
- * to cut it out (a click segment model such as SlimSAM), fix the mask by
10922
- * hand with the wand, brush and lasso of hackbuild-ui maskEditor (x swaps
10923
- * add and erase, [ and ] size the brush, ctrl z undoes), zoom and fit, and
10924
- * export the mask, a matte or the cutout. With a segment everything model
10925
- * (FastSAM) one press finds every object; each lands in the found list and
10926
- * a press adds it to the mask. Models are functions the page passes, so
10927
- * stubs run the studio with no weights. Three views (composite, mask,
10928
- * found objects) paint over the canvas; the pointer previews the click cut
10929
- * or lights the found object under it; two fingers or ctrl and the wheel
10930
- * zoom; a page that loads models on demand hears `onWant` when a tool needs
10931
- * one.
10932
- */
10933
- const ZOOMS = [
10934
- .5,
10935
- .75,
10936
- 1,
10937
- 1.5,
10938
- 2,
10939
- 3,
10940
- 4
10941
- ];
10942
- const clampZoom = (z) => Math.max(.25, Math.min(6, z));
10918
+ // packages/ai-views/src/mask-pixels.ts
10943
10919
  /** Paint `rgb` at `alpha` (0 to 255) where the mask keeps, leaving the other pixels as they are. */
10944
10920
  function tintInto(rgba, mask, rgb, alpha) {
10945
10921
  for (let i = 0, j = 0; i < mask.length; i++, j += 4) {
@@ -10984,6 +10960,267 @@ function thumbOf(img) {
10984
10960
  t.getContext("2d")?.drawImage(c, 0, 0, t.width, t.height);
10985
10961
  return t.toDataURL("image/png");
10986
10962
  }
10963
+ // packages/ai-views/src/mask-overlay.ts
10964
+ /**
10965
+ * The paint over the studio's canvas: the mask alone, the found objects in
10966
+ * colour with their boxes and words, or the pointer's preview of a cut.
10967
+ * One repaint per animation frame, skipped when nothing it shows changed.
10968
+ */
10969
+ /** An overlay on a canvas; `paint` asks for a repaint on the next animation frame. */
10970
+ function maskOverlay(over, scene) {
10971
+ let painted = "";
10972
+ let req = 0;
10973
+ const draw = () => {
10974
+ req = 0;
10975
+ const s = scene();
10976
+ const { width: w, height: h, view, hover, preview, found } = s;
10977
+ const g = over.getContext("2d");
10978
+ if (!g || !w || !h || !s.hasImage) return;
10979
+ if (over.width !== w || over.height !== h) {
10980
+ over.width = w;
10981
+ over.height = h;
10982
+ }
10983
+ const key = `${view}:${hover}:${preview ? "p" : ""}:${view === "mask" ? s.maskKey : ""}:${found.map((f) => f.hidden ? 0 : 1).join("")}`;
10984
+ if (view === "composite" && hover < 0 && !preview) {
10985
+ if (painted !== "clear") g.clearRect(0, 0, w, h);
10986
+ painted = "clear";
10987
+ return;
10988
+ }
10989
+ if (key === painted && view !== "mask") return;
10990
+ painted = key;
10991
+ if (view === "mask") {
10992
+ g.putImageData(new ImageData(er(s.mask(), "matte"), w, h), 0, 0);
10993
+ return;
10994
+ }
10995
+ const rgba = new Uint8ClampedArray(w * h * 4);
10996
+ if (view === "instances") found.forEach((f, i) => !f.hidden && i !== hover && tintInto(rgba, f.mask, nr(f.colour), 150));
10997
+ const hot = hover >= 0 ? found[hover] : void 0;
10998
+ if (hot) tintInto(rgba, hot.mask, nr(hot.colour), 220);
10999
+ else if (preview) tintInto(rgba, preview, [
11000
+ 254,
11001
+ 3,
11002
+ 134
11003
+ ], 110);
11004
+ g.putImageData(new ImageData(rgba, w, h), 0, 0);
11005
+ if (view !== "instances") return;
11006
+ g.lineWidth = Math.max(1, w / 400);
11007
+ g.font = `${Math.max(10, Math.round(w / 60))}px monospace`;
11008
+ for (const f of found) {
11009
+ if (f.hidden || !f.box) continue;
11010
+ g.strokeStyle = f.colour;
11011
+ g.fillStyle = f.colour;
11012
+ g.strokeRect(f.box.x, f.box.y, f.box.w, f.box.h);
11013
+ g.fillText(`${f.label} ${Math.round(f.score * 100)}`, f.box.x + 3, Math.max(12, f.box.y - 4));
11014
+ }
11015
+ };
11016
+ return { paint() {
11017
+ if (req) return;
11018
+ req = typeof requestAnimationFrame === "function" ? requestAnimationFrame(draw) : (draw(), 0);
11019
+ } };
11020
+ }
11021
+ // packages/ai-views/src/mask-input.ts
11022
+ /** Pinch and ctrl wheel zoom on the well around the canvas. */
11023
+ function pinchZoom(l, well, z) {
11024
+ const touches = /* @__PURE__ */ new Map();
11025
+ let pinch = null;
11026
+ const spread = () => {
11027
+ const [a, b] = [...touches.values()];
11028
+ return a && b ? Math.hypot(a.x - b.x, a.y - b.y) : 0;
11029
+ };
11030
+ l.on(well, "pointerdown", (e) => {
11031
+ const pe = e;
11032
+ if (pe.pointerType !== "touch") return;
11033
+ touches.set(pe.pointerId, {
11034
+ x: pe.clientX,
11035
+ y: pe.clientY
11036
+ });
11037
+ if (touches.size === 2) {
11038
+ z.cancel();
11039
+ pinch = {
11040
+ d: spread() || 1,
11041
+ z: z.zoom()
11042
+ };
11043
+ }
11044
+ });
11045
+ l.on(well, "pointermove", (e) => {
11046
+ const pe = e;
11047
+ if (!touches.has(pe.pointerId)) return;
11048
+ touches.set(pe.pointerId, {
11049
+ x: pe.clientX,
11050
+ y: pe.clientY
11051
+ });
11052
+ if (pinch && touches.size === 2) z.zoomTo(pinch.z * (spread() / pinch.d));
11053
+ });
11054
+ const lift = (e) => {
11055
+ touches.delete(e.pointerId);
11056
+ if (touches.size < 2) pinch = null;
11057
+ };
11058
+ l.on(well, "pointerup", lift);
11059
+ l.on(well, "pointercancel", lift);
11060
+ l.on(well, "wheel", (e) => {
11061
+ const we = e;
11062
+ if (!we.ctrlKey || !z.ready()) return;
11063
+ we.preventDefault();
11064
+ z.zoomTo(z.zoom() * Math.exp(-we.deltaY / 200));
11065
+ });
11066
+ }
11067
+ /** The page keys of a studio, on the document. */
11068
+ function studioKeys(l, k) {
11069
+ l.on(document, "keydown", (e) => {
11070
+ const ke = e;
11071
+ if (ke.defaultPrevented || ke.target.closest?.("input,textarea,select,[contenteditable]")) return;
11072
+ const key = ke.key.toLowerCase();
11073
+ const cmd = ke.ctrlKey || ke.metaKey;
11074
+ if (cmd && key === "o") return ke.preventDefault(), k.open();
11075
+ if (!k.ready()) return;
11076
+ if (cmd && (key === "z" || key === "y")) return ke.preventDefault(), key === "y" || ke.shiftKey ? k.redo() : k.undo();
11077
+ if (cmd && key === "s") return ke.preventDefault(), k.save(ke.shiftKey ? "cutout" : "matte");
11078
+ if (cmd && key === "i") return ke.preventDefault(), k.invert();
11079
+ if (cmd) return;
11080
+ if (key === "delete") return ke.preventDefault(), k.clear();
11081
+ if (key === "+" || key === "=") return k.zoom(1);
11082
+ if (key === "-") return k.zoom(-1);
11083
+ if (key === "0") return k.zoom(0);
11084
+ });
11085
+ }
11086
+ // packages/ai-views/src/mask-parts.ts
11087
+ /**
11088
+ * The pieces around a mask studio, each mounted on its own host: the tool
11089
+ * rail, the verb row on the canvas, and the rack of found objects, the mask
11090
+ * and saved masks. Each reads the studio's state and calls its methods.
11091
+ */
11092
+ /** The tool rail of a studio: point (when a click model is set), wand, brush, lasso, and segment everything (when that model is set). */
11093
+ function mountMaskTools(host, studio) {
11094
+ const l = listeners();
11095
+ const draw = (s) => {
11096
+ host.innerHTML = [...studio.hasSegment || studio.canWant ? [{
11097
+ id: "point",
11098
+ label: "cut out a thing",
11099
+ key: "a",
11100
+ icon: "crosshairs"
11101
+ }] : [], ...gt.map((t) => ({
11102
+ id: t.id,
11103
+ label: t.label,
11104
+ key: t.key,
11105
+ icon: t.icon
11106
+ }))].map((t) => `<button type="button" class="hb-iconbtn" data-tool="${t.id}" aria-label="${esc(t.label)} (${t.key})" title="${esc(t.label)} (${t.key})" aria-pressed="${s.tool === t.id}" aria-keyshortcuts="${t.key}">${rn(t.icon, { size: 18 })}</button>`).join("");
11107
+ };
11108
+ l.on(host, "click", (e) => {
11109
+ const t = e.target.closest("[data-tool]");
11110
+ if (t) studio.setTool(t.dataset.tool);
11111
+ });
11112
+ l.on(document, "keydown", (e) => {
11113
+ const ke = e;
11114
+ if (ke.key === "a" && !ke.ctrlKey && !ke.metaKey && (studio.hasSegment || studio.canWant) && !ke.target.closest?.("input,textarea,[contenteditable]")) studio.setTool("point");
11115
+ });
11116
+ l.add(studio.subscribe(draw));
11117
+ return { dispose: () => l.off() };
11118
+ }
11119
+ /** The verb row on the canvas: undo and redo once there is a change, zoom once there is an image. */
11120
+ function mountMaskVerbs(host, studio) {
11121
+ const l = listeners();
11122
+ let last = "";
11123
+ const draw = (s) => {
11124
+ const html = s.ready ? `${s.canUndo ? `<button type="button" class="hb-iconbtn" data-v="undo" aria-label="undo" title="undo (ctrl z)">${ICON.undo}</button>` : ""}${s.canRedo ? `<button type="button" class="hb-iconbtn" data-v="redo" aria-label="redo" title="redo (ctrl shift z)">${ICON.redo}</button>` : ""}` + (s.zoom !== 1 ? `<button type="button" class="hb-iconbtn" data-v="out" aria-label="zoom out" title="zoom out (-)">${ICON.zoomOut}</button><button type="button" class="hb-iconbtn" data-v="fit" aria-label="fit, now ${Math.round(s.zoom * 100)} percent" title="fit (0)">${ICON.fit}</button>` : "") + `<button type="button" class="hb-iconbtn" data-v="in" aria-label="zoom in" title="zoom in (+)">${ICON.zoomIn}</button>` + (s.coverage > 0 || s.found.length ? `<div class="hb-seg is-toggle" role="group" aria-label="view">${[
11125
+ "composite",
11126
+ "mask",
11127
+ "instances"
11128
+ ].filter((v) => v !== "instances" || s.found.length).map((v) => `<button type="button" data-view="${v}" class="${s.view === v ? "is-on" : ""}" aria-pressed="${s.view === v}">${v === "instances" ? "objects" : v === "composite" ? "picture" : "mask"}</button>`).join("")}</div>` : "") : "";
11129
+ if (html !== last) host.innerHTML = last = html;
11130
+ host.hidden = !html;
11131
+ };
11132
+ l.on(host, "click", (e) => {
11133
+ const view = e.target.closest("[data-view]")?.dataset.view;
11134
+ if (view) return studio.setView(view);
11135
+ const v = e.target.closest("[data-v]")?.dataset.v;
11136
+ if (v === "undo") studio.undo();
11137
+ else if (v === "redo") studio.redo();
11138
+ else if (v === "in") studio.zoom(1);
11139
+ else if (v === "out") studio.zoom(-1);
11140
+ else if (v === "fit") studio.zoom(0);
11141
+ });
11142
+ l.add(studio.subscribe(draw));
11143
+ return { dispose: () => l.off() };
11144
+ }
11145
+ /** The rack parts: what was found, the mask tools' settings, and the exports once there is a mask. */
11146
+ function mountMaskRack(host, studio, part, o = {}) {
11147
+ const l = listeners();
11148
+ let last = "";
11149
+ const draw = (s) => {
11150
+ let html = "";
11151
+ if (part === "found") {
11152
+ html = s.found.length ? `<div class="hb-inst"><p class="hb-inst-empty">${s.found.length} found, ${s.found.filter((f) => f.on).length} in the mask</p><div class="hb-toolbar hb-inst-foot"><button type="button" class="hb-btn hb-btn-go" data-op="merge">merge all</button><button type="button" class="hb-btn" data-op="unfind">clear all</button></div><ul class="hb-inst-list">${s.found.map((f, i) => `<li class="hb-inst-row${f.on || s.hover === i ? " is-on" : ""}${f.hidden ? " is-hidden" : ""}" style="--hb-inst-c:${f.colour}"><button type="button" class="hb-inst-pick" data-found="${i}" aria-pressed="${f.on}"><span class="hb-inst-sw"></span><span class="hb-inst-name">${esc(f.label)}</span><span class="hb-inst-score">${Math.min(100, Math.round(f.score * 100))}%</span></button><button type="button" class="hb-inst-act" data-show="${i}" aria-pressed="${!f.hidden}" aria-label="${f.hidden ? "show" : "hide"} ${esc(f.label)}" title="${f.hidden ? "show" : "hide"}">${rn("eye", { size: 14 })}</button><button type="button" class="hb-inst-act" data-drop="${i}" aria-label="delete ${esc(f.label)}" title="delete">${ICON.trash}</button></li>`).join("")}</ul></div>` : `<p class="hb-lay-empty-note">${s.ready ? studio.hasEverything || studio.canWant ? "find every object at once, or press a thing on the picture." : "press a thing to cut it out." : "nothing yet. open an image or try the sample."}</p>` + (s.ready && (studio.hasEverything || studio.canWant) && !s.busy ? `<button type="button" class="hb-btn hb-btn-go" data-all>find every object</button>` : "");
11153
+ html += s.ready ? `<div class="hb-kv"><span class="hb-label">mask keeps</span><span class="hb-readout">${Math.round(s.coverage * 100)}%</span></div>` : "";
11154
+ } else if (part === "mask") html = `<div class="hb-seg is-toggle" role="group" aria-label="mode">${["add", "erase"].map((m) => `<button type="button" data-mode="${m}" class="${s.mode === m ? "is-on" : ""}" aria-pressed="${s.mode === m}">${m}<span class="hb-mask-key" aria-hidden="true">x</span></button>`).join("")}</div>` + (s.tool === "brush" || s.tool === "eraser" ? `<label class="hb-field"><span class="hb-label">brush, ${s.size} px</span><input class="hb-range" type="range" min="4" max="160" step="2" value="${s.size}" data-size></label>` : "") + (s.tool === "wand" || s.tool === "flood" ? `<label class="hb-field"><span class="hb-label">reach, ${s.tolerance}</span><input class="hb-range" type="range" min="0" max="100" step="1" value="${s.tolerance}" data-tol></label>` : "") + (s.tool === "wand" ? `<label class="hb-check"><input type="checkbox" data-contig${s.contiguous ? " checked" : ""}><span>touching pixels only</span></label>` : "") + (s.coverage > 0 ? `<div class="hb-toolbar"><button type="button" class="hb-btn" data-op="smooth">smooth</button><button type="button" class="hb-btn" data-op="expand">expand</button><button type="button" class="hb-btn" data-op="shrink">shrink</button><button type="button" class="hb-btn" data-op="invert">invert</button><button type="button" class="hb-btn" data-op="clear">clear</button></div>` : "");
11155
+ else if (part === "saved") html = (s.coverage > 0 ? `<div class="hb-toolbar"><button type="button" class="hb-btn" data-op="save">save this mask</button></div>` : "") + (s.saved.length ? `<div class="hb-inst"><ul class="hb-inst-list">${s.saved.map((m) => `<li class="hb-inst-row"><button type="button" class="hb-inst-pick" data-restore="${m.id}"><img class="hb-inst-sw" src="${m.thumb}" alt=""><span class="hb-inst-name">${esc(m.name)}</span><span class="hb-inst-score">${Math.round(m.coverage * 100)}%</span></button><button type="button" class="hb-inst-act" data-forget="${m.id}" aria-label="forget ${esc(m.name)}" title="forget">${ICON.x}</button></li>`).join("")}</ul></div>` : `<p class="hb-lay-empty-note">no saved masks. save one to come back to it.</p>`);
11156
+ else html = s.coverage > 0 ? `<div class="hb-toolbar" role="group" aria-label="export"><button type="button" class="hb-btn" data-ex="cutout">cutout png</button><button type="button" class="hb-btn" data-ex="alpha">mask png</button><button type="button" class="hb-btn" data-ex="matte">matte png</button><button type="button" class="hb-btn" data-ex="inverse">inverse png</button></div>` : `<p class="hb-lay-empty-note">exports show once there is a mask.</p>`;
11157
+ if (html !== last) host.innerHTML = last = html;
11158
+ };
11159
+ l.on(host, "click", (e) => {
11160
+ const t = e.target;
11161
+ const f = t.closest("[data-found]");
11162
+ if (f) return studio.toggleFound(Number(f.dataset.found));
11163
+ const m = t.closest("[data-mode]");
11164
+ if (m) return studio.setMode(m.dataset.mode);
11165
+ const show = t.closest("[data-show]");
11166
+ if (show) return studio.showFound(Number(show.dataset.show), show.getAttribute("aria-pressed") !== "true");
11167
+ const drop = t.closest("[data-drop]");
11168
+ if (drop) return studio.removeFound(Number(drop.dataset.drop));
11169
+ const op = t.closest("[data-op]")?.dataset.op;
11170
+ if (op === "merge") return studio.mergeFound();
11171
+ if (op === "unfind") return studio.clearFound();
11172
+ if (op === "invert") return studio.invert();
11173
+ if (op === "clear") return studio.clear();
11174
+ if (op === "smooth") return studio.smooth();
11175
+ if (op === "expand") return studio.expand();
11176
+ if (op === "shrink") return studio.shrink();
11177
+ if (op === "save") return studio.save();
11178
+ const restore = t.closest("[data-restore]");
11179
+ if (restore) return studio.restore(Number(restore.dataset.restore));
11180
+ const forget = t.closest("[data-forget]");
11181
+ if (forget) return studio.forget(Number(forget.dataset.forget));
11182
+ const ex = t.closest("[data-ex]");
11183
+ if (ex) {
11184
+ const kind = ex.dataset.ex;
11185
+ studio.exportAs(kind).then((b) => b && download(`${o.name ?? "mask"}-${kind}.png`, b));
11186
+ return;
11187
+ }
11188
+ if (t.closest("[data-all]")) studio.everything();
11189
+ });
11190
+ l.on(host, "input", (e) => {
11191
+ const i = e.target;
11192
+ if (i.dataset.size !== void 0) studio.setSize(Number(i.value));
11193
+ if (i.dataset.tol !== void 0) studio.setTolerance(Number(i.value));
11194
+ if (i.dataset.contig !== void 0) studio.setContiguous(i.checked);
11195
+ });
11196
+ l.add(studio.subscribe(draw));
11197
+ return { dispose: () => l.off() };
11198
+ }
11199
+ // packages/ai-views/src/mount-mask.ts
11200
+ /**
11201
+ * MaskStudio: drop an image, press a thing
11202
+ * to cut it out (a click segment model such as SlimSAM), fix the mask by
11203
+ * hand with the wand, brush and lasso of hackbuild-ui maskEditor (x swaps
11204
+ * add and erase, [ and ] size the brush, ctrl z undoes), zoom and fit, and
11205
+ * export the mask, a matte or the cutout. With a segment everything model
11206
+ * (FastSAM) one press finds every object; each lands in the found list and
11207
+ * a press adds it to the mask. Models are functions the page passes, so
11208
+ * stubs run the studio with no weights. Three views (composite, mask,
11209
+ * found objects) paint over the canvas; the pointer previews the click cut
11210
+ * or lights the found object under it; two fingers or ctrl and the wheel
11211
+ * zoom; a page that loads models on demand hears `onWant` when a tool needs
11212
+ * one.
11213
+ */
11214
+ const ZOOMS = [
11215
+ .5,
11216
+ .75,
11217
+ 1,
11218
+ 1.5,
11219
+ 2,
11220
+ 3,
11221
+ 4
11222
+ ];
11223
+ const clampZoom = (z) => Math.max(.25, Math.min(6, z));
10987
11224
  /**
10988
11225
  * Mounts a mask studio on a host: drop or open an image, then cut masks with the point, wand, brush
10989
11226
  * and lasso tools or a segment model.
@@ -11040,56 +11277,17 @@ function mountMaskStudio(host, o = {}) {
11040
11277
  for (const fn of subs) fn(s);
11041
11278
  paint();
11042
11279
  };
11043
- let painted = "";
11044
- let frameReq = 0;
11045
- const paint = () => {
11046
- if (frameReq) return;
11047
- frameReq = typeof requestAnimationFrame === "function" ? requestAnimationFrame(drawOver) : (drawOver(), 0);
11048
- };
11049
- const drawOver = () => {
11050
- frameReq = 0;
11051
- const w = ed?.width ?? 0;
11052
- const h = ed?.height ?? 0;
11053
- const g = over.getContext("2d");
11054
- if (!g || !w || !h || !pixels) return;
11055
- if (over.width !== w || over.height !== h) {
11056
- over.width = w;
11057
- over.height = h;
11058
- }
11059
- const key = `${view}:${hover}:${preview ? "p" : ""}:${view === "mask" ? editor.state.coverage + ":" + (ed?.canUndo ? 1 : 0) : ""}:${found.map((f) => f.hidden ? 0 : 1).join("")}`;
11060
- if (view === "composite" && hover < 0 && !preview) {
11061
- if (painted !== "clear") g.clearRect(0, 0, w, h);
11062
- painted = "clear";
11063
- return;
11064
- }
11065
- if (key === painted && view !== "mask") return;
11066
- painted = key;
11067
- if (view === "mask") {
11068
- g.putImageData(new ImageData(er(editor.mask(), "matte"), w, h), 0, 0);
11069
- return;
11070
- }
11071
- const rgba = new Uint8ClampedArray(w * h * 4);
11072
- if (view === "instances") found.forEach((f, i) => !f.hidden && i !== hover && tintInto(rgba, f.mask, nr(f.colour), 150));
11073
- const hot = hover >= 0 ? found[hover] : void 0;
11074
- if (hot) tintInto(rgba, hot.mask, nr(hot.colour), 220);
11075
- else if (preview) tintInto(rgba, preview, [
11076
- 254,
11077
- 3,
11078
- 134
11079
- ], 110);
11080
- g.putImageData(new ImageData(rgba, w, h), 0, 0);
11081
- if (view === "instances") {
11082
- g.lineWidth = Math.max(1, w / 400);
11083
- g.font = `${Math.max(10, Math.round(w / 60))}px monospace`;
11084
- for (const f of found) {
11085
- if (f.hidden || !f.box) continue;
11086
- g.strokeStyle = f.colour;
11087
- g.fillStyle = f.colour;
11088
- g.strokeRect(f.box.x, f.box.y, f.box.w, f.box.h);
11089
- g.fillText(`${f.label} ${Math.round(f.score * 100)}`, f.box.x + 3, Math.max(12, f.box.y - 4));
11090
- }
11091
- }
11092
- };
11280
+ const { paint } = maskOverlay(over, () => ({
11281
+ width: ed?.width ?? 0,
11282
+ height: ed?.height ?? 0,
11283
+ hasImage: !!pixels,
11284
+ view,
11285
+ hover,
11286
+ preview,
11287
+ found,
11288
+ maskKey: `${editor.state.coverage}:${ed?.canUndo ? 1 : 0}`,
11289
+ mask: () => editor.mask()
11290
+ }));
11093
11291
  const editor = ns(canvas, {
11094
11292
  tool: tool === "point" ? "wand" : tool,
11095
11293
  onError: (e) => (error = `the image did not load: ${e.message}`, emit())
@@ -11193,63 +11391,21 @@ function mountMaskStudio(host, o = {}) {
11193
11391
  canvas.addEventListener("pointermove", onHover);
11194
11392
  canvas.addEventListener("pointerleave", offHover);
11195
11393
  l.add(() => (canvas.removeEventListener("pointermove", onHover), canvas.removeEventListener("pointerleave", offHover)));
11196
- const touches = /* @__PURE__ */ new Map();
11197
- let pinch = null;
11198
- const spread = () => {
11199
- const [a, b] = [...touches.values()];
11200
- return a && b ? Math.hypot(a.x - b.x, a.y - b.y) : 0;
11201
- };
11202
- l.on(well, "pointerdown", (e) => {
11203
- const pe = e;
11204
- if (pe.pointerType !== "touch") return;
11205
- touches.set(pe.pointerId, {
11206
- x: pe.clientX,
11207
- y: pe.clientY
11208
- });
11209
- if (touches.size === 2) {
11210
- editor.cancel();
11211
- pinch = {
11212
- d: spread() || 1,
11213
- z: zoom
11214
- };
11215
- }
11394
+ pinchZoom(l, well, {
11395
+ zoom: () => zoom,
11396
+ zoomTo: (z) => studioApi.zoomTo(z),
11397
+ cancel: () => editor.cancel(),
11398
+ ready: () => !!ed?.ready
11216
11399
  });
11217
- l.on(well, "pointermove", (e) => {
11218
- const pe = e;
11219
- if (!touches.has(pe.pointerId)) return;
11220
- touches.set(pe.pointerId, {
11221
- x: pe.clientX,
11222
- y: pe.clientY
11223
- });
11224
- if (pinch && touches.size === 2) studioApi.zoomTo(pinch.z * (spread() / pinch.d));
11225
- });
11226
- const lift = (e) => {
11227
- touches.delete(e.pointerId);
11228
- if (touches.size < 2) pinch = null;
11229
- };
11230
- l.on(well, "pointerup", lift);
11231
- l.on(well, "pointercancel", lift);
11232
- l.on(well, "wheel", (e) => {
11233
- const we = e;
11234
- if (!we.ctrlKey || !ed?.ready) return;
11235
- we.preventDefault();
11236
- studioApi.zoomTo(zoom * Math.exp(-we.deltaY / 200));
11237
- });
11238
- l.on(document, "keydown", (e) => {
11239
- const ke = e;
11240
- if (ke.defaultPrevented || ke.target.closest?.("input,textarea,select,[contenteditable]")) return;
11241
- const k = ke.key.toLowerCase();
11242
- const cmd = ke.ctrlKey || ke.metaKey;
11243
- if (cmd && k === "o") return ke.preventDefault(), input.click();
11244
- if (!ed?.ready) return;
11245
- if (cmd && (k === "z" || k === "y")) return ke.preventDefault(), void (k === "y" || ke.shiftKey ? editor.redo() : editor.undo());
11246
- if (cmd && k === "s") return ke.preventDefault(), void studioApi.exportAs(ke.shiftKey ? "cutout" : "matte").then((b) => b && download(`${o.name ?? "mask"}-${ke.shiftKey ? "cutout" : "matte"}.png`, b));
11247
- if (cmd && k === "i") return ke.preventDefault(), editor.invert();
11248
- if (cmd) return;
11249
- if (k === "delete") return ke.preventDefault(), editor.clear();
11250
- if (k === "+" || k === "=") return studioApi.zoom(1);
11251
- if (k === "-") return studioApi.zoom(-1);
11252
- if (k === "0") return studioApi.zoom(0);
11400
+ studioKeys(l, {
11401
+ ready: () => !!ed?.ready,
11402
+ open: () => input.click(),
11403
+ undo: () => void editor.undo(),
11404
+ redo: () => void editor.redo(),
11405
+ invert: () => editor.invert(),
11406
+ clear: () => editor.clear(),
11407
+ save: (kind) => void studioApi.exportAs(kind).then((b) => b && download(`${o.name ?? "mask"}-${kind}.png`, b)),
11408
+ zoom: (dir) => studioApi.zoom(dir)
11253
11409
  });
11254
11410
  canvas.addEventListener("pointerdown", onPoint, true);
11255
11411
  l.add(() => canvas.removeEventListener("pointerdown", onPoint, true));
@@ -11479,113 +11635,6 @@ function mountMaskStudio(host, o = {}) {
11479
11635
  emit();
11480
11636
  return studioApi;
11481
11637
  }
11482
- /** The tool rail of a studio: point (when a click model is set), wand, brush, lasso, and segment everything (when that model is set). */
11483
- function mountMaskTools(host, studio) {
11484
- const l = listeners();
11485
- const draw = (s) => {
11486
- host.innerHTML = [...studio.hasSegment || studio.canWant ? [{
11487
- id: "point",
11488
- label: "cut out a thing",
11489
- key: "a",
11490
- icon: "crosshairs"
11491
- }] : [], ...gt.map((t) => ({
11492
- id: t.id,
11493
- label: t.label,
11494
- key: t.key,
11495
- icon: t.icon
11496
- }))].map((t) => `<button type="button" class="hb-iconbtn" data-tool="${t.id}" aria-label="${esc(t.label)} (${t.key})" title="${esc(t.label)} (${t.key})" aria-pressed="${s.tool === t.id}" aria-keyshortcuts="${t.key}">${rn(t.icon, { size: 18 })}</button>`).join("");
11497
- };
11498
- l.on(host, "click", (e) => {
11499
- const t = e.target.closest("[data-tool]");
11500
- if (t) studio.setTool(t.dataset.tool);
11501
- });
11502
- l.on(document, "keydown", (e) => {
11503
- const ke = e;
11504
- if (ke.key === "a" && !ke.ctrlKey && !ke.metaKey && (studio.hasSegment || studio.canWant) && !ke.target.closest?.("input,textarea,[contenteditable]")) studio.setTool("point");
11505
- });
11506
- l.add(studio.subscribe(draw));
11507
- return { dispose: () => l.off() };
11508
- }
11509
- /** The verb row on the canvas: undo and redo once there is a change, zoom once there is an image. */
11510
- function mountMaskVerbs(host, studio) {
11511
- const l = listeners();
11512
- let last = "";
11513
- const draw = (s) => {
11514
- const html = s.ready ? `${s.canUndo ? `<button type="button" class="hb-iconbtn" data-v="undo" aria-label="undo" title="undo (ctrl z)">${ICON.undo}</button>` : ""}${s.canRedo ? `<button type="button" class="hb-iconbtn" data-v="redo" aria-label="redo" title="redo (ctrl shift z)">${ICON.redo}</button>` : ""}` + (s.zoom !== 1 ? `<button type="button" class="hb-iconbtn" data-v="out" aria-label="zoom out" title="zoom out (-)">${ICON.zoomOut}</button><button type="button" class="hb-iconbtn" data-v="fit" aria-label="fit, now ${Math.round(s.zoom * 100)} percent" title="fit (0)">${ICON.fit}</button>` : "") + `<button type="button" class="hb-iconbtn" data-v="in" aria-label="zoom in" title="zoom in (+)">${ICON.zoomIn}</button>` + (s.coverage > 0 || s.found.length ? `<div class="hb-seg is-toggle" role="group" aria-label="view">${[
11515
- "composite",
11516
- "mask",
11517
- "instances"
11518
- ].filter((v) => v !== "instances" || s.found.length).map((v) => `<button type="button" data-view="${v}" class="${s.view === v ? "is-on" : ""}" aria-pressed="${s.view === v}">${v === "instances" ? "objects" : v === "composite" ? "picture" : "mask"}</button>`).join("")}</div>` : "") : "";
11519
- if (html !== last) host.innerHTML = last = html;
11520
- host.hidden = !html;
11521
- };
11522
- l.on(host, "click", (e) => {
11523
- const view = e.target.closest("[data-view]")?.dataset.view;
11524
- if (view) return studio.setView(view);
11525
- const v = e.target.closest("[data-v]")?.dataset.v;
11526
- if (v === "undo") studio.undo();
11527
- else if (v === "redo") studio.redo();
11528
- else if (v === "in") studio.zoom(1);
11529
- else if (v === "out") studio.zoom(-1);
11530
- else if (v === "fit") studio.zoom(0);
11531
- });
11532
- l.add(studio.subscribe(draw));
11533
- return { dispose: () => l.off() };
11534
- }
11535
- /** The rack parts: what was found, the mask tools' settings, and the exports once there is a mask. */
11536
- function mountMaskRack(host, studio, part, o = {}) {
11537
- const l = listeners();
11538
- let last = "";
11539
- const draw = (s) => {
11540
- let html = "";
11541
- if (part === "found") {
11542
- html = s.found.length ? `<div class="hb-inst"><p class="hb-inst-empty">${s.found.length} found, ${s.found.filter((f) => f.on).length} in the mask</p><div class="hb-toolbar hb-inst-foot"><button type="button" class="hb-btn hb-btn-go" data-op="merge">merge all</button><button type="button" class="hb-btn" data-op="unfind">clear all</button></div><ul class="hb-inst-list">${s.found.map((f, i) => `<li class="hb-inst-row${f.on || s.hover === i ? " is-on" : ""}${f.hidden ? " is-hidden" : ""}" style="--hb-inst-c:${f.colour}"><button type="button" class="hb-inst-pick" data-found="${i}" aria-pressed="${f.on}"><span class="hb-inst-sw"></span><span class="hb-inst-name">${esc(f.label)}</span><span class="hb-inst-score">${Math.min(100, Math.round(f.score * 100))}%</span></button><button type="button" class="hb-inst-act" data-show="${i}" aria-pressed="${!f.hidden}" aria-label="${f.hidden ? "show" : "hide"} ${esc(f.label)}" title="${f.hidden ? "show" : "hide"}">${rn("eye", { size: 14 })}</button><button type="button" class="hb-inst-act" data-drop="${i}" aria-label="delete ${esc(f.label)}" title="delete">${ICON.trash}</button></li>`).join("")}</ul></div>` : `<p class="hb-lay-empty-note">${s.ready ? studio.hasEverything || studio.canWant ? "find every object at once, or press a thing on the picture." : "press a thing to cut it out." : "nothing yet. open an image or try the sample."}</p>` + (s.ready && (studio.hasEverything || studio.canWant) && !s.busy ? `<button type="button" class="hb-btn hb-btn-go" data-all>find every object</button>` : "");
11543
- html += s.ready ? `<div class="hb-kv"><span class="hb-label">mask keeps</span><span class="hb-readout">${Math.round(s.coverage * 100)}%</span></div>` : "";
11544
- } else if (part === "mask") html = `<div class="hb-seg is-toggle" role="group" aria-label="mode">${["add", "erase"].map((m) => `<button type="button" data-mode="${m}" class="${s.mode === m ? "is-on" : ""}" aria-pressed="${s.mode === m}">${m}<span class="hb-mask-key" aria-hidden="true">x</span></button>`).join("")}</div>` + (s.tool === "brush" || s.tool === "eraser" ? `<label class="hb-field"><span class="hb-label">brush, ${s.size} px</span><input class="hb-range" type="range" min="4" max="160" step="2" value="${s.size}" data-size></label>` : "") + (s.tool === "wand" || s.tool === "flood" ? `<label class="hb-field"><span class="hb-label">reach, ${s.tolerance}</span><input class="hb-range" type="range" min="0" max="100" step="1" value="${s.tolerance}" data-tol></label>` : "") + (s.tool === "wand" ? `<label class="hb-check"><input type="checkbox" data-contig${s.contiguous ? " checked" : ""}><span>touching pixels only</span></label>` : "") + (s.coverage > 0 ? `<div class="hb-toolbar"><button type="button" class="hb-btn" data-op="smooth">smooth</button><button type="button" class="hb-btn" data-op="expand">expand</button><button type="button" class="hb-btn" data-op="shrink">shrink</button><button type="button" class="hb-btn" data-op="invert">invert</button><button type="button" class="hb-btn" data-op="clear">clear</button></div>` : "");
11545
- else if (part === "saved") html = (s.coverage > 0 ? `<div class="hb-toolbar"><button type="button" class="hb-btn" data-op="save">save this mask</button></div>` : "") + (s.saved.length ? `<div class="hb-inst"><ul class="hb-inst-list">${s.saved.map((m) => `<li class="hb-inst-row"><button type="button" class="hb-inst-pick" data-restore="${m.id}"><img class="hb-inst-sw" src="${m.thumb}" alt=""><span class="hb-inst-name">${esc(m.name)}</span><span class="hb-inst-score">${Math.round(m.coverage * 100)}%</span></button><button type="button" class="hb-inst-act" data-forget="${m.id}" aria-label="forget ${esc(m.name)}" title="forget">${ICON.x}</button></li>`).join("")}</ul></div>` : `<p class="hb-lay-empty-note">no saved masks. save one to come back to it.</p>`);
11546
- else html = s.coverage > 0 ? `<div class="hb-toolbar" role="group" aria-label="export"><button type="button" class="hb-btn" data-ex="cutout">cutout png</button><button type="button" class="hb-btn" data-ex="alpha">mask png</button><button type="button" class="hb-btn" data-ex="matte">matte png</button><button type="button" class="hb-btn" data-ex="inverse">inverse png</button></div>` : `<p class="hb-lay-empty-note">exports show once there is a mask.</p>`;
11547
- if (html !== last) host.innerHTML = last = html;
11548
- };
11549
- l.on(host, "click", (e) => {
11550
- const t = e.target;
11551
- const f = t.closest("[data-found]");
11552
- if (f) return studio.toggleFound(Number(f.dataset.found));
11553
- const m = t.closest("[data-mode]");
11554
- if (m) return studio.setMode(m.dataset.mode);
11555
- const show = t.closest("[data-show]");
11556
- if (show) return studio.showFound(Number(show.dataset.show), show.getAttribute("aria-pressed") !== "true");
11557
- const drop = t.closest("[data-drop]");
11558
- if (drop) return studio.removeFound(Number(drop.dataset.drop));
11559
- const op = t.closest("[data-op]")?.dataset.op;
11560
- if (op === "merge") return studio.mergeFound();
11561
- if (op === "unfind") return studio.clearFound();
11562
- if (op === "invert") return studio.invert();
11563
- if (op === "clear") return studio.clear();
11564
- if (op === "smooth") return studio.smooth();
11565
- if (op === "expand") return studio.expand();
11566
- if (op === "shrink") return studio.shrink();
11567
- if (op === "save") return studio.save();
11568
- const restore = t.closest("[data-restore]");
11569
- if (restore) return studio.restore(Number(restore.dataset.restore));
11570
- const forget = t.closest("[data-forget]");
11571
- if (forget) return studio.forget(Number(forget.dataset.forget));
11572
- const ex = t.closest("[data-ex]");
11573
- if (ex) {
11574
- const kind = ex.dataset.ex;
11575
- studio.exportAs(kind).then((b) => b && download(`${o.name ?? "mask"}-${kind}.png`, b));
11576
- return;
11577
- }
11578
- if (t.closest("[data-all]")) studio.everything();
11579
- });
11580
- l.on(host, "input", (e) => {
11581
- const i = e.target;
11582
- if (i.dataset.size !== void 0) studio.setSize(Number(i.value));
11583
- if (i.dataset.tol !== void 0) studio.setTolerance(Number(i.value));
11584
- if (i.dataset.contig !== void 0) studio.setContiguous(i.checked);
11585
- });
11586
- l.add(studio.subscribe(draw));
11587
- return { dispose: () => l.off() };
11588
- }
11589
11638
  // packages/ai-views/src/memory.ts
11590
11639
  const n3 = (x) => Number.isFinite(x) ? x.toFixed(3) : "none";
11591
11640
  const rank = (r) => r >= 0 ? `#${r + 1}` : "none";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hackbuild/ai-views",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "description": "Views that bind the browser AI packages to hackbuild-ui: a model picker per job, a chat pane and composer, a drop first transcriber, a mask studio, a frame view and a camera feed. Each ships as a Vue component and a custom element.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -44,15 +44,15 @@
44
44
  "@hackbuild/ai-env": "0.1.2",
45
45
  "@hackbuild/audio-dsp": "0.1.2",
46
46
  "@hackbuild/chat": "0.1.3",
47
- "@hackbuild/devices": "0.1.4",
48
- "@hackbuild/embed": "0.1.5",
47
+ "@hackbuild/devices": "0.1.5",
48
+ "@hackbuild/embed": "0.1.6",
49
49
  "@hackbuild/imagegen": "0.2.4",
50
50
  "@hackbuild/memory": "0.1.4",
51
51
  "@hackbuild/models": "0.2.3",
52
52
  "@hackbuild/speak": "0.1.4",
53
- "@hackbuild/transcribe": "0.1.4",
54
- "@hackbuild/vision": "0.2.2",
55
- "@hackbuild/weights": "0.1.3"
53
+ "@hackbuild/transcribe": "0.1.5",
54
+ "@hackbuild/vision": "0.2.3",
55
+ "@hackbuild/weights": "0.1.4"
56
56
  },
57
57
  "peerDependencies": {
58
58
  "@virgilvox/hackbuild-ui": ">=0.10.0 <0.11.0",