@liiift-studio/threadtext 0.1.0 → 0.2.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.
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import { useRef as wt, useLayoutEffect as me, useEffect as Qt, forwardRef as ye, useCallback as xe } from "react";
2
- import { jsx as ge } from "react/jsx-runtime";
3
- const Bt = {
1
+ import { useRef as Tt, useLayoutEffect as Se, useEffect as ne, forwardRef as Ae, useCallback as Fe } from "react";
2
+ import { jsx as Le } from "react/jsx-runtime";
3
+ const zt = {
4
4
  /** Base canvas: the baked satin stitches (transparent ground). */
5
5
  bg: "tt-bg",
6
6
  /** Overlay canvas: cursor sheen (`mix-blend-mode: screen`). */
@@ -8,95 +8,98 @@ const Bt = {
8
8
  /** Blinking text caret shown in editable mode. */
9
9
  caret: "tt-caret"
10
10
  };
11
- function J(m, e) {
12
- let o = Math.imul(m | 0, 374761393) + Math.imul(e | 0, 668265263) | 0;
13
- return o = Math.imul(o ^ o >>> 13, 1274126177), ((o ^ o >>> 16) >>> 0) / 4294967295;
11
+ function nt(x, o) {
12
+ let r = Math.imul(x | 0, 374761393) + Math.imul(o | 0, 668265263) | 0;
13
+ return r = Math.imul(r ^ r >>> 13, 1274126177), ((r ^ r >>> 16) >>> 0) / 4294967295;
14
14
  }
15
- function Zt(m, e) {
16
- const o = Math.floor(m), r = Math.floor(e), i = m - o, a = e - r, f = i * i * (3 - 2 * i), A = a * a * (3 - 2 * a), E = J(o, r), C = J(o + 1, r), c = J(o, r + 1), u = J(o + 1, r + 1);
17
- return E + (C - E) * f + (c - E) * A + (E - C - c + u) * f * A;
15
+ function oe(x, o) {
16
+ const r = Math.floor(x), l = Math.floor(o), c = x - r, s = o - l, h = c * c * (3 - 2 * c), L = s * s * (3 - 2 * s), S = nt(r, l), P = nt(r + 1, l), f = nt(r, l + 1), m = nt(r + 1, l + 1);
17
+ return S + (P - S) * h + (f - S) * L + (S - P - f + m) * h * L;
18
18
  }
19
- const pt = 1e20;
20
- function Ht(m, e, o, r, i) {
21
- let a = 0;
22
- r[0] = 0, i[0] = -pt, i[1] = pt;
23
- for (let f = 1; f < e; f++) {
24
- let A = (m[f] + f * f - (m[r[a]] + r[a] * r[a])) / (2 * f - 2 * r[a]);
25
- for (; A <= i[a]; )
26
- a--, A = (m[f] + f * f - (m[r[a]] + r[a] * r[a])) / (2 * f - 2 * r[a]);
27
- a++, r[a] = f, i[a] = A, i[a + 1] = pt;
28
- }
29
- a = 0;
30
- for (let f = 0; f < e; f++) {
31
- for (; i[a + 1] < f; ) a++;
32
- o[f] = (f - r[a]) * (f - r[a]) + m[r[a]];
19
+ const St = 1e20;
20
+ function re(x, o, r, l, c) {
21
+ let s = 0;
22
+ l[0] = 0, c[0] = -St, c[1] = St;
23
+ for (let h = 1; h < o; h++) {
24
+ let L = (x[h] + h * h - (x[l[s]] + l[s] * l[s])) / (2 * h - 2 * l[s]);
25
+ for (; L <= c[s]; )
26
+ s--, L = (x[h] + h * h - (x[l[s]] + l[s] * l[s])) / (2 * h - 2 * l[s]);
27
+ s++, l[s] = h, c[s] = L, c[s + 1] = St;
28
+ }
29
+ s = 0;
30
+ for (let h = 0; h < o; h++) {
31
+ for (; c[s + 1] < h; ) s++;
32
+ r[h] = (h - l[s]) * (h - l[s]) + x[l[s]];
33
33
  }
34
34
  }
35
- function we(m, e, o) {
36
- const r = new Float64Array(e * o);
37
- for (let c = 0; c < e * o; c++) r[c] = m[c] ? pt : 0;
38
- const i = Math.max(e, o), a = new Float64Array(i), f = new Float64Array(i), A = new Int32Array(i), E = new Float64Array(i + 1);
39
- for (let c = 0; c < e; c++) {
40
- for (let u = 0; u < o; u++) a[u] = r[u * e + c];
41
- Ht(a, o, f, A, E);
42
- for (let u = 0; u < o; u++) r[u * e + c] = f[u];
43
- }
44
- for (let c = 0; c < o; c++) {
45
- for (let u = 0; u < e; u++) a[u] = r[c * e + u];
46
- Ht(a, e, f, A, E);
47
- for (let u = 0; u < e; u++) r[c * e + u] = f[u];
48
- }
49
- const C = new Float32Array(e * o);
50
- for (let c = 0; c < e * o; c++) C[c] = Math.sqrt(r[c]);
51
- return C;
35
+ function Re(x, o, r) {
36
+ const l = new Float64Array(o * r);
37
+ for (let f = 0; f < o * r; f++) l[f] = x[f] ? St : 0;
38
+ const c = Math.max(o, r), s = new Float64Array(c), h = new Float64Array(c), L = new Int32Array(c), S = new Float64Array(c + 1);
39
+ for (let f = 0; f < o; f++) {
40
+ for (let m = 0; m < r; m++) s[m] = l[m * o + f];
41
+ re(s, r, h, L, S);
42
+ for (let m = 0; m < r; m++) l[m * o + f] = h[m];
43
+ }
44
+ for (let f = 0; f < r; f++) {
45
+ for (let m = 0; m < o; m++) s[m] = l[f * o + m];
46
+ re(s, o, h, L, S);
47
+ for (let m = 0; m < o; m++) l[f * o + m] = h[m];
48
+ }
49
+ const P = new Float32Array(o * r);
50
+ for (let f = 0; f < o * r; f++) P[f] = Math.sqrt(l[f]);
51
+ return P;
52
52
  }
53
- function Wt(m, e, o, r, i) {
54
- let a = Float32Array.from(m);
55
- const f = new Float32Array(e * o), A = 1 / (2 * r + 1), E = (C, c) => C < 0 ? 0 : C > c ? c : C;
56
- for (let C = 0; C < i; C++) {
57
- for (let c = 0; c < o; c++) {
58
- const u = c * e;
59
- let $ = 0;
60
- for (let D = -r; D <= r; D++) $ += a[u + E(D, e - 1)];
61
- for (let D = 0; D < e; D++)
62
- f[u + D] = $ * A, $ += a[u + E(D + r + 1, e - 1)] - a[u + E(D - r, e - 1)];
53
+ function ie(x, o, r, l, c) {
54
+ let s = Float32Array.from(x);
55
+ const h = new Float32Array(o * r), L = 1 / (2 * l + 1), S = (P, f) => P < 0 ? 0 : P > f ? f : P;
56
+ for (let P = 0; P < c; P++) {
57
+ for (let f = 0; f < r; f++) {
58
+ const m = f * o;
59
+ let B = 0;
60
+ for (let q = -l; q <= l; q++) B += s[m + S(q, o - 1)];
61
+ for (let q = 0; q < o; q++)
62
+ h[m + q] = B * L, B += s[m + S(q + l + 1, o - 1)] - s[m + S(q - l, o - 1)];
63
63
  }
64
- for (let c = 0; c < e; c++) {
65
- let u = 0;
66
- for (let $ = -r; $ <= r; $++) u += f[E($, o - 1) * e + c];
67
- for (let $ = 0; $ < o; $++)
68
- a[$ * e + c] = u * A, u += f[E($ + r + 1, o - 1) * e + c] - f[E($ - r, o - 1) * e + c];
64
+ for (let f = 0; f < o; f++) {
65
+ let m = 0;
66
+ for (let B = -l; B <= l; B++) m += h[S(B, r - 1) * o + f];
67
+ for (let B = 0; B < r; B++)
68
+ s[B * o + f] = m * L, m += h[S(B + l + 1, r - 1) * o + f] - h[S(B - l, r - 1) * o + f];
69
69
  }
70
70
  }
71
- return a;
71
+ return s;
72
72
  }
73
- function te(m, e) {
74
- if (!m) return e;
75
- const o = m.trim();
76
- if (o[0] === "#") {
77
- let i = o.slice(1);
78
- if (i.length === 3 && (i = i[0] + i[0] + i[1] + i[1] + i[2] + i[2]), i.length >= 6) {
79
- const a = parseInt(i.slice(0, 2), 16), f = parseInt(i.slice(2, 4), 16), A = parseInt(i.slice(4, 6), 16);
80
- if (!Number.isNaN(a) && !Number.isNaN(f) && !Number.isNaN(A)) return [a, f, A];
73
+ function ae(x, o) {
74
+ if (!x) return o;
75
+ const r = x.trim();
76
+ if (r[0] === "#") {
77
+ let c = r.slice(1);
78
+ if (c.length === 3 && (c = c[0] + c[0] + c[1] + c[1] + c[2] + c[2]), c.length >= 6) {
79
+ const s = parseInt(c.slice(0, 2), 16), h = parseInt(c.slice(2, 4), 16), L = parseInt(c.slice(4, 6), 16);
80
+ if (!Number.isNaN(s) && !Number.isNaN(h) && !Number.isNaN(L)) return [s, h, L];
81
81
  }
82
- return e;
82
+ return o;
83
83
  }
84
- const r = o.match(/rgba?\(([^)]+)\)/i);
85
- if (r) {
86
- const i = r[1].split(",").map((a) => parseFloat(a));
87
- if (i.length >= 3 && i.slice(0, 3).every((a) => !Number.isNaN(a)))
88
- return [Math.round(i[0]), Math.round(i[1]), Math.round(i[2])];
84
+ const l = r.match(/rgba?\(([^)]+)\)/i);
85
+ if (l) {
86
+ const c = l[1].split(",").map((s) => parseFloat(s));
87
+ if (c.length >= 3 && c.slice(0, 3).every((s) => !Number.isNaN(s)))
88
+ return [Math.round(c[0]), Math.round(c[1]), Math.round(c[2])];
89
89
  }
90
- return e;
90
+ return o;
91
91
  }
92
- function K(m) {
93
- return m < 0 ? 0 : m > 255 ? 255 : Math.round(m);
92
+ function J(x) {
93
+ return x < 0 ? 0 : x > 255 ? 255 : Math.round(x);
94
94
  }
95
- function ee(m) {
96
- return (m.split(",")[0] || "serif").trim().replace(/^["']|["']$/g, "");
95
+ function Et(x, o, r) {
96
+ return x < o ? o : x > r ? r : x;
97
97
  }
98
- function pe(m, e) {
99
- if (typeof window > "u" || typeof document > "u" || !m)
98
+ function le(x) {
99
+ return (x.split(",")[0] || "serif").trim().replace(/^["']|["']$/g, "");
100
+ }
101
+ function Ie(x, o) {
102
+ if (typeof window > "u" || typeof document > "u" || !x)
100
103
  return { setText() {
101
104
  }, replay() {
102
105
  }, resize() {
@@ -104,390 +107,475 @@ function pe(m, e) {
104
107
  }, focus() {
105
108
  }, destroy() {
106
109
  }, get text() {
107
- return e.text ?? "";
110
+ return o.text ?? "";
108
111
  } };
109
- let o = e.text ?? "", r = e.font ?? "Georgia, serif", i = e.weight ?? 680, a = e.fill ?? 0.9, f = e.sewRate ?? 110, A = e.sheen ?? !0, E = e.animate ?? !0, C = e.editable ?? !1, c = e.pitch, u = e.onTextChange, $ = ee(r);
110
- const D = e.reducedMotion ?? !!(window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches);
111
- let k = te(e.threadColor ?? "#fffbf3", [255, 251, 243]), W = [0, 0, 0], N = [0, 0, 0];
112
- function tt(t) {
113
- k = te(t, [255, 251, 243]), W = [k[0] * 0.585, k[1] * 0.585, k[2] * 0.585], N = [k[0] * 0.47, k[1] * 0.47, k[2] * 0.47];
114
- }
115
- tt(e.threadColor ?? "#fffbf3");
116
- const G = [];
117
- let P, g;
118
- m instanceof HTMLCanvasElement ? (P = m, g = m.parentElement ?? m) : (g = m, P = document.createElement("canvas"), G.push(P), g.appendChild(P)), P.classList.add(Bt.bg);
119
- const O = document.createElement("canvas");
120
- G.push(O), O.classList.add(Bt.fx), g.appendChild(O), getComputedStyle(g).position === "static" && (g.style.position = "relative"), P.style.display = "block", P.style.width = "100%", Object.assign(O.style, { position: "absolute", inset: "0", width: "100%", pointerEvents: "none", mixBlendMode: "screen" });
121
- let S = null, Q = !1, s = 0, h = 0, Z = 0, Mt = 0, st = 4, et = [], nt = 0, ot = 0, bt = new Uint8Array(0), Ct = new Float32Array(0), ft = new Float32Array(0), dt = new Float32Array(0), U = null, rt = null, ut = null, ht = [], mt = 3;
122
- const R = { rows: [], idx: 0, acc: 0, rate: 0, on: !1 }, V = { x: 0, y: 0, set: !1 };
123
- let H = !0, vt = 0, Dt = !1, yt = 0, z = !1, Y = !1;
124
- function ne() {
125
- const t = g.getBoundingClientRect(), n = Math.max(1, Math.round(t.width || Math.min(window.innerWidth * 0.95, 1240))), l = o || "Ag", y = P.getContext("2d");
126
- let v = 100;
127
- y && (y.font = `${i} 100px ${r}`, v = y.measureText(l).width || 100);
128
- let b = Math.max(8, 100 * (n * a) / v), T = b * 0.7, L = b * 0.3, I = n * a;
129
- if (y) {
130
- y.font = `${i} ${b}px ${r}`;
131
- const p = y.measureText(l);
132
- I = p.width || I, p.actualBoundingBoxAscent > 0 && (T = p.actualBoundingBoxAscent), p.actualBoundingBoxDescent > 0 && (L = p.actualBoundingBoxDescent);
112
+ let r = o.text ?? "", l = o.font ?? "Georgia, serif", c = Et(o.weight ?? 680, 1, 1e3), s = Et(o.fill ?? 0.9, 0.05, 1), h = Math.max(1, o.sewRate ?? 110), L = o.sewStyle === "hand" ? "hand" : "machine", S = o.sheen ?? !0, P = o.animate ?? !0, f = o.editable ?? !1, m = o.pitch, B = o.onTextChange, q = le(l);
113
+ const gt = o.reducedMotion ?? !!(window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches);
114
+ let N = ae(o.threadColor ?? "#fffbf3", [255, 251, 243]), ft = [0, 0, 0], U = [0, 0, 0];
115
+ function dt(t) {
116
+ N = ae(t, [255, 251, 243]), ft = [N[0] * 0.585, N[1] * 0.585, N[2] * 0.585], U = [N[0] * 0.47, N[1] * 0.47, N[2] * 0.47];
117
+ }
118
+ dt(o.threadColor ?? "#fffbf3");
119
+ const Y = [];
120
+ let O, k;
121
+ x instanceof HTMLCanvasElement ? (O = x, k = x.parentElement ?? x) : (k = x, O = document.createElement("canvas"), Y.push(O), k.appendChild(O)), O.classList.add(zt.bg);
122
+ const X = document.createElement("canvas");
123
+ Y.push(X), X.classList.add(zt.fx), k.appendChild(X), getComputedStyle(k).position === "static" && (k.style.position = "relative"), O.style.display = "block", O.style.width = "100%", Object.assign(X.style, { position: "absolute", inset: "0", width: "100%", pointerEvents: "none", mixBlendMode: "screen" });
124
+ let w = null, F = null, W = !1, d = 0, g = 0, ot = 0, At = 0, rt = 4, it = [], ut = 0, ht = 0, qt = -1, _t = "", Gt = -1, Ut = -1, Ft = new Uint8Array(0), Lt = new Float32Array(0), pt = new Float32Array(0), wt = new Float32Array(0), Q = null, at = null, Mt = null, lt = [], tt = 3;
125
+ const I = { rows: [], idx: 0, acc: 0, rate: 0, on: !1 }, H = { x: 0, y: 0, set: !1 };
126
+ let et = !0, Rt = 0, mt = !1, yt = 0, K = !1, Z = !1;
127
+ function ce() {
128
+ const t = k.getBoundingClientRect(), e = Math.max(1, Math.round(t.width || Math.min(window.innerWidth * 0.95, 1240))), i = r || "Ag", u = O.getContext("2d");
129
+ u && (u.textBaseline = "middle");
130
+ let T = 100;
131
+ u && (u.font = `${c} 100px ${l}`, T = u.measureText(i).width || 100);
132
+ const C = Math.max(8, 100 * (e * s) / T);
133
+ let b = C * 0.62, A = C * 0.62, E = e * s;
134
+ if (u) {
135
+ u.font = `${c} ${C}px ${l}`;
136
+ const a = u.measureText(i);
137
+ E = a.width || E, a.actualBoundingBoxAscent > 0 && (b = a.actualBoundingBoxAscent), a.actualBoundingBoxDescent > 0 && (A = a.actualBoundingBoxDescent);
133
138
  }
134
- const w = Math.max(1, Math.round(2 * Math.max(T, L) * 1.12));
135
- let d = Math.min(window.devicePixelRatio || 1, 1.7);
136
- for (; n * d * w * d > 9e5 && d > 0.4; ) d -= 0.1;
137
- s = Math.max(1, Math.round(n * d)), h = Math.max(1, Math.round(w * d)), Z = Math.max(4, Math.round(b * d)), Mt = Math.round(s / 2 - I * d / 2);
138
- for (const p of [P, O])
139
- p.width = s, p.height = h, p.style.height = w + "px";
140
- g.style.height = w + "px", st = c ?? Math.max(3, h * 95e-4);
141
- }
142
- function oe() {
143
- rt = document.createElement("canvas"), rt.width = s, rt.height = h, ut = rt.getContext("2d", { willReadFrequently: !0 });
144
- }
145
- function Nt() {
146
- const t = Math.max(7, st * 2.5), n = Math.max(3.2, st * 1.3);
147
- nt = Math.ceil(t) + 2, ot = Math.ceil(n) + 2, et = [];
148
- for (let l = 0; l < 20; l++) {
149
- const y = 0.34 + l / 19 * 0.82, v = document.createElement("canvas");
150
- v.width = nt, v.height = ot;
151
- const b = v.getContext("2d");
152
- if (!b) {
153
- et.push(v);
139
+ const M = Math.max(1, Math.round(2 * Math.max(b, A) * 1.1));
140
+ let n = Math.min(window.devicePixelRatio || 1, 1.7);
141
+ for (; e * n * M * n > 9e5 && n > 0.4; ) n -= 0.1;
142
+ d = Math.max(1, Math.round(e * n)), g = Math.max(1, Math.round(M * n)), ot = Math.max(4, Math.round(C * n)), At = Math.round(d / 2 - E * n / 2);
143
+ for (const a of [O, X])
144
+ a.width = d, a.height = g, a.style.height = M + "px";
145
+ k.style.height = M + "px", rt = m ?? Math.max(3, g * 95e-4);
146
+ }
147
+ function se() {
148
+ at && Gt === d && Ut === g || (at = document.createElement("canvas"), at.width = d, at.height = g, Mt = at.getContext("2d", { willReadFrequently: !0 }), Gt = d, Ut = g);
149
+ }
150
+ function Xt() {
151
+ const t = Math.max(7, rt * 2.5), e = Math.max(3.2, rt * 1.3);
152
+ ut = Math.ceil(t) + 2, ht = Math.ceil(e) + 2, it = [];
153
+ for (let i = 0; i < 20; i++) {
154
+ const u = 0.34 + i / 19 * 0.82, T = document.createElement("canvas");
155
+ T.width = ut, T.height = ht;
156
+ const C = T.getContext("2d");
157
+ if (!C) {
158
+ it.push(T);
154
159
  continue;
155
160
  }
156
- const T = nt / 2, L = ot / 2, I = b.createLinearGradient(0, L - n / 2, 0, L + n / 2), w = (B, j) => `rgba(${K(B[0] * y)},${K(B[1] * y)},${K(B[2] * y)},${j})`;
157
- I.addColorStop(0, w(N, 0)), I.addColorStop(0.16, w(W, 1)), I.addColorStop(0.5, w(k, 1)), I.addColorStop(0.84, w(W, 1)), I.addColorStop(1, w(N, 0)), b.fillStyle = I, b.beginPath();
158
- const d = n / 2;
159
- b.moveTo(T - t / 2 + d, L - d), b.lineTo(T + t / 2 - d, L - d), b.arc(T + t / 2 - d, L, d, -Math.PI / 2, Math.PI / 2), b.lineTo(T - t / 2 + d, L + d), b.arc(T - t / 2 + d, L, d, Math.PI / 2, -Math.PI / 2), b.closePath(), b.fill(), b.globalCompositeOperation = "destination-in";
160
- const p = b.createLinearGradient(T - t / 2, 0, T + t / 2, 0);
161
- p.addColorStop(0, "rgba(0,0,0,0)"), p.addColorStop(0.22, "rgba(0,0,0,1)"), p.addColorStop(0.78, "rgba(0,0,0,1)"), p.addColorStop(1, "rgba(0,0,0,0)"), b.fillStyle = p, b.fillRect(0, 0, nt, ot), et.push(v);
161
+ const b = ut / 2, A = ht / 2, E = C.createLinearGradient(0, A - e / 2, 0, A + e / 2), M = (R, $) => `rgba(${J(R[0] * u)},${J(R[1] * u)},${J(R[2] * u)},${$})`;
162
+ E.addColorStop(0, M(U, 0)), E.addColorStop(0.16, M(ft, 1)), E.addColorStop(0.5, M(N, 1)), E.addColorStop(0.84, M(ft, 1)), E.addColorStop(1, M(U, 0)), C.fillStyle = E, C.beginPath();
163
+ const n = e / 2;
164
+ C.moveTo(b - t / 2 + n, A - n), C.lineTo(b + t / 2 - n, A - n), C.arc(b + t / 2 - n, A, n, -Math.PI / 2, Math.PI / 2), C.lineTo(b - t / 2 + n, A + n), C.arc(b - t / 2 + n, A, n, Math.PI / 2, -Math.PI / 2), C.closePath(), C.fill(), C.globalCompositeOperation = "destination-in";
165
+ const a = C.createLinearGradient(b - t / 2, 0, b + t / 2, 0);
166
+ a.addColorStop(0, "rgba(0,0,0,0)"), a.addColorStop(0.22, "rgba(0,0,0,1)"), a.addColorStop(0.78, "rgba(0,0,0,1)"), a.addColorStop(1, "rgba(0,0,0,0)"), C.fillStyle = a, C.fillRect(0, 0, ut, ht), it.push(T);
162
167
  }
168
+ qt = rt, _t = N.join(",");
163
169
  }
164
- function re() {
165
- if (!ut) return;
166
- const t = ut;
167
- t.clearRect(0, 0, s, h), t.fillStyle = "#fff", t.textAlign = "left", t.textBaseline = "middle", t.font = `${i} ${Z}px ${r}`, t.fillText(o, Mt, h * 0.5);
168
- const n = t.getImageData(0, 0, s, h).data, l = new Uint8Array(s * h);
169
- for (let x = 0; x < h; x++) for (let M = 0; M < s; M++) {
170
- const F = x * s + M, q = n[F * 4 + 3] / 255, _ = Zt(M * 0.5, x * 0.5) * 0.22 + Zt(M * 1.8, x * 1.8) * 0.1;
171
- l[F] = q > 0.5 + (_ - 0.16) ? 1 : 0;
170
+ function fe() {
171
+ it.length && qt === rt && _t === N.join(",") || Xt();
172
+ }
173
+ function de() {
174
+ if (!Mt) return;
175
+ const t = Mt;
176
+ t.clearRect(0, 0, d, g), t.fillStyle = "#fff", t.textAlign = "left", t.textBaseline = "middle", t.font = `${c} ${ot}px ${l}`, t.fillText(r, At, g * 0.5);
177
+ const e = t.getImageData(0, 0, d, g).data, i = new Uint8Array(d * g);
178
+ for (let y = 0; y < g; y++) for (let p = 0; p < d; p++) {
179
+ const v = y * d + p, _ = e[v * 4 + 3] / 255, z = oe(p * 0.5, y * 0.5) * 0.22 + oe(p * 1.8, y * 1.8) * 0.1;
180
+ i[v] = _ > 0.5 + (z - 0.16) ? 1 : 0;
172
181
  }
173
- bt = l;
174
- const y = we(l, s, h), v = new Float32Array(s * h);
175
- for (let x = 1; x < h - 1; x++) for (let M = 1; M < s - 1; M++) {
176
- const F = x * s + M;
177
- v[F] = (y[F] * 4 + y[F - 1] + y[F + 1] + y[F - s] + y[F + s]) / 8;
182
+ Ft = i;
183
+ const u = Re(i, d, g), T = new Float32Array(d * g);
184
+ for (let y = 1; y < g - 1; y++) for (let p = 1; p < d - 1; p++) {
185
+ const v = y * d + p;
186
+ T[v] = (u[v] * 4 + u[v - 1] + u[v + 1] + u[v - d] + u[v + d]) / 8;
178
187
  }
179
- const b = new Float32Array(s * h), T = new Float32Array(s * h), L = new Float32Array(s * h), I = new Float32Array(s * h);
180
- for (let x = 1; x < h - 1; x++) for (let M = 1; M < s - 1; M++) {
181
- const F = x * s + M;
182
- if (!l[F]) continue;
183
- const q = (v[F + 1] - v[F - 1]) * 0.5, _ = (v[F + s] - v[F - s]) * 0.5;
184
- L[F] = q, I[F] = _, b[F] = q * q - _ * _, T[F] = 2 * q * _;
188
+ const C = new Float32Array(d * g), b = new Float32Array(d * g), A = new Float32Array(d * g), E = new Float32Array(d * g);
189
+ for (let y = 1; y < g - 1; y++) for (let p = 1; p < d - 1; p++) {
190
+ const v = y * d + p;
191
+ if (!i[v]) continue;
192
+ const _ = (T[v + 1] - T[v - 1]) * 0.5, z = (T[v + d] - T[v - d]) * 0.5;
193
+ A[v] = _, E[v] = z, C[v] = _ * _ - z * z, b[v] = 2 * _ * z;
185
194
  }
186
- const w = Math.max(7, Math.round(h * 0.05)), d = Wt(b, s, h, w, 2), p = Wt(T, s, h, w, 2);
187
- ft = new Float32Array(s * h), dt = new Float32Array(s * h);
188
- for (let x = 0; x < s * h; x++) {
189
- if (!l[x]) continue;
190
- const M = 0.5 * Math.atan2(p[x], d[x]);
191
- ft[x] = Math.cos(M), dt[x] = Math.sin(M);
195
+ const M = Math.max(7, Math.round(g * 0.05)), n = ie(C, d, g, M, 2), a = ie(b, d, g, M, 2);
196
+ pt = new Float32Array(d * g), wt = new Float32Array(d * g);
197
+ for (let y = 0; y < d * g; y++) {
198
+ if (!i[y]) continue;
199
+ const p = 0.5 * Math.atan2(a[y], n[y]);
200
+ pt[y] = Math.cos(p), wt[y] = Math.sin(p);
192
201
  }
193
- const B = Math.max(9, h * 0.04), j = new Float32Array(s * h), lt = -0.45, ct = -0.62, gt = 0.64;
194
- for (let x = 1; x < h - 1; x++) for (let M = 1; M < s - 1; M++) {
195
- const F = x * s + M;
196
- if (!l[F]) continue;
197
- let q = L[F], _ = I[F];
198
- const Xt = Math.hypot(q, _) || 1;
199
- q /= Xt, _ /= Xt;
200
- const Vt = v[F], Yt = (1 - Math.min(Vt / B, 1)) * 1.25;
201
- let $t = -q * Yt, It = -_ * Yt;
202
- const Jt = 1.5, Pt = Math.hypot($t, It, Jt);
203
- $t /= Pt, It /= Pt;
204
- let kt = $t * lt + It * ct + Jt / Pt * gt;
205
- kt < 0 && (kt = 0);
206
- const he = Math.min(1, 0.45 + 0.55 * Math.min(Vt / (B * 0.7), 1));
207
- j[F] = Math.min(1.12, (0.5 + 0.75 * kt) * he);
202
+ const R = Math.max(9, g * 0.04), $ = new Float32Array(d * g), G = -0.45, V = -0.62, D = 0.64;
203
+ for (let y = 1; y < g - 1; y++) for (let p = 1; p < d - 1; p++) {
204
+ const v = y * d + p;
205
+ if (!i[v]) continue;
206
+ let _ = A[v], z = E[v];
207
+ const xt = Math.hypot(_, z) || 1;
208
+ _ /= xt, z /= xt;
209
+ const Wt = T[v], te = (1 - Math.min(Wt / R, 1)) * 1.25;
210
+ let Nt = -_ * te, Ot = -z * te;
211
+ const ee = 1.5, Dt = Math.hypot(Nt, Ot, ee);
212
+ Nt /= Dt, Ot /= Dt;
213
+ let jt = Nt * G + Ot * V + ee / Dt * D;
214
+ jt < 0 && (jt = 0);
215
+ const Ee = Math.min(1, 0.45 + 0.55 * Math.min(Wt / (R * 0.7), 1));
216
+ $[v] = Math.min(1.12, (0.5 + 0.75 * jt) * Ee);
208
217
  }
209
- Ct = j, U = document.createElement("canvas"), U.width = s, U.height = h;
210
- const X = U.getContext("2d");
211
- if (X) {
212
- const x = X.createImageData(s, h);
213
- for (let M = 0; M < s * h; M++) l[M] && (x.data[M * 4] = 255, x.data[M * 4 + 1] = 255, x.data[M * 4 + 2] = 255, x.data[M * 4 + 3] = 255);
214
- X.putImageData(x, 0, 0);
218
+ Lt = $, Q = document.createElement("canvas"), Q.width = d, Q.height = g;
219
+ const j = Q.getContext("2d");
220
+ if (j) {
221
+ const y = j.createImageData(d, g);
222
+ for (let p = 0; p < d * g; p++) i[p] && (y.data[p * 4] = 255, y.data[p * 4 + 1] = 255, y.data[p * 4 + 2] = 255, y.data[p * 4 + 3] = 255);
223
+ j.putImageData(y, 0, 0);
215
224
  }
216
225
  }
217
- function ie() {
218
- const t = Math.max(1.6, st * 0.6);
219
- mt = t;
220
- const n = [];
221
- for (let l = 0; l < h; l += t) for (let y = 0; y < s; y += t) {
222
- const v = (J(Math.floor(y), Math.floor(l)) - 0.5) * t * 0.9, b = (J(Math.floor(l), Math.floor(y)) - 0.5) * t * 0.9, T = Math.round(y + v), L = Math.round(l + b);
223
- if (T < 1 || L < 1 || T >= s - 1 || L >= h - 1) continue;
224
- const I = L * s + T;
225
- if (!bt[I]) continue;
226
- const w = Math.atan2(dt[I], ft[I]), d = 0.86 + J(T * 7, L * 13) * 0.18;
227
- let p = Ct[I] * d;
228
- p = p < 0 ? 0 : p > 1.12 ? 1.12 : p, n.push({ x: T, y: L, ang: w, idx: Math.min(19, Math.max(0, Math.round(p / 1.12 * 19))) });
226
+ function ue() {
227
+ const t = Math.max(1.6, rt * 0.6);
228
+ tt = t;
229
+ const e = [];
230
+ for (let i = 0; i < g; i += t) for (let u = 0; u < d; u += t) {
231
+ const T = (nt(Math.floor(u), Math.floor(i)) - 0.5) * t * 0.9, C = (nt(Math.floor(i), Math.floor(u)) - 0.5) * t * 0.9, b = Math.round(u + T), A = Math.round(i + C);
232
+ if (b < 1 || A < 1 || b >= d - 1 || A >= g - 1) continue;
233
+ const E = A * d + b;
234
+ if (!Ft[E]) continue;
235
+ const M = Math.atan2(wt[E], pt[E]), n = 0.86 + nt(b * 7, A * 13) * 0.18;
236
+ let a = Lt[E] * n;
237
+ a = a < 0 ? 0 : a > 1.12 ? 1.12 : a, e.push({ x: b, y: A, ang: M, idx: Math.min(19, Math.max(0, Math.round(a / 1.12 * 19))) });
229
238
  }
230
- n.sort((l, y) => l.x - y.x), ht = n;
239
+ lt = e;
231
240
  }
232
- function Ot(t, n) {
233
- t.save(), t.translate(n.x, n.y), t.rotate(n.ang), t.drawImage(et[n.idx], -nt / 2, -ot / 2), t.restore();
241
+ function Kt(t, e) {
242
+ t.save(), t.translate(e.x, e.y), t.rotate(e.ang), t.drawImage(it[e.idx], -ut / 2, -ht / 2), t.restore();
234
243
  }
235
- function Tt() {
236
- const t = P.getContext("2d");
244
+ function It() {
245
+ const t = O.getContext("2d");
237
246
  if (t)
238
- for (const n of ht) Ot(t, n);
247
+ for (const e of lt) Kt(t, e);
239
248
  }
240
- function At() {
241
- const t = P.getContext("2d");
242
- t && t.clearRect(0, 0, s, h);
249
+ function $t() {
250
+ const t = O.getContext("2d");
251
+ t && t.clearRect(0, 0, d, g);
243
252
  }
244
- function ae(t) {
253
+ function he(t) {
245
254
  if (!t.length) return [];
246
- const n = Math.max(4, mt * 1.5), l = /* @__PURE__ */ new Map(), y = (w, d) => w + "," + d;
247
- t.forEach((w, d) => {
248
- const p = y(Math.floor(w.x / n), Math.floor(w.y / n));
249
- let B = l.get(p);
250
- B || l.set(p, B = []), B.push(d);
255
+ const e = Math.max(4, tt * 1.5), i = /* @__PURE__ */ new Map(), u = (M, n) => M + "," + n;
256
+ t.forEach((M, n) => {
257
+ const a = u(Math.floor(M.x / e), Math.floor(M.y / e));
258
+ let R = i.get(a);
259
+ R || i.set(a, R = []), R.push(n);
251
260
  });
252
- const v = mt * 1.8 * (mt * 1.8);
253
- function b(w) {
254
- const d = t[w], p = Math.floor(d.x / n), B = Math.floor(d.y / n), j = [];
255
- for (let lt = -1; lt <= 1; lt++) for (let ct = -1; ct <= 1; ct++) {
256
- const gt = l.get(y(p + lt, B + ct));
257
- if (gt)
258
- for (const X of gt) {
259
- if (X === w) continue;
260
- const x = t[X].x - d.x, M = t[X].y - d.y;
261
- x * x + M * M <= v && j.push(X);
261
+ const T = tt * 1.8 * (tt * 1.8);
262
+ function C(M) {
263
+ const n = t[M], a = Math.floor(n.x / e), R = Math.floor(n.y / e), $ = [];
264
+ for (let G = -1; G <= 1; G++) for (let V = -1; V <= 1; V++) {
265
+ const D = i.get(u(a + G, R + V));
266
+ if (D)
267
+ for (const j of D) {
268
+ if (j === M) continue;
269
+ const y = t[j].x - n.x, p = t[j].y - n.y;
270
+ y * y + p * p <= T && $.push(j);
262
271
  }
263
272
  }
264
- return j;
273
+ return $;
265
274
  }
266
- const T = new Uint8Array(t.length), L = [], I = t.map((w, d) => d).sort((w, d) => t[w].x + t[w].y - (t[d].x + t[d].y));
267
- for (const w of I) {
268
- if (T[w]) continue;
269
- let d = [w];
270
- for (T[w] = 1; d.length; ) {
271
- L.push(d.map((B) => t[B]));
272
- const p = [];
273
- for (const B of d) for (const j of b(B)) T[j] || (T[j] = 1, p.push(j));
274
- d = p;
275
+ const b = new Uint8Array(t.length), A = [], E = t.map((M, n) => n).sort((M, n) => t[M].x + t[M].y - (t[n].x + t[n].y));
276
+ for (const M of E) {
277
+ if (b[M]) continue;
278
+ let n = [M];
279
+ for (b[M] = 1; n.length; ) {
280
+ A.push(n.map((R) => t[R]));
281
+ const a = [];
282
+ for (const R of n) for (const $ of C(R)) b[$] || (b[$] = 1, a.push($));
283
+ n = a;
275
284
  }
276
285
  }
277
- return L;
286
+ return A;
278
287
  }
279
- function le() {
280
- if (At(), D || !E) {
281
- Tt(), R.on = !1;
288
+ function me(t) {
289
+ if (!t.length) return [];
290
+ const e = Math.max(4, tt * 1.5), i = /* @__PURE__ */ new Map(), u = (n, a) => n + "," + a;
291
+ t.forEach((n, a) => {
292
+ const R = u(Math.floor(n.x / e), Math.floor(n.y / e));
293
+ let $ = i.get(R);
294
+ $ || i.set(R, $ = []), $.push(a);
295
+ });
296
+ const T = tt * 1.8 * (tt * 1.8), C = (n) => {
297
+ const a = t[n], R = u(Math.floor(a.x / e), Math.floor(a.y / e)), $ = i.get(R);
298
+ if (!$) return;
299
+ const G = $.indexOf(n);
300
+ G >= 0 && $.splice(G, 1), $.length || i.delete(R);
301
+ };
302
+ function b(n) {
303
+ const a = t[n], R = Math.floor(a.x / e), $ = Math.floor(a.y / e);
304
+ let G = -1, V = 1 / 0;
305
+ for (let D = -1; D <= 1; D++) for (let j = -1; j <= 1; j++) {
306
+ const y = i.get(u(R + D, $ + j));
307
+ if (y)
308
+ for (const p of y) {
309
+ const v = t[p].x - a.x, _ = t[p].y - a.y, z = v * v + _ * _;
310
+ z <= T && z < V && (V = z, G = p);
311
+ }
312
+ }
313
+ return G;
314
+ }
315
+ function A(n) {
316
+ const a = t[n], R = Math.floor(a.x / e), $ = Math.floor(a.y / e);
317
+ let G = -1, V = 1 / 0;
318
+ for (let D = 1; D < 512; D++) {
319
+ for (let j = -D; j <= D; j++) for (let y = -D; y <= D; y++) {
320
+ if (Math.max(Math.abs(j), Math.abs(y)) !== D) continue;
321
+ const p = i.get(u(R + j, $ + y));
322
+ if (p)
323
+ for (const v of p) {
324
+ const _ = t[v].x - a.x, z = t[v].y - a.y, xt = _ * _ + z * z;
325
+ xt < V && (V = xt, G = v);
326
+ }
327
+ }
328
+ if (G >= 0 && D > Math.ceil(Math.sqrt(V) / e) + 1) break;
329
+ }
330
+ return G;
331
+ }
332
+ let E = 0;
333
+ for (let n = 1; n < t.length; n++) t[n].x + t[n].y < t[E].x + t[E].y && (E = n);
334
+ const M = [t[E]];
335
+ C(E);
336
+ for (let n = 1; n < t.length; n++) {
337
+ let a = b(E);
338
+ if (a < 0 && (a = A(E)), a < 0) break;
339
+ M.push(t[a]), C(a), E = a;
340
+ }
341
+ return M;
342
+ }
343
+ function ye() {
344
+ if ($t(), gt || !P) {
345
+ It(), I.on = !1;
282
346
  return;
283
347
  }
284
- R.rows = ae(ht), R.idx = 0, R.acc = 0, R.rate = f, R.on = R.rows.length > 0;
285
- }
286
- function ce(t) {
287
- const n = P.getContext("2d");
288
- if (n)
289
- for (t > R.rows.length && (t = R.rows.length); R.idx < t; ) {
290
- const l = R.rows[R.idx++];
291
- for (const y of l) Ot(n, y);
348
+ L === "hand" ? (I.rows = me(lt).map((t) => [t]), I.rate = Math.max(h, lt.length / 3.2)) : (I.rows = he(lt), I.rate = h), I.idx = 0, I.acc = 0, I.on = I.rows.length > 0;
349
+ }
350
+ function xe(t) {
351
+ const e = O.getContext("2d");
352
+ if (e)
353
+ for (t > I.rows.length && (t = I.rows.length); I.idx < t; ) {
354
+ const i = I.rows[I.idx++];
355
+ for (const u of i) Kt(e, u);
292
356
  }
293
357
  }
294
- function se(t, n) {
295
- const l = O.getContext("2d");
296
- if (!l || (l.clearRect(0, 0, s, h), !A || !U)) return;
297
- const y = s * 0.32, v = l.createRadialGradient(t, n, 0, t, n, y);
298
- v.addColorStop(0, "rgba(255,250,238,0.18)"), v.addColorStop(0.5, "rgba(255,250,238,0.055)"), v.addColorStop(1, "rgba(255,250,238,0)"), l.fillStyle = v, l.fillRect(0, 0, s, h), l.globalCompositeOperation = "destination-in", l.drawImage(U, 0, 0), l.globalCompositeOperation = "source-over";
358
+ function ge(t, e) {
359
+ const i = X.getContext("2d");
360
+ if (!i || (i.clearRect(0, 0, d, g), !S || !Q)) return;
361
+ const u = d * 0.32, T = i.createRadialGradient(t, e, 0, t, e, u);
362
+ T.addColorStop(0, "rgba(255,250,238,0.18)"), T.addColorStop(0.5, "rgba(255,250,238,0.055)"), T.addColorStop(1, "rgba(255,250,238,0)"), i.fillStyle = T, i.fillRect(0, 0, d, g), i.globalCompositeOperation = "destination-in", i.drawImage(Q, 0, 0), i.globalCompositeOperation = "source-over";
299
363
  }
300
- function fe() {
301
- S || !C || (S = document.createElement("span"), S.className = Bt.caret, S.setAttribute("aria-hidden", "true"), Object.assign(S.style, { position: "absolute", pointerEvents: "none", borderRadius: "1px", opacity: "0", background: `rgb(${K(k[0])},${K(k[1])},${K(k[2])})` }), g.appendChild(S), G.push(S));
364
+ function pe() {
365
+ F || !f || (F = document.createElement("span"), F.className = zt.caret, F.setAttribute("aria-hidden", "true"), Object.assign(F.style, { position: "absolute", pointerEvents: "none", borderRadius: "1px", opacity: "0", background: `rgb(${J(N[0])},${J(N[1])},${J(N[2])})` }), k.appendChild(F), Y.push(F));
302
366
  }
303
- function Et() {
304
- if (!C || !S) return;
305
- const t = P.getContext("2d");
367
+ function kt() {
368
+ if (!f || !F) return;
369
+ const t = O.getContext("2d");
306
370
  if (!t) return;
307
- t.font = `${i} ${Z}px ${r}`;
308
- const n = t.measureText(o).width, l = (P.clientWidth || s) / s, y = Z * 0.7;
309
- S.style.left = (Mt + n + Z * 0.05) * l + "px", S.style.top = 0.5 * (P.clientHeight || h) - y * l / 2 + "px", S.style.height = y * l + "px", S.style.width = Math.max(2, Math.round(Z * 0.02 * l)) + "px", S.style.background = `rgb(${K(k[0])},${K(k[1])},${K(k[2])})`;
371
+ t.font = `${c} ${ot}px ${l}`;
372
+ const e = t.measureText(r).width, i = (O.clientWidth || d) / d, u = ot * 0.7;
373
+ F.style.left = (At + e + ot * 0.05) * i + "px", F.style.top = 0.5 * (O.clientHeight || g) - u * i / 2 + "px", F.style.height = u * i + "px", F.style.width = Math.max(2, Math.round(ot * 0.02 * i)) + "px", F.style.background = `rgb(${J(N[0])},${J(N[1])},${J(N[2])})`;
310
374
  }
311
- function jt(t) {
312
- if (z) return;
375
+ function ct() {
376
+ K || mt || (mt = !0, yt = 0, Rt = requestAnimationFrame(Vt));
377
+ }
378
+ function Vt(t) {
379
+ if (K) {
380
+ mt = !1;
381
+ return;
382
+ }
313
383
  yt || (yt = t);
314
- const n = Math.min(0.25, (t - yt) / 1e3);
315
- yt = t, R.on && (R.acc += R.rate * n, ce(R.acc), R.idx >= R.rows.length && (R.on = !1)), H && U && (se(V.set ? V.x : s * 0.5, V.set ? V.y : h * 0.34), H = !1), S && (S.style.opacity = C && Q && Math.floor(t / 530) % 2 === 0 ? "1" : "0"), vt = requestAnimationFrame(jt);
384
+ const e = Math.min(0.25, (t - yt) / 1e3);
385
+ yt = t, I.on && (I.acc += I.rate * e, xe(I.acc), I.idx >= I.rows.length && (I.on = !1)), et && Q && (ge(H.set ? H.x : d * 0.5, H.set ? H.y : g * 0.45), et = !1);
386
+ const i = f && W;
387
+ if (F && (F.style.opacity = i && Math.floor(t / 530) % 2 === 0 ? "1" : "0"), !I.on && !et && !i) {
388
+ mt = !1;
389
+ return;
390
+ }
391
+ Rt = requestAnimationFrame(Vt);
316
392
  }
317
- function xt(t) {
318
- const n = O.getBoundingClientRect();
319
- V.x = (t.clientX - n.left) / (n.width || 1) * s, V.y = (t.clientY - n.top) / (n.height || 1) * h, V.set = !0, H = !0;
393
+ function Ct(t) {
394
+ const e = k.getBoundingClientRect();
395
+ H.x = (t.clientX - e.left) / (e.width || 1) * d, H.y = (t.clientY - e.top) / (e.height || 1) * g, H.set = !0, et = !0, ct();
320
396
  }
321
- function zt(t) {
322
- if (!(!C || t.metaKey || t.ctrlKey || t.altKey)) {
323
- if (t.key === "Backspace") {
324
- t.preventDefault(), Ft(o.slice(0, -1), !0);
325
- return;
326
- }
327
- if (t.key === "Enter") {
328
- t.preventDefault(), Rt.replay();
329
- return;
330
- }
331
- [...t.key].length === 1 && (t.preventDefault(), Ft(o + t.key, !0));
332
- }
397
+ function vt() {
398
+ H.set = !1, et = !0, ct();
399
+ }
400
+ function we() {
401
+ w && Ht(w.value, !0);
333
402
  }
334
- function qt() {
335
- Q = !0;
403
+ function Me(t) {
404
+ t.key === "Enter" && (t.preventDefault(), Bt.replay());
336
405
  }
337
- function _t() {
338
- Q = !1, S && (S.style.opacity = "0");
406
+ function Ce() {
407
+ W = !0, ct();
339
408
  }
340
- function Kt() {
341
- C && g.focus();
409
+ function ve() {
410
+ W = !1, F && (F.style.opacity = "0");
342
411
  }
343
- function St(t) {
344
- C = t, t ? (g.tabIndex = 0, g.setAttribute("role", "textbox"), g.getAttribute("aria-label") || g.setAttribute("aria-label", "Editable embroidered text — type to change it"), fe(), g.addEventListener("keydown", zt), g.addEventListener("focus", qt), g.addEventListener("blur", _t), g.addEventListener("pointerdown", Kt), Et()) : (g.removeAttribute("tabindex"), g.removeAttribute("role"), g.removeEventListener("keydown", zt), g.removeEventListener("focus", qt), g.removeEventListener("blur", _t), g.removeEventListener("pointerdown", Kt), S && (S.style.opacity = "0"));
412
+ function Yt(t) {
413
+ t === f && w || (f = t, t ? (w || (w = document.createElement("input"), w.type = "text", w.value = r, w.maxLength = 64, w.setAttribute("aria-label", "Embroidered text — type to change it"), w.setAttribute("autocomplete", "off"), w.setAttribute("autocapitalize", "off"), w.spellcheck = !1, Object.assign(w.style, { position: "absolute", inset: "0", width: "100%", height: "100%", margin: "0", padding: "0", border: "0", outline: "none", background: "transparent", color: "transparent", caretColor: "transparent", font: "inherit", textAlign: "center", cursor: "text" }), w.addEventListener("input", we), w.addEventListener("keydown", Me), w.addEventListener("focus", Ce), w.addEventListener("blur", ve), k.appendChild(w), Y.push(w)), pe(), kt()) : (w && (w.remove(), w = null), W = !1, F && (F.style.opacity = "0")));
345
414
  }
346
- function de(t) {
347
- if (t !== A)
348
- if (A = t, t)
349
- window.addEventListener("pointermove", xt, { passive: !0 }), H = !0;
415
+ function be(t) {
416
+ if (t !== S)
417
+ if (S = t, t)
418
+ k.addEventListener("pointermove", Ct, { passive: !0 }), k.addEventListener("pointerleave", vt, { passive: !0 }), H.set = !1, et = !0, ct();
350
419
  else {
351
- window.removeEventListener("pointermove", xt);
352
- const n = O.getContext("2d");
353
- n && n.clearRect(0, 0, s, h);
420
+ k.removeEventListener("pointermove", Ct), k.removeEventListener("pointerleave", vt);
421
+ const e = X.getContext("2d");
422
+ e && e.clearRect(0, 0, d, g);
354
423
  }
355
424
  }
356
- function it(t) {
357
- ne(), oe(), Nt(), re(), ie(), t ? le() : (At(), Tt(), R.on = !1), Et(), H = !0;
425
+ function st(t) {
426
+ ce(), se(), fe(), de(), ue(), t ? ye() : ($t(), It(), I.on = !1), w && w.value !== r && (w.value = r), kt(), et = !0, ct();
358
427
  }
359
- function Ft(t, n) {
360
- t = t ?? "", !(t === o && Y) && (o = t, Y && (it(!1), n && u && u(o)));
428
+ function Ht(t, e) {
429
+ t = t ?? "", !(t === r && Z) && (r = t, Z && (st(!1), e && B && B(r)));
361
430
  }
362
- let at;
363
- function Gt(t, n, l) {
364
- const y = `${n} 200px ${t}`;
365
- let v = !1;
431
+ const bt = /* @__PURE__ */ new Set();
432
+ function Jt(t, e, i) {
433
+ const u = `${e} 200px ${t}`;
434
+ let T = !1;
366
435
  try {
367
- v = !!document.fonts && document.fonts.check(y);
436
+ T = !!document.fonts && document.fonts.check(u);
368
437
  } catch {
369
- v = !0;
438
+ T = !0;
370
439
  }
371
- if (!document.fonts || v) {
372
- l();
440
+ if (!document.fonts || T) {
441
+ i();
373
442
  return;
374
443
  }
375
- let b = !1;
376
- const T = () => {
377
- b || (b = !0, at && clearTimeout(at), l());
444
+ let C = !1, b;
445
+ const A = () => {
446
+ C || (C = !0, clearTimeout(b), bt.delete(b), K || i());
378
447
  };
379
- document.fonts.load(y, o).then(() => document.fonts.ready).then(T, T), at = setTimeout(T, 1500);
380
- }
381
- function ue() {
382
- z || (it(!0), Y = !0, Dt || (Dt = !0, vt = requestAnimationFrame(jt)));
383
- }
384
- C && St(!0), A && window.addEventListener("pointermove", xt, { passive: !0 }), Gt($, i, ue);
385
- let Lt;
386
- function Ut() {
387
- clearTimeout(Lt), Lt = setTimeout(() => Rt.resize(), 200);
388
- }
389
- window.addEventListener("resize", Ut);
390
- const Rt = {
448
+ document.fonts.load(u, r).then(() => document.fonts.ready).then(A, A), b = setTimeout(A, 1500), bt.add(b);
449
+ }
450
+ function Te() {
451
+ K || (st(!0), Z = !0);
452
+ }
453
+ f && (f = !1, Yt(!0)), S && (k.addEventListener("pointermove", Ct, { passive: !0 }), k.addEventListener("pointerleave", vt, { passive: !0 })), Jt(q, c, Te);
454
+ let Pt, Qt = -1;
455
+ function Zt() {
456
+ clearTimeout(Pt), Pt = setTimeout(() => {
457
+ if (K || !Z) return;
458
+ const t = Math.round(k.getBoundingClientRect().width);
459
+ t !== Qt && (Qt = t, st(!1));
460
+ }, 200);
461
+ }
462
+ window.addEventListener("resize", Zt);
463
+ const Bt = {
391
464
  get text() {
392
- return o;
465
+ return r;
393
466
  },
394
467
  setText(t) {
395
- if (!z) {
396
- if (!Y) {
397
- o = t ?? "";
468
+ if (!K) {
469
+ if (!Z) {
470
+ r = t ?? "", w && (w.value = r);
398
471
  return;
399
472
  }
400
- Ft(t, !1);
473
+ Ht(t, !1);
401
474
  }
402
475
  },
403
476
  replay() {
404
- z || !Y || it(!0);
477
+ K || !Z || st(!0);
405
478
  },
406
479
  resize() {
407
- z || !Y || it(!1);
480
+ K || !Z || st(!1);
408
481
  },
409
482
  update(t) {
410
- if (z) return;
411
- let n = !1, l = !1;
412
- t.font !== void 0 && t.font !== r && (r = t.font, $ = ee(r), n = !0), t.weight !== void 0 && t.weight !== i && (i = t.weight, n = !0), t.fill !== void 0 && t.fill !== a && (a = t.fill, n = !0), t.pitch !== void 0 && t.pitch !== c && (c = t.pitch, n = !0), t.threadColor !== void 0 && (tt(t.threadColor), l = !0), t.sewRate !== void 0 && (f = t.sewRate), t.animate !== void 0 && (E = t.animate), t.onTextChange !== void 0 && (u = t.onTextChange), t.sheen !== void 0 && de(t.sheen), t.editable !== void 0 && t.editable !== C && St(t.editable), Y && (n ? Gt($, i, () => {
413
- z || it(!1);
414
- }) : l && (Nt(), At(), Tt(), Et()), H = !0);
483
+ if (K) return;
484
+ t.text !== void 0 && Bt.setText(t.text);
485
+ let e = !1, i = !1;
486
+ if (t.font !== void 0 && t.font !== l && (l = t.font, q = le(l), e = !0), t.weight !== void 0) {
487
+ const u = Et(t.weight, 1, 1e3);
488
+ u !== c && (c = u, e = !0);
489
+ }
490
+ if (t.fill !== void 0) {
491
+ const u = Et(t.fill, 0.05, 1);
492
+ u !== s && (s = u, e = !0);
493
+ }
494
+ t.pitch !== void 0 && t.pitch !== m && (m = t.pitch, e = !0), t.threadColor !== void 0 && (dt(t.threadColor), i = !0), t.sewRate !== void 0 && (h = Math.max(1, t.sewRate)), t.sewStyle !== void 0 && (L = t.sewStyle === "hand" ? "hand" : "machine"), t.animate !== void 0 && (P = t.animate), t.onTextChange !== void 0 && (B = t.onTextChange), t.sheen !== void 0 && be(t.sheen), t.editable !== void 0 && Yt(t.editable), Z && (e ? Jt(q, c, () => {
495
+ K || st(!1);
496
+ }) : i && (Xt(), $t(), It(), kt(), ct()));
415
497
  },
416
498
  focus() {
417
- !z && C && g.focus();
499
+ !K && f && (w == null || w.focus());
418
500
  },
419
501
  destroy() {
420
- if (!z) {
421
- z = !0, cancelAnimationFrame(vt), clearTimeout(Lt), at && clearTimeout(at), window.removeEventListener("resize", Ut), window.removeEventListener("pointermove", xt), C && St(!1);
422
- for (const t of G) t.remove();
423
- et = [], ht = [], R.rows = [], bt = new Uint8Array(0), Ct = new Float32Array(0), ft = new Float32Array(0), dt = new Float32Array(0), U = rt = null, ut = null, S = null;
502
+ if (!K) {
503
+ K = !0, mt = !1, cancelAnimationFrame(Rt), clearTimeout(Pt);
504
+ for (const t of bt) clearTimeout(t);
505
+ bt.clear(), window.removeEventListener("resize", Zt), k.removeEventListener("pointermove", Ct), k.removeEventListener("pointerleave", vt);
506
+ for (const t of Y) t.remove();
507
+ it = [], lt = [], I.rows = [], Ft = new Uint8Array(0), Lt = new Float32Array(0), pt = new Float32Array(0), wt = new Float32Array(0), Q = at = null, Mt = null, F = null, w = null;
424
508
  }
425
509
  }
426
510
  };
427
- return Rt;
511
+ return Bt;
428
512
  }
429
- function Me(m) {
430
- const e = wt(null), o = wt(null), r = wt(m);
431
- r.current = m;
432
- const i = wt(m.onTextChange);
433
- i.current = m.onTextChange;
434
- const { text: a, font: f, weight: A, threadColor: E, pitch: C, fill: c, animate: u, sewRate: $, sheen: D, editable: k, reducedMotion: W } = m;
435
- return me(() => {
436
- const N = e.current;
437
- if (!N) return;
438
- const tt = pe(N, {
439
- ...r.current,
513
+ function $e(x) {
514
+ const o = Tt(null), r = Tt(null), l = Tt(x);
515
+ l.current = x;
516
+ const c = Tt(x.onTextChange);
517
+ c.current = x.onTextChange;
518
+ const { text: s, font: h, weight: L, threadColor: S, pitch: P, fill: f, animate: m, sewStyle: B, sewRate: q, sheen: gt, editable: N, reducedMotion: ft } = x;
519
+ return Se(() => {
520
+ const U = o.current;
521
+ if (!U) return;
522
+ const dt = Ie(U, {
523
+ ...l.current,
440
524
  // Stable callback wrapper so typing edits always reach the latest handler.
441
- onTextChange: (O) => {
442
- var S;
443
- return (S = i.current) == null ? void 0 : S.call(i, O);
525
+ onTextChange: (w) => {
526
+ var F;
527
+ return (F = c.current) == null ? void 0 : F.call(c, w);
444
528
  }
445
529
  });
446
- o.current = tt;
447
- let G = 0, P = 0, g;
448
- return typeof ResizeObserver < "u" && (g = new ResizeObserver((O) => {
449
- const S = Math.round(O[0].contentRect.width);
450
- S !== P && (P = S, cancelAnimationFrame(G), G = requestAnimationFrame(() => {
451
- var Q;
452
- return (Q = o.current) == null ? void 0 : Q.resize();
530
+ r.current = dt;
531
+ let Y = 0, O = 0, k = !0, X;
532
+ return typeof ResizeObserver < "u" && (X = new ResizeObserver((w) => {
533
+ const F = Math.round(w[0].contentRect.width);
534
+ if (k) {
535
+ k = !1, O = F;
536
+ return;
537
+ }
538
+ F !== O && (O = F, cancelAnimationFrame(Y), Y = requestAnimationFrame(() => {
539
+ var W;
540
+ return (W = r.current) == null ? void 0 : W.resize();
453
541
  }));
454
- }), g.observe(N)), () => {
455
- g == null || g.disconnect(), cancelAnimationFrame(G), tt.destroy(), o.current = null;
542
+ }), X.observe(U)), () => {
543
+ X == null || X.disconnect(), cancelAnimationFrame(Y), dt.destroy(), r.current = null;
456
544
  };
457
- }, [W]), Qt(() => {
458
- var N;
459
- (N = o.current) == null || N.update({ font: f, weight: A, threadColor: E, pitch: C, fill: c, animate: u, sewRate: $, sheen: D, editable: k });
460
- }, [f, A, E, C, c, u, $, D, k]), Qt(() => {
461
- var N;
462
- (N = o.current) == null || N.setText(a);
463
- }, [a]), e;
545
+ }, [ft]), ne(() => {
546
+ var U;
547
+ (U = r.current) == null || U.update({ font: h, weight: L, threadColor: S, pitch: P, fill: f, animate: m, sewStyle: B, sewRate: q, sheen: gt, editable: N });
548
+ }, [h, L, S, P, f, m, B, q, gt, N]), ne(() => {
549
+ var U;
550
+ (U = r.current) == null || U.setText(s);
551
+ }, [s]), o;
464
552
  }
465
- const be = ye(
466
- function({ className: e, style: o, as: r = "div", "aria-label": i, role: a, "aria-describedby": f, ...A }, E) {
467
- const C = Me(A), c = xe(
468
- (u) => {
469
- C.current = u, typeof E == "function" ? E(u) : E && (E.current = u);
553
+ const ke = Ae(
554
+ function({ className: o, style: r, as: l = "div", "aria-label": c, role: s, "aria-describedby": h, ...L }, S) {
555
+ const P = $e(L), f = Fe(
556
+ (m) => {
557
+ P.current = m, typeof S == "function" ? S(m) : S && (S.current = m);
470
558
  },
471
559
  // eslint-disable-next-line react-hooks/exhaustive-deps
472
- [C, E]
560
+ [P, S]
473
561
  );
474
- return /* @__PURE__ */ ge(
475
- r,
562
+ return /* @__PURE__ */ Le(
563
+ l,
476
564
  {
477
- ref: c,
478
- className: e,
479
- style: o,
480
- role: a ?? "img",
481
- "aria-label": i ?? A.text,
482
- "aria-describedby": f
565
+ ref: f,
566
+ className: o,
567
+ style: r,
568
+ role: L.editable ? s : s ?? "img",
569
+ "aria-label": L.editable ? c : c ?? L.text,
570
+ "aria-describedby": h
483
571
  }
484
572
  );
485
573
  }
486
574
  );
487
- be.displayName = "ThreadText";
575
+ ke.displayName = "ThreadText";
488
576
  export {
489
- Bt as THREAD_TEXT_CLASSES,
490
- be as ThreadText,
491
- pe as createThreadText,
492
- Me as useThreadText
577
+ zt as THREAD_TEXT_CLASSES,
578
+ ke as ThreadText,
579
+ Ie as createThreadText,
580
+ $e as useThreadText
493
581
  };