@liiift-studio/threadtext 0.0.1 → 0.1.1

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