@algorisys/tinyfly 0.115.0 → 0.116.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/README.md +11 -6
- package/docs/api-reference.md +2 -1
- package/docs/editor-guide.md +1 -1
- package/docs/examples.md +1 -1
- package/docs/getting-started.md +1 -1
- package/docs/gsap-compat.md +1 -1
- package/docs/llm-guide.md +16 -0
- package/docs/maps.md +2 -2
- package/docs/math.md +280 -0
- package/docs/teaching.md +1 -1
- package/lib/addons/characters.js +2607 -2548
- package/lib/addons/math.js +2864 -0
- package/lib/browser/tinyfly-math.iife.js +53 -0
- package/lib/browser/tinyfly.iife.js +3 -3
- package/lib/browser/tinyfly.js +705 -638
- package/lib/browser/tinyfly.umd.js +3 -3
- package/lib/embed/tinyfly-embed.iife.js +1 -1
- package/lib/embed/tinyfly-embed.js +8 -0
- package/lib/player/tinyfly-player.es.js +8 -0
- package/lib/player/tinyfly-player.iife.js +1 -1
- package/lib/player/tinyfly-player.umd.js +1 -1
- package/lib/types/characters/capabilities.d.ts +8 -0
- package/lib/types/math/browser-entry.d.ts +3 -0
- package/lib/types/math/embed/auto-mount-math.d.ts +7 -0
- package/lib/types/math/embed/explorable.d.ts +46 -0
- package/lib/types/math/embed/mount-math.d.ts +31 -0
- package/lib/types/math/expr/differentiate.d.ts +7 -0
- package/lib/types/math/expr/evaluate.d.ts +21 -0
- package/lib/types/math/expr/parse-expression.d.ts +50 -0
- package/lib/types/math/figure/derived.d.ts +17 -0
- package/lib/types/math/figure/display-list.d.ts +87 -0
- package/lib/types/math/figure/figure-spec.d.ts +239 -0
- package/lib/types/math/figure/item-context.d.ts +33 -0
- package/lib/types/math/figure/items/calculus.d.ts +7 -0
- package/lib/types/math/figure/items/curves.d.ts +7 -0
- package/lib/types/math/figure/items/grid.d.ts +5 -0
- package/lib/types/math/figure/items/regions.d.ts +5 -0
- package/lib/types/math/figure/items/text.d.ts +5 -0
- package/lib/types/math/figure/items/vectors.d.ts +9 -0
- package/lib/types/math/figure/layout-figure.d.ts +13 -0
- package/lib/types/math/figure/plane.d.ts +20 -0
- package/lib/types/math/figure/validate-spec.d.ts +2 -0
- package/lib/types/math/index.d.ts +43 -0
- package/lib/types/math/linear/mat2.d.ts +41 -0
- package/lib/types/math/linear/vec2.d.ts +18 -0
- package/lib/types/math/presets/calculus.d.ts +59 -0
- package/lib/types/math/presets/linear-algebra.d.ts +42 -0
- package/lib/types/math/render/arrow.d.ts +10 -0
- package/lib/types/math/render/display-list-to-svg.d.ts +10 -0
- package/lib/types/math/render/draw-display-list.d.ts +6 -0
- package/lib/types/math/steps/math-steps.d.ts +34 -0
- package/lib/types/math/targets/math-target.d.ts +43 -0
- package/lib/types/math/tex/equation-morph.d.ts +21 -0
- package/lib/types/math/tex/glyph-metrics.d.ts +24 -0
- package/lib/types/math/tex/tex-layout.d.ts +59 -0
- package/lib/types/math/tex/tex-parser.d.ts +88 -0
- package/lib/types/player/player.d.ts +6 -0
- package/llms-full.txt +358 -10
- package/llms.txt +32 -31
- package/package.json +6 -2
- package/skills/tinyfly/SKILL.md +10 -0
|
@@ -0,0 +1,2864 @@
|
|
|
1
|
+
import { closestName as je, unknownName as it, deserializeTimeline as ie, playheadCrossings as De } from "@algorisys/tinyfly";
|
|
2
|
+
import { D as re } from "./dom-adapter-Cphr2HZn.js";
|
|
3
|
+
const _e = (t, e) => [t[0] + e[0], t[1] + e[1]], Xt = (t, e) => [t[0] - e[0], t[1] - e[1]], qt = (t, e) => [t[0] * e, t[1] * e], Re = (t, e) => t[0] * e[0] + t[1] * e[1], Ve = (t, e) => t[0] * e[1] - t[1] * e[0], ve = (t) => Math.hypot(t[0], t[1]);
|
|
4
|
+
function we(t) {
|
|
5
|
+
const e = ve(t);
|
|
6
|
+
return e === 0 ? [0, 0] : [t[0] / e, t[1] / e];
|
|
7
|
+
}
|
|
8
|
+
const We = (t, e, n) => [t[0] + (e[0] - t[0]) * n, t[1] + (e[1] - t[1]) * n];
|
|
9
|
+
function tt(t) {
|
|
10
|
+
return Array.isArray(t) && t.length === 2 && t.every(Number.isFinite);
|
|
11
|
+
}
|
|
12
|
+
const hi = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
13
|
+
__proto__: null,
|
|
14
|
+
add: _e,
|
|
15
|
+
cross: Ve,
|
|
16
|
+
dot: Re,
|
|
17
|
+
isVec2: tt,
|
|
18
|
+
length: ve,
|
|
19
|
+
lerp: We,
|
|
20
|
+
normalize: we,
|
|
21
|
+
scale: qt,
|
|
22
|
+
sub: Xt
|
|
23
|
+
}, Symbol.toStringTag, { value: "Module" })), yt = [1, 0, 0, 1], B = ([t, e, n, i], [r, s]) => [t * r + e * s, n * r + i * s];
|
|
24
|
+
function Be(t, e) {
|
|
25
|
+
const [n, i, r, s] = t, [o, l, c, f] = e;
|
|
26
|
+
return [n * o + i * c, n * l + i * f, r * o + s * c, r * l + s * f];
|
|
27
|
+
}
|
|
28
|
+
const gt = ([t, e, n, i]) => t * i - e * n;
|
|
29
|
+
function Ue(t) {
|
|
30
|
+
const e = gt(t);
|
|
31
|
+
if (Math.abs(e) < 1e-12) return;
|
|
32
|
+
const [n, i, r, s] = t;
|
|
33
|
+
return [s / e, -i / e, -r / e, n / e];
|
|
34
|
+
}
|
|
35
|
+
const He = (t, e, n) => t.map((i, r) => i + (e[r] - i) * n), Xe = (t) => [Math.cos(t), -Math.sin(t), Math.sin(t), Math.cos(t)];
|
|
36
|
+
function ke(t) {
|
|
37
|
+
const [e, n, i, r] = t, s = e + r, o = gt(t), l = s * s - 4 * o, c = 1e-9;
|
|
38
|
+
if (l < -c) return [];
|
|
39
|
+
const f = Math.sqrt(Math.max(0, l)), u = f < c ? [s / 2] : [(s + f) / 2, (s - f) / 2], h = [];
|
|
40
|
+
for (const d of u) {
|
|
41
|
+
const a = [[e - d, n], [i, r - d]].reduce((w, $) => Math.hypot(...$) > Math.hypot(...w) ? $ : w);
|
|
42
|
+
if (Math.hypot(...a) < c) {
|
|
43
|
+
h.push({ value: d, vector: [1, 0] }, { value: d, vector: [0, 1] });
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const p = [-a[1], a[0]], m = Math.hypot(...p), b = p[0] < -c || Math.abs(p[0]) <= c && p[1] < 0 ? -1 : 1;
|
|
47
|
+
h.push({ value: d, vector: [b * p[0] / m + 0, b * p[1] / m + 0] });
|
|
48
|
+
}
|
|
49
|
+
return h;
|
|
50
|
+
}
|
|
51
|
+
function lt(t) {
|
|
52
|
+
return Array.isArray(t) && t.length === 4 && t.every(Number.isFinite);
|
|
53
|
+
}
|
|
54
|
+
const fi = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
55
|
+
__proto__: null,
|
|
56
|
+
IDENTITY: yt,
|
|
57
|
+
apply: B,
|
|
58
|
+
determinant: gt,
|
|
59
|
+
eigen: ke,
|
|
60
|
+
inverse: Ue,
|
|
61
|
+
isMat2: lt,
|
|
62
|
+
lerp: He,
|
|
63
|
+
multiply: Be,
|
|
64
|
+
rotation: Xe
|
|
65
|
+
}, Symbol.toStringTag, { value: "Module" })), zt = {
|
|
66
|
+
sin: { arity: 1, apply: Math.sin },
|
|
67
|
+
cos: { arity: 1, apply: Math.cos },
|
|
68
|
+
tan: { arity: 1, apply: Math.tan },
|
|
69
|
+
asin: { arity: 1, apply: Math.asin },
|
|
70
|
+
acos: { arity: 1, apply: Math.acos },
|
|
71
|
+
atan: { arity: 1, apply: Math.atan },
|
|
72
|
+
atan2: { arity: 2, apply: Math.atan2 },
|
|
73
|
+
sinh: { arity: 1, apply: Math.sinh },
|
|
74
|
+
cosh: { arity: 1, apply: Math.cosh },
|
|
75
|
+
tanh: { arity: 1, apply: Math.tanh },
|
|
76
|
+
exp: { arity: 1, apply: Math.exp },
|
|
77
|
+
ln: { arity: 1, apply: Math.log },
|
|
78
|
+
log: { arity: 1, apply: Math.log10 },
|
|
79
|
+
log2: { arity: 1, apply: Math.log2 },
|
|
80
|
+
sqrt: { arity: 1, apply: Math.sqrt },
|
|
81
|
+
abs: { arity: 1, apply: Math.abs },
|
|
82
|
+
floor: { arity: 1, apply: Math.floor },
|
|
83
|
+
ceil: { arity: 1, apply: Math.ceil },
|
|
84
|
+
round: { arity: 1, apply: Math.round },
|
|
85
|
+
sign: { arity: 1, apply: Math.sign },
|
|
86
|
+
min: { arity: 2, apply: Math.min },
|
|
87
|
+
max: { arity: 2, apply: Math.max },
|
|
88
|
+
// The logistic function, common in ML lessons
|
|
89
|
+
sigmoid: { arity: 1, apply: (t) => 1 / (1 + Math.exp(-t)) },
|
|
90
|
+
relu: { arity: 1, apply: (t) => Math.max(0, t) }
|
|
91
|
+
}, Et = { pi: Math.PI, e: Math.E, tau: Math.PI * 2 };
|
|
92
|
+
class ot extends Error {
|
|
93
|
+
source;
|
|
94
|
+
/** 0-based index into the source */
|
|
95
|
+
position;
|
|
96
|
+
constructor(e, n, i) {
|
|
97
|
+
super(`${e} at ${i + 1} in "${n}"`), this.name = "ExpressionError", this.source = n, this.position = i;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function Ye(t) {
|
|
101
|
+
const e = [];
|
|
102
|
+
let n = 0;
|
|
103
|
+
for (; n < t.length; ) {
|
|
104
|
+
const i = t[n];
|
|
105
|
+
if (/\s/.test(i)) {
|
|
106
|
+
n++;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const r = /^(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?/.exec(t.slice(n));
|
|
110
|
+
if (r) {
|
|
111
|
+
e.push({ kind: "number", value: Number(r[0]), at: n }), n += r[0].length;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
const s = /^[A-Za-z_][A-Za-z_0-9]*/.exec(t.slice(n));
|
|
115
|
+
if (s) {
|
|
116
|
+
e.push({ kind: "name", value: s[0], at: n }), n += s[0].length;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if ("+-*/^(),".includes(i)) {
|
|
120
|
+
e.push({ kind: "op", value: i, at: n }), n++;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
throw new ot(`Unexpected "${i}"`, t, n);
|
|
124
|
+
}
|
|
125
|
+
return e.push({ kind: "end", at: t.length }), e;
|
|
126
|
+
}
|
|
127
|
+
function At(t) {
|
|
128
|
+
if (typeof t != "string") throw new TypeError("parseExpression: expected a string");
|
|
129
|
+
const e = Ye(t);
|
|
130
|
+
let n = 0;
|
|
131
|
+
const i = () => e[n], r = (d) => {
|
|
132
|
+
const y = i();
|
|
133
|
+
return y.kind === "op" && y.value === d;
|
|
134
|
+
}, s = (d) => {
|
|
135
|
+
if (!r(d)) throw new ot(`Expected "${d}"`, t, i().at);
|
|
136
|
+
n++;
|
|
137
|
+
}, o = () => {
|
|
138
|
+
let d = l();
|
|
139
|
+
for (; r("+") || r("-"); )
|
|
140
|
+
d = { type: "binary", operator: e[n++].value, left: d, right: l() };
|
|
141
|
+
return d;
|
|
142
|
+
}, l = () => {
|
|
143
|
+
let d = c();
|
|
144
|
+
for (; ; )
|
|
145
|
+
if (r("*") || r("/"))
|
|
146
|
+
d = { type: "binary", operator: e[n++].value, left: d, right: c() };
|
|
147
|
+
else if (i().kind === "name" || i().kind === "number" || r("("))
|
|
148
|
+
d = { type: "binary", operator: "*", left: d, right: f() };
|
|
149
|
+
else
|
|
150
|
+
return d;
|
|
151
|
+
}, c = () => r("-") || r("+") ? { type: "unary", operator: e[n++].value, operand: c() } : f(), f = () => {
|
|
152
|
+
const d = u();
|
|
153
|
+
return r("^") ? (n++, { type: "binary", operator: "^", left: d, right: c() }) : d;
|
|
154
|
+
}, u = () => {
|
|
155
|
+
const d = i();
|
|
156
|
+
if (d.kind === "number")
|
|
157
|
+
return n++, { type: "number", value: d.value };
|
|
158
|
+
if (d.kind === "name") {
|
|
159
|
+
if (n++, !r("(")) return { type: "name", name: d.value };
|
|
160
|
+
const y = Object.prototype.hasOwnProperty.call(zt, d.value) ? zt[d.value] : void 0;
|
|
161
|
+
if (!y) throw new ot(`Unknown function "${d.value}"`, t, d.at);
|
|
162
|
+
n++;
|
|
163
|
+
const a = [];
|
|
164
|
+
if (!r(")"))
|
|
165
|
+
for (a.push(o()); r(","); )
|
|
166
|
+
n++, a.push(o());
|
|
167
|
+
if (s(")"), a.length !== y.arity)
|
|
168
|
+
throw new ot(`"${d.value}" takes ${y.arity} argument${y.arity === 1 ? "" : "s"}, got ${a.length}`, t, d.at);
|
|
169
|
+
return { type: "call", name: d.value, args: a };
|
|
170
|
+
}
|
|
171
|
+
if (r("(")) {
|
|
172
|
+
n++;
|
|
173
|
+
const y = o();
|
|
174
|
+
return s(")"), y;
|
|
175
|
+
}
|
|
176
|
+
throw new ot(d.kind === "end" ? "Unexpected end" : `Unexpected "${d.value}"`, t, d.at);
|
|
177
|
+
}, h = o();
|
|
178
|
+
if (i().kind !== "end") throw new ot(`Unexpected "${i().value}"`, t, i().at);
|
|
179
|
+
return h;
|
|
180
|
+
}
|
|
181
|
+
function Ze(t) {
|
|
182
|
+
const e = /* @__PURE__ */ new Set(), n = (i) => {
|
|
183
|
+
i.type === "name" && !(i.name in Et) ? e.add(i.name) : i.type === "unary" ? n(i.operand) : i.type === "binary" ? (n(i.left), n(i.right)) : i.type === "call" && i.args.forEach(n);
|
|
184
|
+
};
|
|
185
|
+
return n(t), [...e].sort();
|
|
186
|
+
}
|
|
187
|
+
const E = (t) => ({ type: "number", value: t }), ft = E(0), O = E(1), I = (t) => t.type === "number", _ = (t, e) => t.type === "number" && t.value === e;
|
|
188
|
+
function J(t, e) {
|
|
189
|
+
return I(t) && I(e) ? E(t.value + e.value) : _(t, 0) ? e : _(e, 0) ? t : { type: "binary", operator: "+", left: t, right: e };
|
|
190
|
+
}
|
|
191
|
+
function H(t, e) {
|
|
192
|
+
return I(t) && I(e) ? E(t.value - e.value) : _(e, 0) ? t : _(t, 0) ? Mt(e) : { type: "binary", operator: "-", left: t, right: e };
|
|
193
|
+
}
|
|
194
|
+
function A(t, e) {
|
|
195
|
+
return I(t) && I(e) ? E(t.value * e.value) : _(t, 0) || _(e, 0) ? ft : _(t, 1) ? e : _(e, 1) ? t : I(e) ? A(e, t) : I(t) && e.type === "binary" && e.operator === "*" && I(e.left) ? A(E(t.value * e.left.value), e.right) : { type: "binary", operator: "*", left: t, right: e };
|
|
196
|
+
}
|
|
197
|
+
function q(t, e) {
|
|
198
|
+
return _(t, 0) ? ft : _(e, 1) ? t : I(t) && I(e) && e.value !== 0 ? E(t.value / e.value) : { type: "binary", operator: "/", left: t, right: e };
|
|
199
|
+
}
|
|
200
|
+
function Z(t, e) {
|
|
201
|
+
return _(e, 0) ? O : _(e, 1) ? t : I(t) && I(e) ? E(t.value ** e.value) : { type: "binary", operator: "^", left: t, right: e };
|
|
202
|
+
}
|
|
203
|
+
function Mt(t) {
|
|
204
|
+
return I(t) ? E(-t.value) : t.type === "unary" && t.operator === "-" ? t.operand : { type: "unary", operator: "-", operand: t };
|
|
205
|
+
}
|
|
206
|
+
const z = (t, ...e) => ({ type: "call", name: t, args: e });
|
|
207
|
+
function rt(t, e) {
|
|
208
|
+
switch (t.type) {
|
|
209
|
+
case "number":
|
|
210
|
+
return !0;
|
|
211
|
+
case "name":
|
|
212
|
+
return t.name !== e;
|
|
213
|
+
case "unary":
|
|
214
|
+
return rt(t.operand, e);
|
|
215
|
+
case "binary":
|
|
216
|
+
return rt(t.left, e) && rt(t.right, e);
|
|
217
|
+
case "call":
|
|
218
|
+
return t.args.every((n) => rt(n, e));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
function G(t, e = "x") {
|
|
222
|
+
if (rt(t, e)) return ft;
|
|
223
|
+
switch (t.type) {
|
|
224
|
+
case "number":
|
|
225
|
+
return ft;
|
|
226
|
+
case "name":
|
|
227
|
+
return t.name === e ? O : ft;
|
|
228
|
+
case "unary":
|
|
229
|
+
return t.operator === "-" ? Mt(G(t.operand, e)) : G(t.operand, e);
|
|
230
|
+
case "binary": {
|
|
231
|
+
const { left: n, right: i } = t, r = () => G(n, e), s = () => G(i, e);
|
|
232
|
+
switch (t.operator) {
|
|
233
|
+
case "+":
|
|
234
|
+
return J(r(), s());
|
|
235
|
+
case "-":
|
|
236
|
+
return H(r(), s());
|
|
237
|
+
case "*":
|
|
238
|
+
return J(A(r(), i), A(n, s()));
|
|
239
|
+
case "/":
|
|
240
|
+
return q(H(A(r(), i), A(n, s())), Z(i, E(2)));
|
|
241
|
+
case "^":
|
|
242
|
+
if (rt(i, e)) {
|
|
243
|
+
const o = I(i) ? E(i.value - 1) : H(i, O);
|
|
244
|
+
return A(A(i, Z(n, o)), r());
|
|
245
|
+
}
|
|
246
|
+
return rt(n, e) ? A(A(t, z("ln", n)), s()) : A(t, J(A(s(), z("ln", n)), q(A(i, r()), n)));
|
|
247
|
+
}
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
case "call": {
|
|
251
|
+
const [n, i] = t.args, r = () => G(n, e), s = (o) => A(o, r());
|
|
252
|
+
switch (t.name) {
|
|
253
|
+
case "sin":
|
|
254
|
+
return s(z("cos", n));
|
|
255
|
+
case "cos":
|
|
256
|
+
return s(Mt(z("sin", n)));
|
|
257
|
+
case "tan":
|
|
258
|
+
return s(q(O, Z(z("cos", n), E(2))));
|
|
259
|
+
case "asin":
|
|
260
|
+
return s(q(O, z("sqrt", H(O, Z(n, E(2))))));
|
|
261
|
+
case "acos":
|
|
262
|
+
return s(Mt(q(O, z("sqrt", H(O, Z(n, E(2)))))));
|
|
263
|
+
case "atan":
|
|
264
|
+
return s(q(O, J(O, Z(n, E(2)))));
|
|
265
|
+
case "sinh":
|
|
266
|
+
return s(z("cosh", n));
|
|
267
|
+
case "cosh":
|
|
268
|
+
return s(z("sinh", n));
|
|
269
|
+
case "tanh":
|
|
270
|
+
return s(H(O, Z(z("tanh", n), E(2))));
|
|
271
|
+
case "exp":
|
|
272
|
+
return s(t);
|
|
273
|
+
case "ln":
|
|
274
|
+
return s(q(O, n));
|
|
275
|
+
case "log":
|
|
276
|
+
return s(q(O, A(n, E(Math.LN10))));
|
|
277
|
+
case "log2":
|
|
278
|
+
return s(q(O, A(n, E(Math.LN2))));
|
|
279
|
+
case "sqrt":
|
|
280
|
+
return s(q(O, A(E(2), t)));
|
|
281
|
+
case "abs":
|
|
282
|
+
return s(z("sign", n));
|
|
283
|
+
case "sigmoid":
|
|
284
|
+
return s(A(t, H(O, t)));
|
|
285
|
+
case "relu":
|
|
286
|
+
return s(q(J(z("sign", n), O), E(2)));
|
|
287
|
+
case "atan2": {
|
|
288
|
+
const o = G(i, e);
|
|
289
|
+
return q(H(A(i, r()), A(n, o)), J(Z(n, E(2)), Z(i, E(2))));
|
|
290
|
+
}
|
|
291
|
+
case "min":
|
|
292
|
+
case "max": {
|
|
293
|
+
const o = t.name === "max" ? 1 : -1, l = G(i, e), c = q(J(O, A(E(o), z("sign", H(n, i)))), E(2));
|
|
294
|
+
return J(A(c, r()), A(H(O, c), l));
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
throw new Error(`differentiate: "${t.name}" has no derivative rule`);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
throw new Error("differentiate: malformed expression");
|
|
301
|
+
}
|
|
302
|
+
function Ge(t, e, n = "x") {
|
|
303
|
+
let i = t;
|
|
304
|
+
for (let r = 0; r < e; r++) i = G(i, n);
|
|
305
|
+
return i;
|
|
306
|
+
}
|
|
307
|
+
function $t(t) {
|
|
308
|
+
switch (t.type) {
|
|
309
|
+
case "number":
|
|
310
|
+
return t.value < 0 ? `(${t.value})` : String(t.value);
|
|
311
|
+
case "name":
|
|
312
|
+
return t.name;
|
|
313
|
+
case "unary":
|
|
314
|
+
return `(${t.operator}${$t(t.operand)})`;
|
|
315
|
+
case "binary":
|
|
316
|
+
return `(${$t(t.left)} ${t.operator} ${$t(t.right)})`;
|
|
317
|
+
case "call":
|
|
318
|
+
return `${t.name}(${t.args.map($t).join(", ")})`;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
function at(t, e = {}) {
|
|
322
|
+
switch (t.type) {
|
|
323
|
+
case "number":
|
|
324
|
+
return t.value;
|
|
325
|
+
case "name": {
|
|
326
|
+
if (Object.prototype.hasOwnProperty.call(e, t.name)) return e[t.name];
|
|
327
|
+
if (Object.prototype.hasOwnProperty.call(Et, t.name)) return Et[t.name];
|
|
328
|
+
throw new Error(`evaluate: unknown name "${t.name}"`);
|
|
329
|
+
}
|
|
330
|
+
case "unary": {
|
|
331
|
+
const n = at(t.operand, e);
|
|
332
|
+
return t.operator === "-" ? -n : n;
|
|
333
|
+
}
|
|
334
|
+
case "binary": {
|
|
335
|
+
const n = at(t.left, e), i = at(t.right, e);
|
|
336
|
+
switch (t.operator) {
|
|
337
|
+
case "+":
|
|
338
|
+
return n + i;
|
|
339
|
+
case "-":
|
|
340
|
+
return n - i;
|
|
341
|
+
case "*":
|
|
342
|
+
return n * i;
|
|
343
|
+
case "/":
|
|
344
|
+
return n / i;
|
|
345
|
+
case "^":
|
|
346
|
+
return n ** i;
|
|
347
|
+
}
|
|
348
|
+
break;
|
|
349
|
+
}
|
|
350
|
+
case "call":
|
|
351
|
+
return zt[t.name].apply(...t.args.map((n) => at(n, e)));
|
|
352
|
+
}
|
|
353
|
+
throw new Error("evaluate: malformed expression");
|
|
354
|
+
}
|
|
355
|
+
const St = /* @__PURE__ */ new Map();
|
|
356
|
+
function D(t, e = "x") {
|
|
357
|
+
let n = St.get(t);
|
|
358
|
+
n || (n = At(t), St.size < 256 && St.set(t, n));
|
|
359
|
+
const i = n;
|
|
360
|
+
return (r, s = {}) => at(i, { ...s, [e]: r });
|
|
361
|
+
}
|
|
362
|
+
const Nt = /* @__PURE__ */ new Map();
|
|
363
|
+
function Yt(t, e, n = "x") {
|
|
364
|
+
if (!Number.isInteger(e) || e < 0) throw new Error(`compileDerivative: order must be a whole number ≥ 0, got ${e}`);
|
|
365
|
+
const i = `${n}|${e}|${t}`;
|
|
366
|
+
let r = Nt.get(i);
|
|
367
|
+
if (!r) {
|
|
368
|
+
let o = St.get(t);
|
|
369
|
+
o || (o = At(t)), r = Ge(o, e, n), Nt.size < 512 && Nt.set(i, r);
|
|
370
|
+
}
|
|
371
|
+
const s = r;
|
|
372
|
+
return (o, l = {}) => at(s, { ...l, [n]: o });
|
|
373
|
+
}
|
|
374
|
+
function $e(t) {
|
|
375
|
+
const e = {};
|
|
376
|
+
for (const [n, i] of Object.entries(t))
|
|
377
|
+
if (typeof i == "number")
|
|
378
|
+
e[n] = i;
|
|
379
|
+
else if (lt(i)) {
|
|
380
|
+
const [r, s, o, l] = i;
|
|
381
|
+
Object.assign(e, { [`${n}.a`]: r, [`${n}.b`]: s, [`${n}.c`]: o, [`${n}.d`]: l, [`${n}.det`]: gt(i) });
|
|
382
|
+
} else tt(i) && Object.assign(e, { [`${n}.x`]: i[0], [`${n}.y`]: i[1], [`${n}.length`]: Math.hypot(i[0], i[1]) });
|
|
383
|
+
return e;
|
|
384
|
+
}
|
|
385
|
+
function F(t, e, n) {
|
|
386
|
+
return e ? Object.prototype.hasOwnProperty.call(t.colors, e) ? t.colors[e] : e : n;
|
|
387
|
+
}
|
|
388
|
+
function pt(t, e) {
|
|
389
|
+
if (typeof e != "string") return e;
|
|
390
|
+
const n = t[e];
|
|
391
|
+
if (!tt(n)) throw new Error(`math figure: value "${e}" is not an [x, y] vector`);
|
|
392
|
+
return n;
|
|
393
|
+
}
|
|
394
|
+
function st(t, e) {
|
|
395
|
+
if (e === void 0) return yt;
|
|
396
|
+
const n = t[e];
|
|
397
|
+
if (!lt(n)) throw new Error(`math figure: value "${e}" is not a [a, b, c, d] matrix`);
|
|
398
|
+
return n;
|
|
399
|
+
}
|
|
400
|
+
function N(t, e) {
|
|
401
|
+
if (typeof e == "number") return e;
|
|
402
|
+
const n = t[e];
|
|
403
|
+
if (typeof n != "number") throw new Error(`math figure: value "${e}" is not a number`);
|
|
404
|
+
return n;
|
|
405
|
+
}
|
|
406
|
+
function se(t, e, n, i = 512) {
|
|
407
|
+
if (e === n) return 0;
|
|
408
|
+
const r = (n - e) / i;
|
|
409
|
+
let s = t(e) + t(n);
|
|
410
|
+
for (let o = 1; o < i; o++) s += t(e + o * r) * (o % 2 ? 4 : 2);
|
|
411
|
+
return s * r / 3;
|
|
412
|
+
}
|
|
413
|
+
function xe(t, e, n, i, r) {
|
|
414
|
+
const s = (n - e) / i;
|
|
415
|
+
return Array.from({ length: i }, (o, l) => {
|
|
416
|
+
const c = e + l * s, f = c + s;
|
|
417
|
+
if (r === "trapezoid") return [c, f, t(c), t(f)];
|
|
418
|
+
const u = t(r === "left" ? c : r === "right" ? f : (c + f) / 2);
|
|
419
|
+
return [c, f, u, u];
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
const Me = (t) => Math.min(400, Math.max(1, Math.round(t)));
|
|
423
|
+
function Se(t, e, n) {
|
|
424
|
+
const i = {}, r = { ...n };
|
|
425
|
+
for (const s of t)
|
|
426
|
+
switch (s.kind) {
|
|
427
|
+
case "tangent": {
|
|
428
|
+
const o = D(s.expr), l = N(e, s.at), c = s.h === void 0 ? 0 : N(e, s.h), f = Math.abs(c) > 1e-9 ? (o(l + c, r) - o(l, r)) / c : Yt(s.expr, 1)(l, r);
|
|
429
|
+
Object.assign(i, { [`${s.id}.slope`]: f, [`${s.id}.x`]: l, [`${s.id}.y`]: o(l, r) });
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
case "riemann": {
|
|
433
|
+
const o = D(s.expr), l = N(e, s.from), c = N(e, s.to), f = Me(N(e, s.n)), h = xe((d) => o(d, r), l, c, f, s.rule ?? "left").reduce((d, [y, a, p, m]) => d + (p + m) / 2 * (a - y), 0);
|
|
434
|
+
Object.assign(i, { [`${s.id}.sum`]: h, [`${s.id}.exact`]: se((d) => o(d, r), l, c), [`${s.id}.n`]: f });
|
|
435
|
+
break;
|
|
436
|
+
}
|
|
437
|
+
case "area": {
|
|
438
|
+
const o = D(s.expr), l = s.lower ? D(s.lower) : () => 0;
|
|
439
|
+
i[`${s.id}.value`] = se((c) => o(c, r) - l(c, r), N(e, s.from), N(e, s.to));
|
|
440
|
+
break;
|
|
441
|
+
}
|
|
442
|
+
case "point":
|
|
443
|
+
if (s.on !== void 0 && s.x !== void 0) {
|
|
444
|
+
const o = N(e, s.x);
|
|
445
|
+
Object.assign(i, { [`${s.id}.x`]: o, [`${s.id}.y`]: D(s.on)(o, r) });
|
|
446
|
+
}
|
|
447
|
+
break;
|
|
448
|
+
case "taylor":
|
|
449
|
+
i[`${s.id}.order`] = Math.min(20, Math.max(0, N(e, s.order))), i[`${s.id}.degree`] = Math.floor(i[`${s.id}.order`]);
|
|
450
|
+
break;
|
|
451
|
+
}
|
|
452
|
+
return i;
|
|
453
|
+
}
|
|
454
|
+
const jt = {
|
|
455
|
+
alpha: "α",
|
|
456
|
+
beta: "β",
|
|
457
|
+
gamma: "γ",
|
|
458
|
+
delta: "δ",
|
|
459
|
+
epsilon: "ε",
|
|
460
|
+
varepsilon: "ε",
|
|
461
|
+
zeta: "ζ",
|
|
462
|
+
eta: "η",
|
|
463
|
+
theta: "θ",
|
|
464
|
+
iota: "ι",
|
|
465
|
+
kappa: "κ",
|
|
466
|
+
lambda: "λ",
|
|
467
|
+
mu: "μ",
|
|
468
|
+
nu: "ν",
|
|
469
|
+
xi: "ξ",
|
|
470
|
+
pi: "π",
|
|
471
|
+
rho: "ρ",
|
|
472
|
+
sigma: "σ",
|
|
473
|
+
tau: "τ",
|
|
474
|
+
upsilon: "υ",
|
|
475
|
+
phi: "φ",
|
|
476
|
+
varphi: "φ",
|
|
477
|
+
chi: "χ",
|
|
478
|
+
psi: "ψ",
|
|
479
|
+
omega: "ω",
|
|
480
|
+
Gamma: "Γ",
|
|
481
|
+
Delta: "Δ",
|
|
482
|
+
Theta: "Θ",
|
|
483
|
+
Lambda: "Λ",
|
|
484
|
+
Xi: "Ξ",
|
|
485
|
+
Pi: "Π",
|
|
486
|
+
Sigma: "Σ",
|
|
487
|
+
Phi: "Φ",
|
|
488
|
+
Psi: "Ψ",
|
|
489
|
+
Omega: "Ω"
|
|
490
|
+
}, Dt = {
|
|
491
|
+
cdot: ["·", "bin"],
|
|
492
|
+
times: ["×", "bin"],
|
|
493
|
+
div: ["÷", "bin"],
|
|
494
|
+
pm: ["±", "bin"],
|
|
495
|
+
mp: ["∓", "bin"],
|
|
496
|
+
circ: ["∘", "bin"],
|
|
497
|
+
cup: ["∪", "bin"],
|
|
498
|
+
cap: ["∩", "bin"],
|
|
499
|
+
land: ["∧", "bin"],
|
|
500
|
+
lor: ["∨", "bin"],
|
|
501
|
+
setminus: ["∖", "bin"],
|
|
502
|
+
le: ["≤", "rel"],
|
|
503
|
+
leq: ["≤", "rel"],
|
|
504
|
+
ge: ["≥", "rel"],
|
|
505
|
+
geq: ["≥", "rel"],
|
|
506
|
+
ne: ["≠", "rel"],
|
|
507
|
+
neq: ["≠", "rel"],
|
|
508
|
+
approx: ["≈", "rel"],
|
|
509
|
+
sim: ["∼", "rel"],
|
|
510
|
+
equiv: ["≡", "rel"],
|
|
511
|
+
propto: ["∝", "rel"],
|
|
512
|
+
to: ["→", "rel"],
|
|
513
|
+
rightarrow: ["→", "rel"],
|
|
514
|
+
leftarrow: ["←", "rel"],
|
|
515
|
+
mapsto: ["↦", "rel"],
|
|
516
|
+
Rightarrow: ["⇒", "rel"],
|
|
517
|
+
implies: ["⇒", "rel"],
|
|
518
|
+
iff: ["⇔", "rel"],
|
|
519
|
+
Leftrightarrow: ["⇔", "rel"],
|
|
520
|
+
in: ["∈", "rel"],
|
|
521
|
+
notin: ["∉", "rel"],
|
|
522
|
+
subset: ["⊂", "rel"],
|
|
523
|
+
subseteq: ["⊆", "rel"],
|
|
524
|
+
supset: ["⊃", "rel"],
|
|
525
|
+
mid: ["|", "rel"],
|
|
526
|
+
perp: ["⊥", "rel"],
|
|
527
|
+
infty: ["∞", "ord"],
|
|
528
|
+
partial: ["∂", "ord"],
|
|
529
|
+
nabla: ["∇", "ord"],
|
|
530
|
+
forall: ["∀", "ord"],
|
|
531
|
+
exists: ["∃", "ord"],
|
|
532
|
+
emptyset: ["∅", "ord"],
|
|
533
|
+
neg: ["¬", "ord"],
|
|
534
|
+
ldots: ["…", "ord"],
|
|
535
|
+
cdots: ["⋯", "ord"],
|
|
536
|
+
prime: ["′", "ord"],
|
|
537
|
+
hbar: ["ℏ", "ord"],
|
|
538
|
+
ell: ["ℓ", "ord"],
|
|
539
|
+
degree: ["°", "ord"],
|
|
540
|
+
lVert: ["‖", "open"],
|
|
541
|
+
rVert: ["‖", "close"],
|
|
542
|
+
langle: ["⟨", "open"],
|
|
543
|
+
rangle: ["⟩", "close"],
|
|
544
|
+
lbrace: ["{", "open"],
|
|
545
|
+
rbrace: ["}", "close"]
|
|
546
|
+
}, _t = { sum: ["∑", !0], prod: ["∏", !0], int: ["∫", !1], oint: ["∮", !1] }, Rt = {
|
|
547
|
+
sin: !1,
|
|
548
|
+
cos: !1,
|
|
549
|
+
tan: !1,
|
|
550
|
+
log: !1,
|
|
551
|
+
ln: !1,
|
|
552
|
+
exp: !1,
|
|
553
|
+
det: !1,
|
|
554
|
+
tr: !1,
|
|
555
|
+
rank: !1,
|
|
556
|
+
lim: !0,
|
|
557
|
+
max: !0,
|
|
558
|
+
min: !0,
|
|
559
|
+
argmax: !0,
|
|
560
|
+
argmin: !0,
|
|
561
|
+
arg: !1,
|
|
562
|
+
sup: !0,
|
|
563
|
+
inf: !0,
|
|
564
|
+
Pr: !1,
|
|
565
|
+
E: !1,
|
|
566
|
+
Var: !1
|
|
567
|
+
}, Vt = { ",": 3 / 18, ":": 4 / 18, ";": 5 / 18, "!": -3 / 18, quad: 1, qquad: 2 }, oe = /* @__PURE__ */ new Set(["pmatrix", "bmatrix", "vmatrix", "matrix"]);
|
|
568
|
+
class Ke extends Error {
|
|
569
|
+
source;
|
|
570
|
+
/** 0-based index into the source */
|
|
571
|
+
position;
|
|
572
|
+
constructor(e, n, i) {
|
|
573
|
+
super(`${e} at ${i + 1} in "${n}"`), this.name = "TexError", this.source = n, this.position = i;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
const Qe = [
|
|
577
|
+
...Object.keys(jt),
|
|
578
|
+
...Object.keys(Dt),
|
|
579
|
+
...Object.keys(_t),
|
|
580
|
+
...Object.keys(Rt),
|
|
581
|
+
...Object.keys(Vt).filter((t) => t.length > 1),
|
|
582
|
+
"frac",
|
|
583
|
+
"sqrt",
|
|
584
|
+
"vec",
|
|
585
|
+
"hat",
|
|
586
|
+
"bar",
|
|
587
|
+
"text",
|
|
588
|
+
"color",
|
|
589
|
+
"id",
|
|
590
|
+
"val",
|
|
591
|
+
"begin",
|
|
592
|
+
"end",
|
|
593
|
+
"left",
|
|
594
|
+
"right"
|
|
595
|
+
].sort();
|
|
596
|
+
function Je(t) {
|
|
597
|
+
if (typeof t != "string") throw new TypeError("parseTex: expected a string");
|
|
598
|
+
let e = 0;
|
|
599
|
+
const n = () => {
|
|
600
|
+
for (; e < t.length && /\s/.test(t[e]); ) e++;
|
|
601
|
+
}, i = (u, h = e) => {
|
|
602
|
+
throw new Ke(u, t, h);
|
|
603
|
+
}, r = () => {
|
|
604
|
+
const u = e;
|
|
605
|
+
if (/[A-Za-z]/.test(t[e] ?? ""))
|
|
606
|
+
for (; /[A-Za-z]/.test(t[e] ?? ""); ) e++;
|
|
607
|
+
else e < t.length ? e++ : i("A backslash with no command");
|
|
608
|
+
return t.slice(u, e);
|
|
609
|
+
}, s = (u) => {
|
|
610
|
+
n(), t[e] !== "{" && i(`Expected "{" for ${u}`);
|
|
611
|
+
const h = t.indexOf("}", e);
|
|
612
|
+
h < 0 && i(`Missing "}" for ${u}`);
|
|
613
|
+
const d = t.slice(e + 1, h);
|
|
614
|
+
return e = h + 1, d;
|
|
615
|
+
}, o = (u) => {
|
|
616
|
+
if (n(), e >= t.length && i(`Missing ${u}`), t[e] === "{") {
|
|
617
|
+
e++;
|
|
618
|
+
const d = l("}");
|
|
619
|
+
return e++, { type: "group", body: d };
|
|
620
|
+
}
|
|
621
|
+
const h = c();
|
|
622
|
+
return h || i(`Missing ${u}`), h;
|
|
623
|
+
}, l = (u) => {
|
|
624
|
+
const h = [];
|
|
625
|
+
for (; ; ) {
|
|
626
|
+
if (n(), e >= t.length)
|
|
627
|
+
return u === "}" && i('Missing "}"'), u === "cell" && i("Missing \\end"), h;
|
|
628
|
+
if (u === "}" && t[e] === "}" || (t[e] === "}" && i('Unexpected "}"'), u === "cell" && (t[e] === "&" || t.startsWith("\\\\", e) || t.startsWith("\\end", e)))) return h;
|
|
629
|
+
if (t[e] === "^" || t[e] === "_") {
|
|
630
|
+
const y = e, a = t[e] === "^";
|
|
631
|
+
e++;
|
|
632
|
+
const p = o(a ? "superscript" : "subscript");
|
|
633
|
+
let m = h.pop() ?? { type: "group", body: [] };
|
|
634
|
+
m.type !== "scripts" && (m = { type: "scripts", base: m }), m.type === "scripts" && ((a && m.sup || !a && m.sub) && i(`Double ${a ? "superscript" : "subscript"}`, y), a ? m.sup = p : m.sub = p), h.push(m);
|
|
635
|
+
continue;
|
|
636
|
+
}
|
|
637
|
+
if (t[e] === "'") {
|
|
638
|
+
e++;
|
|
639
|
+
const y = { type: "symbol", text: "′", cls: "ord", italic: !1 }, a = h.pop() ?? { type: "group", body: [] };
|
|
640
|
+
h.push(a.type === "scripts" && !a.sup ? { ...a, sup: y } : { type: "scripts", base: a, sup: y });
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
const d = c();
|
|
644
|
+
d && h.push(d);
|
|
645
|
+
}
|
|
646
|
+
}, c = () => {
|
|
647
|
+
const u = t[e];
|
|
648
|
+
if (u === "{") {
|
|
649
|
+
e++;
|
|
650
|
+
const h = l("}");
|
|
651
|
+
return e++, { type: "group", body: h };
|
|
652
|
+
}
|
|
653
|
+
if (u === "&" && i('"&" outside a matrix'), u === "\\") {
|
|
654
|
+
const h = e;
|
|
655
|
+
e++;
|
|
656
|
+
const d = r();
|
|
657
|
+
if (d === "\\" && i('"\\\\" outside a matrix', h), d in Vt) return { type: "space", width: Vt[d] };
|
|
658
|
+
if (d === "{" || d === "}") return { type: "symbol", text: d, cls: d === "{" ? "open" : "close", italic: !1 };
|
|
659
|
+
if (d === "|") return { type: "symbol", text: "‖", cls: "ord", italic: !1 };
|
|
660
|
+
if (Object.prototype.hasOwnProperty.call(jt, d))
|
|
661
|
+
return { type: "symbol", text: jt[d], cls: "ord", italic: d[0] === d[0].toLowerCase() };
|
|
662
|
+
if (Object.prototype.hasOwnProperty.call(Dt, d)) {
|
|
663
|
+
const [a, p] = Dt[d];
|
|
664
|
+
return { type: "symbol", text: a, cls: p, italic: !1 };
|
|
665
|
+
}
|
|
666
|
+
if (Object.prototype.hasOwnProperty.call(_t, d)) {
|
|
667
|
+
const [a, p] = _t[d];
|
|
668
|
+
return { type: "bigop", text: a, limits: p };
|
|
669
|
+
}
|
|
670
|
+
if (Object.prototype.hasOwnProperty.call(Rt, d)) return { type: "opname", text: d, limits: Rt[d] };
|
|
671
|
+
switch (d) {
|
|
672
|
+
case "frac":
|
|
673
|
+
return { type: "frac", num: o("numerator"), den: o("denominator") };
|
|
674
|
+
case "sqrt":
|
|
675
|
+
return { type: "sqrt", body: o("the square root's body") };
|
|
676
|
+
case "vec":
|
|
677
|
+
case "hat":
|
|
678
|
+
case "bar":
|
|
679
|
+
return { type: "accent", accent: d, body: o(`\\${d}'s body`) };
|
|
680
|
+
case "text":
|
|
681
|
+
return { type: "text", text: s("\\text") };
|
|
682
|
+
case "color": {
|
|
683
|
+
const a = s("\\color").trim();
|
|
684
|
+
return /^(#[0-9a-fA-F]{3,8}|[a-zA-Z]+)$/.test(a) || i(`"${a}" is not a colour name or #hex`, h), { type: "color", color: a, body: o("\\color's body") };
|
|
685
|
+
}
|
|
686
|
+
case "id": {
|
|
687
|
+
const a = s("\\id").trim();
|
|
688
|
+
return /^[\w.-]+$/.test(a) || i(`"${a}" is not a term id (letters, digits, _ . -)`, h), { type: "id", id: a, body: o("\\id's body") };
|
|
689
|
+
}
|
|
690
|
+
case "val": {
|
|
691
|
+
const a = s("\\val").trim(), p = /^([A-Za-z_][\w.]*)(?::(\d))?$/.exec(a);
|
|
692
|
+
return p || i(`"${a}" is not a value name (name or name:digits)`, h), { type: "value", name: p[1], digits: p[2] === void 0 ? 2 : Number(p[2]) };
|
|
693
|
+
}
|
|
694
|
+
case "left":
|
|
695
|
+
case "right": {
|
|
696
|
+
n();
|
|
697
|
+
const a = t[e] === "\\" ? (e++, r()) : t[e++];
|
|
698
|
+
if (a === ".") return { type: "space", width: 0 };
|
|
699
|
+
const m = { "(": "(", ")": ")", "[": "[", "]": "]", "|": "|", "{": "{", "}": "}", langle: "⟨", rangle: "⟩", "|.": "‖" }[a];
|
|
700
|
+
return m || i(`"${a}" is not a delimiter`, h), { type: "symbol", text: m, cls: d === "left" ? "open" : "close", italic: !1 };
|
|
701
|
+
}
|
|
702
|
+
case "begin": {
|
|
703
|
+
const a = s("\\begin").trim();
|
|
704
|
+
oe.has(a) || i(`Unknown environment "${a}" (use ${[...oe].join(", ")})`, h);
|
|
705
|
+
const p = [[]];
|
|
706
|
+
for (; ; ) {
|
|
707
|
+
const m = l("cell");
|
|
708
|
+
if (p[p.length - 1].push({ type: "group", body: m }), t[e] === "&")
|
|
709
|
+
e++;
|
|
710
|
+
else if (t.startsWith("\\\\", e))
|
|
711
|
+
e += 2, p.push([]);
|
|
712
|
+
else {
|
|
713
|
+
e += 4;
|
|
714
|
+
const b = s("\\end").trim();
|
|
715
|
+
b !== a && i(`\\begin{${a}} ended by \\end{${b}}`);
|
|
716
|
+
break;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
return p.length > 1 && p[p.length - 1].every((m) => m.type === "group" && m.body.length === 0) && p.pop(), { type: "matrix", delimiters: a, rows: p };
|
|
720
|
+
}
|
|
721
|
+
case "end":
|
|
722
|
+
return i("\\end without \\begin", h);
|
|
723
|
+
}
|
|
724
|
+
const y = je(d, Qe);
|
|
725
|
+
return i(`Unknown command "\\${d}"${y ? ` (did you mean "\\${y}"?)` : ""}`, h);
|
|
726
|
+
}
|
|
727
|
+
return e++, /[A-Za-z]/.test(u) ? { type: "symbol", text: u, cls: "ord", italic: !0 } : /[0-9.]/.test(u) ? { type: "symbol", text: u, cls: "ord", italic: !1 } : u === "+" ? { type: "symbol", text: "+", cls: "bin", italic: !1 } : u === "-" ? { type: "symbol", text: "−", cls: "bin", italic: !1 } : u === "*" ? { type: "symbol", text: "∗", cls: "bin", italic: !1 } : "=<>".includes(u) ? { type: "symbol", text: u, cls: "rel", italic: !1 } : "([".includes(u) ? { type: "symbol", text: u, cls: "open", italic: !1 } : ")]".includes(u) ? { type: "symbol", text: u, cls: "close", italic: !1 } : ",;".includes(u) ? { type: "symbol", text: u, cls: "punct", italic: !1 } : "|/!:".includes(u) ? { type: "symbol", text: u, cls: u === ":" ? "rel" : "ord", italic: !1 } : i(`Unexpected "${u}"`, e - 1);
|
|
728
|
+
};
|
|
729
|
+
return { type: "group", body: l("") };
|
|
730
|
+
}
|
|
731
|
+
const tn = '"Latin Modern Math", "STIX Two Math", "Cambria Math", "Times New Roman", "Liberation Serif", "Noto Serif", Times, serif', Ee = 0.25, mt = 0.05, ae = {
|
|
732
|
+
a: 0.5,
|
|
733
|
+
b: 0.56,
|
|
734
|
+
c: 0.44,
|
|
735
|
+
d: 0.56,
|
|
736
|
+
e: 0.44,
|
|
737
|
+
f: 0.31,
|
|
738
|
+
g: 0.5,
|
|
739
|
+
h: 0.56,
|
|
740
|
+
i: 0.28,
|
|
741
|
+
j: 0.31,
|
|
742
|
+
k: 0.53,
|
|
743
|
+
l: 0.28,
|
|
744
|
+
m: 0.83,
|
|
745
|
+
n: 0.56,
|
|
746
|
+
o: 0.5,
|
|
747
|
+
p: 0.56,
|
|
748
|
+
q: 0.53,
|
|
749
|
+
r: 0.39,
|
|
750
|
+
s: 0.39,
|
|
751
|
+
t: 0.39,
|
|
752
|
+
u: 0.56,
|
|
753
|
+
v: 0.53,
|
|
754
|
+
w: 0.72,
|
|
755
|
+
x: 0.53,
|
|
756
|
+
y: 0.53,
|
|
757
|
+
z: 0.44,
|
|
758
|
+
A: 0.75,
|
|
759
|
+
B: 0.71,
|
|
760
|
+
C: 0.72,
|
|
761
|
+
D: 0.76,
|
|
762
|
+
E: 0.68,
|
|
763
|
+
F: 0.65,
|
|
764
|
+
G: 0.78,
|
|
765
|
+
H: 0.75,
|
|
766
|
+
I: 0.36,
|
|
767
|
+
J: 0.51,
|
|
768
|
+
K: 0.78,
|
|
769
|
+
L: 0.63,
|
|
770
|
+
M: 0.92,
|
|
771
|
+
N: 0.75,
|
|
772
|
+
O: 0.78,
|
|
773
|
+
P: 0.68,
|
|
774
|
+
Q: 0.78,
|
|
775
|
+
R: 0.74,
|
|
776
|
+
S: 0.56,
|
|
777
|
+
T: 0.72,
|
|
778
|
+
U: 0.75,
|
|
779
|
+
V: 0.75,
|
|
780
|
+
W: 1.03,
|
|
781
|
+
X: 0.75,
|
|
782
|
+
Y: 0.75,
|
|
783
|
+
Z: 0.61
|
|
784
|
+
};
|
|
785
|
+
function Wt(t) {
|
|
786
|
+
return t.length !== 1 ? t.length * 0.5 : /[0-9]/.test(t) ? 0.5 : Object.prototype.hasOwnProperty.call(ae, t) ? ae[t] + 0.04 : "()[]".includes(t) ? 0.39 : "{}".includes(t) ? 0.5 : "|".includes(t) || ",.;:!".includes(t) ? 0.28 : "+−=<>±×÷≤≥≠≈∼≡∝→←↔⇒⇔∈∉⊂⊃⊆⊇∪∩∧∨".includes(t) ? 0.78 : "·∘".includes(t) ? 0.28 : "∑∏".includes(t) ? 1.05 : "∫".includes(t) ? 0.56 : "∞".includes(t) || "…⋯".includes(t) ? 1 : /[Α-Ω]/.test(t) ? 0.72 : /[α-ω]/.test(t) ? 0.56 : 0.6;
|
|
787
|
+
}
|
|
788
|
+
function Ae(t) {
|
|
789
|
+
return "gjpqy(),;[]{}|∫".includes(t) || "βγζημξρφχψ".includes(t) ? { height: 0.72, depth: 0.22 } : "∑∏".includes(t) ? { height: 0.75, depth: 0.25 } : /[a-z]/.test(t) && !"bdfhklt".includes(t) ? { height: 0.45, depth: 0 } : { height: 0.7, depth: 0 };
|
|
790
|
+
}
|
|
791
|
+
const Y = [1, 1, 0.7, 0.5], en = (t) => t <= 1 ? 2 : 3, nn = (t) => Math.min(3, t + 1), Zt = () => ({ width: 0, height: 0, depth: 0, pieces: [] });
|
|
792
|
+
function X(t, e, n) {
|
|
793
|
+
return t.pieces.map((i) => i.kind === "stroke" ? { ...i, points: i.points.map(([r, s]) => [r + e, s + n]) } : { ...i, x: i.x + e, y: i.y + n });
|
|
794
|
+
}
|
|
795
|
+
function ct(t, e) {
|
|
796
|
+
if (t.id) return t.id;
|
|
797
|
+
const n = (t.counts.get(e) ?? 0) + 1;
|
|
798
|
+
return t.counts.set(e, n), `${e}#${n}`;
|
|
799
|
+
}
|
|
800
|
+
function le(t, e, n, i = 1, r = t) {
|
|
801
|
+
const s = Y[n.level] * i, o = Wt(t) * s, { height: l, depth: c } = Ae(t);
|
|
802
|
+
return {
|
|
803
|
+
width: o,
|
|
804
|
+
height: l * s,
|
|
805
|
+
depth: c * s,
|
|
806
|
+
pieces: [{ kind: "glyph", text: t, x: 0, y: 0, width: o, size: s, italic: e, ...n.color ? { color: n.color } : {}, id: ct(n, r) }]
|
|
807
|
+
};
|
|
808
|
+
}
|
|
809
|
+
function Ft(t, e, n) {
|
|
810
|
+
const i = Zt(), r = ct(e, n), s = Y[e.level];
|
|
811
|
+
for (const o of t) {
|
|
812
|
+
const l = (o === " " ? 0.28 : /[A-Za-z]/.test(o) ? Wt(o) - 0.04 : Wt(o)) * s;
|
|
813
|
+
if (o !== " ") {
|
|
814
|
+
const { height: c, depth: f } = Ae(o);
|
|
815
|
+
i.pieces.push({ kind: "glyph", text: o, x: i.width, y: 0, width: l, size: s, italic: !1, ...e.color ? { color: e.color } : {}, id: r }), i.height = Math.max(i.height, c * s), i.depth = Math.max(i.depth, f * s);
|
|
816
|
+
}
|
|
817
|
+
i.width += l;
|
|
818
|
+
}
|
|
819
|
+
return i;
|
|
820
|
+
}
|
|
821
|
+
function Bt(t) {
|
|
822
|
+
switch (t.type) {
|
|
823
|
+
case "symbol":
|
|
824
|
+
return t.cls;
|
|
825
|
+
case "bigop":
|
|
826
|
+
case "opname":
|
|
827
|
+
return "op";
|
|
828
|
+
case "scripts":
|
|
829
|
+
return Bt(t.base);
|
|
830
|
+
case "id":
|
|
831
|
+
case "color":
|
|
832
|
+
return Bt(t.body);
|
|
833
|
+
default:
|
|
834
|
+
return "ord";
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
function rn(t, e) {
|
|
838
|
+
return t === "bin" || e === "bin" ? 4 : t === "rel" || e === "rel" ? t === "open" || e === "close" || e === "punct" ? 0 : 5 : t === "punct" || t === "op" && (e === "ord" || e === "op") || e === "op" && t === "ord" ? 3 : 0;
|
|
839
|
+
}
|
|
840
|
+
function sn(t, e) {
|
|
841
|
+
const n = Zt();
|
|
842
|
+
let i;
|
|
843
|
+
return t.forEach((r) => {
|
|
844
|
+
if (r.type === "space") {
|
|
845
|
+
n.width += r.width * Y[e.level];
|
|
846
|
+
return;
|
|
847
|
+
}
|
|
848
|
+
let s = Bt(r);
|
|
849
|
+
s === "bin" && (i === void 0 || i === "bin" || i === "rel" || i === "open" || i === "punct" || i === "op") && (s = "ord"), i !== void 0 && e.level <= 1 && (n.width += rn(i, s) / 18 * Y[e.level]);
|
|
850
|
+
const o = R(r, e);
|
|
851
|
+
n.pieces.push(...X(o, n.width, 0)), n.width += o.width, n.height = Math.max(n.height, o.height), n.depth = Math.max(n.depth, o.depth), i = s;
|
|
852
|
+
}), n;
|
|
853
|
+
}
|
|
854
|
+
function on(t, e) {
|
|
855
|
+
if (!Number.isFinite(t)) return t > 0 ? "∞" : t < 0 ? "−∞" : "?";
|
|
856
|
+
const n = t.toFixed(e), i = n.includes(".") ? n.replace(/\.?0+$/, "") : n;
|
|
857
|
+
return (i === "-0" ? "0" : i).replace("-", "−");
|
|
858
|
+
}
|
|
859
|
+
function R(t, e) {
|
|
860
|
+
const n = Y[e.level];
|
|
861
|
+
switch (t.type) {
|
|
862
|
+
case "symbol":
|
|
863
|
+
return le(t.text, t.italic, e);
|
|
864
|
+
case "group":
|
|
865
|
+
return sn(t.body, e);
|
|
866
|
+
case "space":
|
|
867
|
+
return { ...Zt(), width: t.width * n };
|
|
868
|
+
case "text":
|
|
869
|
+
return Ft(t.text, e, `text:${t.text}`);
|
|
870
|
+
case "opname":
|
|
871
|
+
return Ft(t.text, e, t.text);
|
|
872
|
+
case "value": {
|
|
873
|
+
const i = e.values[t.name];
|
|
874
|
+
if (i === void 0) throw new Error(`\\val{${t.name}}: no value named "${t.name}"`);
|
|
875
|
+
return Ft(on(i, t.digits), { ...e, id: e.id ?? `val:${t.name}` }, `val:${t.name}`);
|
|
876
|
+
}
|
|
877
|
+
case "bigop":
|
|
878
|
+
return le(t.text, !1, e, e.level === 0 ? 1.4 : 1.1);
|
|
879
|
+
case "color":
|
|
880
|
+
return R(t.body, { ...e, color: t.color });
|
|
881
|
+
case "id":
|
|
882
|
+
return R(t.body, { ...e, id: t.id });
|
|
883
|
+
case "scripts":
|
|
884
|
+
return an(t, e);
|
|
885
|
+
case "frac":
|
|
886
|
+
return ln(t, e);
|
|
887
|
+
case "sqrt":
|
|
888
|
+
return cn(t, e);
|
|
889
|
+
case "accent":
|
|
890
|
+
return un(t, e);
|
|
891
|
+
case "matrix":
|
|
892
|
+
return dn(t, e);
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
function an(t, e) {
|
|
896
|
+
const n = R(t.base, e), i = { ...e, level: en(e.level) }, r = t.sup ? R(t.sup, i) : void 0, s = t.sub ? R(t.sub, i) : void 0, o = Y[e.level], l = e.level === 0 && (t.base.type === "bigop" && t.base.limits || t.base.type === "opname" && t.base.limits), c = { ...n, pieces: [...n.pieces] };
|
|
897
|
+
if (l) {
|
|
898
|
+
const y = Math.max(n.width, r?.width ?? 0, s?.width ?? 0), a = X(n, (y - n.width) / 2, 0);
|
|
899
|
+
let p = n.height, m = n.depth;
|
|
900
|
+
const b = 0.12 * o;
|
|
901
|
+
if (r) {
|
|
902
|
+
const w = -(n.height + b + r.depth);
|
|
903
|
+
a.push(...X(r, (y - r.width) / 2, w)), p = -w + r.height;
|
|
904
|
+
}
|
|
905
|
+
if (s) {
|
|
906
|
+
const w = n.depth + b + s.height;
|
|
907
|
+
a.push(...X(s, (y - s.width) / 2, w)), m = w + s.depth;
|
|
908
|
+
}
|
|
909
|
+
return { width: y, height: p, depth: m, pieces: a };
|
|
910
|
+
}
|
|
911
|
+
let f = r ? Math.max(0.42 * o, n.height - 0.3 * o, r.depth + 0.25 * o) : 0, u = s ? Math.max(0.2 * o, n.depth + 0.12 * o, s.height - 0.4 * o) : 0;
|
|
912
|
+
if (r && s) {
|
|
913
|
+
const y = f - r.depth - (s.height - u);
|
|
914
|
+
y < 0.2 * o && (u += 0.2 * o - y);
|
|
915
|
+
}
|
|
916
|
+
const h = n.width + 0.03 * o;
|
|
917
|
+
let d = n.width;
|
|
918
|
+
return r ? (c.pieces.push(...X(r, h, -f)), c.height = Math.max(c.height, f + r.height), d = Math.max(d, h + r.width)) : f = 0, s && (c.pieces.push(...X(s, h, u)), c.depth = Math.max(c.depth, u + s.depth), d = Math.max(d, h + s.width)), c.width = d + 0.04 * o, c;
|
|
919
|
+
}
|
|
920
|
+
function ln(t, e) {
|
|
921
|
+
const n = { ...e, level: nn(e.level) }, i = R(t.num, n), r = R(t.den, n), s = Y[e.level], o = mt * s, l = Ee * s, c = (e.level === 0 ? 0.16 : 0.1) * s, f = 0.1 * s, u = Math.max(i.width, r.width) + f * 2, h = -(l + o / 2 + c + i.depth), d = -l + o / 2 + c + r.height;
|
|
922
|
+
return {
|
|
923
|
+
width: u,
|
|
924
|
+
height: -h + i.height,
|
|
925
|
+
depth: d + r.depth,
|
|
926
|
+
pieces: [
|
|
927
|
+
...X(i, (u - i.width) / 2, h),
|
|
928
|
+
{ kind: "rule", x: 0, y: -l - o / 2, width: u, height: o, ...e.color ? { color: e.color } : {}, id: ct(e, "frac") },
|
|
929
|
+
...X(r, (u - r.width) / 2, d)
|
|
930
|
+
]
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
function cn(t, e) {
|
|
934
|
+
const n = R(t.body, e), i = Y[e.level], r = Math.max(n.height, 0.7 * i) + 0.14 * i, s = Math.max(n.depth, 0.05 * i) + 0.05 * i, o = 0.56 * i, l = o + n.width + 0.08 * i, c = [
|
|
935
|
+
[0.02 * i, -0.36 * i],
|
|
936
|
+
[0.13 * i, -0.44 * i],
|
|
937
|
+
[0.3 * i, s],
|
|
938
|
+
[o - 0.04 * i, -r],
|
|
939
|
+
[l, -r]
|
|
940
|
+
];
|
|
941
|
+
return {
|
|
942
|
+
width: l,
|
|
943
|
+
height: r + mt * i,
|
|
944
|
+
depth: s,
|
|
945
|
+
pieces: [{ kind: "stroke", points: c, width: mt * i, ...e.color ? { color: e.color } : {}, id: ct(e, "sqrt") }, ...X(n, o, 0)]
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
function un(t, e) {
|
|
949
|
+
const n = R(t.body, e), i = Y[e.level], r = -(n.height + 0.12 * i), s = 0.04 * i, o = Math.max(n.width - 0.02 * i, s + 0.3 * i), l = e.color ? { color: e.color } : {}, c = ct(e, t.accent);
|
|
950
|
+
let f;
|
|
951
|
+
if (t.accent === "vec") {
|
|
952
|
+
const u = 0.12 * i;
|
|
953
|
+
f = [[[s, r], [o, r]], [[o - u, r - u * 0.7], [o, r], [o - u, r + u * 0.7]]];
|
|
954
|
+
} else if (t.accent === "hat") {
|
|
955
|
+
const u = (s + o) / 2;
|
|
956
|
+
f = [[[u - 0.16 * i, r + 0.02 * i], [u, r - 0.12 * i], [u + 0.16 * i, r + 0.02 * i]]];
|
|
957
|
+
} else
|
|
958
|
+
f = [[[s, r], [o, r]]];
|
|
959
|
+
return {
|
|
960
|
+
width: Math.max(n.width, o + 0.02 * i),
|
|
961
|
+
height: -r + 0.14 * i,
|
|
962
|
+
depth: n.depth,
|
|
963
|
+
pieces: [...n.pieces, ...f.map((u) => ({ kind: "stroke", points: u, width: mt * i * 0.9, ...l, id: c }))]
|
|
964
|
+
};
|
|
965
|
+
}
|
|
966
|
+
function dn(t, e) {
|
|
967
|
+
const n = { ...e, level: Math.max(e.level, 1) }, i = Y[n.level], r = t.rows.map((v) => v.map((x) => R(x, n))), s = Math.max(...r.map((v) => v.length)), o = Array.from({ length: s }, (v, x) => Math.max(0, ...r.map((M) => M[x]?.width ?? 0))), l = r.map((v) => Math.max(0.7 * i, ...v.map((x) => x.height))), c = r.map((v) => Math.max(0.2 * i, ...v.map((x) => x.depth))), f = 0.9 * i, u = 0.25 * i, h = t.delimiters === "matrix" ? 0 : 0.55 * i, d = o.reduce((v, x) => v + x, 0) + f * (s - 1), y = l.reduce((v, x, M) => v + x + c[M], 0) + u * (r.length - 1), a = -Ee * i - y / 2, p = [];
|
|
968
|
+
let m = a;
|
|
969
|
+
r.forEach((v, x) => {
|
|
970
|
+
m += l[x];
|
|
971
|
+
let M = h;
|
|
972
|
+
v.forEach((T, P) => {
|
|
973
|
+
p.push(...X(T, M + (o[P] - T.width) / 2, m)), M += o[P] + f;
|
|
974
|
+
}), m += c[x] + u;
|
|
975
|
+
});
|
|
976
|
+
const b = d + h * 2, w = a + y, $ = e.color ? { color: e.color } : {}, g = (v, x) => ({ kind: "stroke", points: v, width: mt * i * 1.1, ...$, id: ct(e, x) }), k = 0.08 * i;
|
|
977
|
+
if (t.delimiters === "pmatrix") {
|
|
978
|
+
const v = (x) => {
|
|
979
|
+
const M = x === 1 ? h * 0.75 : b - h * 0.75, T = x * h * 0.5;
|
|
980
|
+
return Array.from({ length: 17 }, (P, K) => {
|
|
981
|
+
const j = K / 16;
|
|
982
|
+
return [M - T * Math.sin(Math.PI * j), a - k + (y + k * 2) * j];
|
|
983
|
+
});
|
|
984
|
+
};
|
|
985
|
+
p.push(g(v(1), "("), g(v(-1), ")"));
|
|
986
|
+
} else if (t.delimiters === "bmatrix") {
|
|
987
|
+
const v = h * 0.35, x = h * 0.2, M = b - h * 0.2;
|
|
988
|
+
p.push(
|
|
989
|
+
g([[x + v, a - k], [x, a - k], [x, w + k], [x + v, w + k]], "["),
|
|
990
|
+
g([[M - v, a - k], [M, a - k], [M, w + k], [M - v, w + k]], "]")
|
|
991
|
+
);
|
|
992
|
+
} else t.delimiters === "vmatrix" && p.push(g([[h * 0.4, a - k], [h * 0.4, w + k]], "|"), g([[b - h * 0.4, a - k], [b - h * 0.4, w + k]], "|"));
|
|
993
|
+
return { width: b, height: -a + k, depth: w + k, pieces: p };
|
|
994
|
+
}
|
|
995
|
+
function ut(t, e = {}) {
|
|
996
|
+
const n = typeof t == "string" ? Je(t) : t, i = { level: e.style === "inline" ? 1 : 0, values: e.values ?? {}, counts: /* @__PURE__ */ new Map() }, r = R(n, i);
|
|
997
|
+
return { width: r.width, height: r.height, depth: r.depth, pieces: r.pieces };
|
|
998
|
+
}
|
|
999
|
+
const V = (t, e, n) => t + (e - t) * n;
|
|
1000
|
+
function ce(t, e, n) {
|
|
1001
|
+
return t.kind === "stroke" ? { ...t, points: t.points.map(([i, r]) => [i + e, r + n]) } : { ...t, x: t.x + e, y: t.y + n };
|
|
1002
|
+
}
|
|
1003
|
+
function hn(t, e, n) {
|
|
1004
|
+
if (t.kind === "glyph" && e.kind === "glyph" && t.text === e.text)
|
|
1005
|
+
return { ...e, x: V(t.x, e.x, n), y: V(t.y, e.y, n), width: V(t.width, e.width, n), size: V(t.size, e.size, n) };
|
|
1006
|
+
if (t.kind === "rule" && e.kind === "rule")
|
|
1007
|
+
return { ...e, x: V(t.x, e.x, n), y: V(t.y, e.y, n), width: V(t.width, e.width, n), height: V(t.height, e.height, n) };
|
|
1008
|
+
if (t.kind === "stroke" && e.kind === "stroke" && t.points.length === e.points.length)
|
|
1009
|
+
return { ...e, width: V(t.width, e.width, n), points: t.points.map(([i, r], s) => [V(i, e.points[s][0], n), V(r, e.points[s][1], n)]) };
|
|
1010
|
+
}
|
|
1011
|
+
const fn = (t) => t * t * (3 - 2 * t);
|
|
1012
|
+
function pn(t, e, n) {
|
|
1013
|
+
const i = Math.min(1, Math.max(0, n)), r = t.layout.pieces.map((y) => ce(y, t.x, t.y)), s = e.layout.pieces.map((y) => ce(y, e.x, e.y));
|
|
1014
|
+
if (i === 0) return r.map((y) => ({ ...y, opacity: 1 }));
|
|
1015
|
+
if (i === 1) return s.map((y) => ({ ...y, opacity: 1 }));
|
|
1016
|
+
const o = (y) => {
|
|
1017
|
+
const a = /* @__PURE__ */ new Map();
|
|
1018
|
+
for (const p of y) a.set(p.id, [...a.get(p.id) ?? [], p]);
|
|
1019
|
+
return a;
|
|
1020
|
+
}, l = o(r), c = o(s), f = [], u = fn(i), h = Math.max(0, 1 - i * 2), d = Math.max(0, i * 2 - 1);
|
|
1021
|
+
for (const y of /* @__PURE__ */ new Set([...l.keys(), ...c.keys()])) {
|
|
1022
|
+
const a = l.get(y) ?? [], p = c.get(y) ?? [], m = Math.max(a.length, p.length);
|
|
1023
|
+
for (let b = 0; b < m; b++) {
|
|
1024
|
+
const w = a[b], $ = p[b], g = w && $ ? hn(w, $, u) : void 0;
|
|
1025
|
+
if (g) {
|
|
1026
|
+
f.push({ ...g, opacity: 1 });
|
|
1027
|
+
continue;
|
|
1028
|
+
}
|
|
1029
|
+
w && f.push({ ...w, opacity: h }), $ && f.push({ ...$, opacity: d });
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
return f.filter((y) => y.opacity > 0);
|
|
1033
|
+
}
|
|
1034
|
+
const ue = [
|
|
1035
|
+
"grid",
|
|
1036
|
+
"axes",
|
|
1037
|
+
"vector",
|
|
1038
|
+
"basis",
|
|
1039
|
+
"point",
|
|
1040
|
+
"function",
|
|
1041
|
+
"parametric",
|
|
1042
|
+
"polygon",
|
|
1043
|
+
"line",
|
|
1044
|
+
"label",
|
|
1045
|
+
"equation",
|
|
1046
|
+
"tangent",
|
|
1047
|
+
"riemann",
|
|
1048
|
+
"area",
|
|
1049
|
+
"taylor"
|
|
1050
|
+
], Ut = {
|
|
1051
|
+
dark: {
|
|
1052
|
+
background: "#0f1117",
|
|
1053
|
+
ink: "#f2f2f2",
|
|
1054
|
+
grid: "#29abca",
|
|
1055
|
+
ghost: "rgba(255,255,255,0.18)",
|
|
1056
|
+
axes: "#f2f2f2",
|
|
1057
|
+
colors: { blue: "#58c4dd", green: "#83c167", red: "#fc6255", yellow: "#ffd75e", purple: "#b189f0", orange: "#ff9b4d", teal: "#5cd0b3", pink: "#f58ad1", ink: "#f2f2f2", grey: "#8a8f99" }
|
|
1058
|
+
},
|
|
1059
|
+
light: {
|
|
1060
|
+
background: "#fbfaf7",
|
|
1061
|
+
ink: "#1f2430",
|
|
1062
|
+
grid: "#7fb3d5",
|
|
1063
|
+
ghost: "rgba(31,36,48,0.14)",
|
|
1064
|
+
axes: "#1f2430",
|
|
1065
|
+
colors: { blue: "#2f6fd0", green: "#2f9a4a", red: "#d64545", yellow: "#c99a00", purple: "#7b4fc9", orange: "#e0761f", teal: "#178f7c", pink: "#c94f9c", ink: "#1f2430", grey: "#7a808c" }
|
|
1066
|
+
},
|
|
1067
|
+
chalk: {
|
|
1068
|
+
background: "#23332b",
|
|
1069
|
+
ink: "#eef0e6",
|
|
1070
|
+
grid: "rgba(238,240,230,0.35)",
|
|
1071
|
+
ghost: "rgba(238,240,230,0.14)",
|
|
1072
|
+
axes: "#eef0e6",
|
|
1073
|
+
colors: { blue: "#9fd3f0", green: "#b9e39a", red: "#f59a8f", yellow: "#f6e58d", purple: "#d0b5f2", orange: "#f7c08a", teal: "#a3e4d4", pink: "#f4b6dc", ink: "#eef0e6", grey: "#a9b0a6" }
|
|
1074
|
+
}
|
|
1075
|
+
}, mn = /* @__PURE__ */ new Set([
|
|
1076
|
+
"x",
|
|
1077
|
+
"y",
|
|
1078
|
+
"z",
|
|
1079
|
+
"t",
|
|
1080
|
+
"opacity",
|
|
1081
|
+
"rotate",
|
|
1082
|
+
"rotateX",
|
|
1083
|
+
"rotateY",
|
|
1084
|
+
"scale",
|
|
1085
|
+
"scaleX",
|
|
1086
|
+
"scaleY",
|
|
1087
|
+
"skewX",
|
|
1088
|
+
"skewY",
|
|
1089
|
+
"originX",
|
|
1090
|
+
"originY",
|
|
1091
|
+
"perspective",
|
|
1092
|
+
"width",
|
|
1093
|
+
"height",
|
|
1094
|
+
"fill",
|
|
1095
|
+
"stroke",
|
|
1096
|
+
"fillStyle",
|
|
1097
|
+
"strokeStyle",
|
|
1098
|
+
"lineWidth",
|
|
1099
|
+
"strokeWidth",
|
|
1100
|
+
"filter",
|
|
1101
|
+
"blur",
|
|
1102
|
+
"visible",
|
|
1103
|
+
"clipTop",
|
|
1104
|
+
"clipRight",
|
|
1105
|
+
"clipBottom",
|
|
1106
|
+
"clipLeft",
|
|
1107
|
+
"quaternion"
|
|
1108
|
+
]);
|
|
1109
|
+
function bt(t) {
|
|
1110
|
+
const e = (a) => {
|
|
1111
|
+
throw new Error(`math figure: ${a}`);
|
|
1112
|
+
};
|
|
1113
|
+
(!t || typeof t != "object") && e("expected a figure object"), (!(t.width > 0) || !(t.height > 0)) && e("width and height must be positive numbers (px)");
|
|
1114
|
+
for (const a of ["x", "y"]) {
|
|
1115
|
+
const p = t[a];
|
|
1116
|
+
p !== void 0 && !(Array.isArray(p) && p.length === 2 && p.every(Number.isFinite) && p[0] < p[1]) && e(`${a} must be [min, max] with min < max`);
|
|
1117
|
+
}
|
|
1118
|
+
t.theme !== void 0 && !(t.theme in Ut) && e(it("theme", t.theme, Object.keys(Ut))), Array.isArray(t.items) || e("items must be an array");
|
|
1119
|
+
const n = t.values ?? {};
|
|
1120
|
+
for (const [a, p] of Object.entries(n))
|
|
1121
|
+
/^[A-Za-z_]\w*$/.test(a) || e(`value name "${a}" must be letters, digits and _ (starting with a letter)`), mn.has(a) && e(`"${a}" cannot name a value: it is ${a === "x" || a === "t" ? "the variable of functions and curves" : "a property of the figure itself (its place, size or look)"}`), typeof p == "number" && Number.isFinite(p) || tt(p) || lt(p) || e(`value "${a}" must be a number, an [x, y] vector or an [a, b, c, d] matrix`);
|
|
1122
|
+
const i = $e(n), r = Object.keys(n).filter((a) => typeof n[a] == "number"), s = (a, p, m) => {
|
|
1123
|
+
m !== void 0 && (typeof m == "string" ? (m in n || e(`${a.id}.${p}: ${it("value", m, Object.keys(n))}`), tt(n[m]) || e(`${a.id}.${p}: value "${m}" is not an [x, y] vector`)) : tt(m) || e(`${a.id}.${p} must be [x, y] or the name of a vector value`));
|
|
1124
|
+
}, o = (a, p, m) => {
|
|
1125
|
+
m !== void 0 && (m in n || e(`${a.id}.${p}: ${it("value", m, Object.keys(n))}`), lt(n[m]) || e(`${a.id}.${p}: value "${m}" is not an [a, b, c, d] matrix`));
|
|
1126
|
+
}, l = (a, p, m, b) => {
|
|
1127
|
+
let w = [];
|
|
1128
|
+
try {
|
|
1129
|
+
w = Ze(At(m));
|
|
1130
|
+
} catch ($) {
|
|
1131
|
+
e(`${a.id}.${p}: ${$.message}`);
|
|
1132
|
+
}
|
|
1133
|
+
for (const $ of w)
|
|
1134
|
+
$ !== b && !($ in Et) && !r.includes($) && e(`${a.id}.${p}: "${$}" is neither ${b} nor a number value (${it("value", $, [b, ...r])})`);
|
|
1135
|
+
}, c = [], f = (a, p, m) => {
|
|
1136
|
+
c.push([a, p, m]);
|
|
1137
|
+
}, u = (a, p, m) => {
|
|
1138
|
+
m !== void 0 && (typeof m == "string" ? (m in n || e(`${a.id}.${p}: ${it("value", m, r)}`), typeof n[m] != "number" && e(`${a.id}.${p}: value "${m}" is not a number`)) : Number.isFinite(m) || e(`${a.id}.${p} must be a number or the name of a number value`));
|
|
1139
|
+
}, h = (a, p, m, b) => {
|
|
1140
|
+
try {
|
|
1141
|
+
let w = At(m);
|
|
1142
|
+
for (let $ = 0; $ < b; $++) w = G(w);
|
|
1143
|
+
} catch (w) {
|
|
1144
|
+
e(`${a.id}.${p}: ${w.message} (calculus needs functions with derivatives)`);
|
|
1145
|
+
}
|
|
1146
|
+
}, d = /* @__PURE__ */ new Set();
|
|
1147
|
+
for (const a of t.items)
|
|
1148
|
+
switch ((!a || typeof a != "object") && e("every item must be an object"), ue.includes(a.kind) || e(it("item kind", a.kind, ue)), (typeof a.id != "string" || !/^[A-Za-z_][\w-]*$/.test(a.id)) && e(`a ${a.kind} needs an id (letters, digits, _ and -)`), d.has(a.id) && e(`two items have the id "${a.id}"`), a.id in n && e(`"${a.id}" is both an item id and a value name`), d.add(a.id), a.kind) {
|
|
1149
|
+
case "grid":
|
|
1150
|
+
a.step !== void 0 && !(a.step > 0) && e(`${a.id}.step must be positive`), o(a, "transform", a.transform);
|
|
1151
|
+
break;
|
|
1152
|
+
case "vector":
|
|
1153
|
+
s(a, "to", a.to), s(a, "from", a.from), o(a, "transform", a.transform), a.label && f(a, "label", a.label);
|
|
1154
|
+
break;
|
|
1155
|
+
case "basis":
|
|
1156
|
+
o(a, "transform", a.transform);
|
|
1157
|
+
break;
|
|
1158
|
+
case "point":
|
|
1159
|
+
a.at === void 0 == (a.on === void 0) && e(`${a.id} needs at, or on with x (a point on a curve)`), a.on !== void 0 && (a.x === void 0 && e(`${a.id}: a point on a curve needs x`), l(a, "on", a.on, "x"), u(a, "x", a.x)), s(a, "at", a.at), o(a, "transform", a.transform), a.label && f(a, "label", a.label);
|
|
1160
|
+
break;
|
|
1161
|
+
case "function":
|
|
1162
|
+
l(a, "expr", a.expr, "x"), a.derivative !== void 0 && ((!Number.isInteger(a.derivative) || a.derivative < 0 || a.derivative > 8) && e(`${a.id}.derivative must be a whole number from 0 to 8`), h(a, "expr", a.expr, a.derivative));
|
|
1163
|
+
break;
|
|
1164
|
+
case "tangent":
|
|
1165
|
+
l(a, "expr", a.expr, "x"), h(a, "expr", a.expr, 1), u(a, "at", a.at), u(a, "h", a.h), a.label && f(a, "label", a.label);
|
|
1166
|
+
break;
|
|
1167
|
+
case "riemann":
|
|
1168
|
+
l(a, "expr", a.expr, "x"), u(a, "from", a.from), u(a, "to", a.to), u(a, "n", a.n), a.rule !== void 0 && !["left", "right", "midpoint", "trapezoid"].includes(a.rule) && e(`${a.id}.rule: ${it("rule", a.rule, ["left", "right", "midpoint", "trapezoid"])}`);
|
|
1169
|
+
break;
|
|
1170
|
+
case "area":
|
|
1171
|
+
l(a, "expr", a.expr, "x"), a.lower !== void 0 && l(a, "lower", a.lower, "x"), u(a, "from", a.from), u(a, "to", a.to);
|
|
1172
|
+
break;
|
|
1173
|
+
case "taylor":
|
|
1174
|
+
l(a, "expr", a.expr, "x"), h(a, "expr", a.expr, 3), u(a, "at", a.at), u(a, "order", a.order);
|
|
1175
|
+
break;
|
|
1176
|
+
case "parametric":
|
|
1177
|
+
l(a, "x", a.x, "t"), l(a, "y", a.y, "t"), o(a, "transform", a.transform);
|
|
1178
|
+
break;
|
|
1179
|
+
case "polygon":
|
|
1180
|
+
a.points !== void 0 && !(Array.isArray(a.points) && a.points.length >= 3 && a.points.every(tt)) && e(`${a.id}.points must be three or more [x, y] points`), o(a, "transform", a.transform), a.label && f(a, "label", a.label);
|
|
1181
|
+
break;
|
|
1182
|
+
case "line":
|
|
1183
|
+
a.along === void 0 == (a.eigen === void 0) && e(`${a.id} needs exactly one of along or eigen`), s(a, "along", a.along), o(a, "eigen", a.eigen);
|
|
1184
|
+
break;
|
|
1185
|
+
case "label":
|
|
1186
|
+
s(a, "at", a.at), f(a, "tex", a.tex);
|
|
1187
|
+
break;
|
|
1188
|
+
case "equation":
|
|
1189
|
+
s(a, "at", a.at), (!Array.isArray(a.steps) || a.steps.length === 0) && e(`${a.id}.steps needs at least one TeX step`), a.steps.forEach((p, m) => f(a, `steps[${m}]`, p));
|
|
1190
|
+
break;
|
|
1191
|
+
}
|
|
1192
|
+
const y = { ...i, ...Se(t.items, n, i) };
|
|
1193
|
+
for (const [a, p, m] of c)
|
|
1194
|
+
try {
|
|
1195
|
+
ut(m, { values: y });
|
|
1196
|
+
} catch (b) {
|
|
1197
|
+
e(`${a.id}.${p}: ${b.message}`);
|
|
1198
|
+
}
|
|
1199
|
+
return t;
|
|
1200
|
+
}
|
|
1201
|
+
function Te(t) {
|
|
1202
|
+
const { width: e, height: n } = t, i = t.x ?? [-5, 5], r = e / (i[1] - i[0]), s = t.y ?? [-(n / r) / 2, n / r / 2], o = n / (s[1] - s[0]);
|
|
1203
|
+
return {
|
|
1204
|
+
width: e,
|
|
1205
|
+
height: n,
|
|
1206
|
+
x: i,
|
|
1207
|
+
y: s,
|
|
1208
|
+
unitX: r,
|
|
1209
|
+
unitY: o,
|
|
1210
|
+
scale: Math.min(e, n) / 600,
|
|
1211
|
+
toScreen: ([l, c]) => [(l - i[0]) * r, (s[1] - c) * o],
|
|
1212
|
+
toWorld: ([l, c]) => [i[0] + l / r, s[1] - c / o]
|
|
1213
|
+
};
|
|
1214
|
+
}
|
|
1215
|
+
const de = 'system-ui, -apple-system, "Segoe UI", sans-serif', he = (t, e) => Math.hypot(e[0] - t[0], e[1] - t[1]);
|
|
1216
|
+
function et(t, e) {
|
|
1217
|
+
if (e >= 1) return t;
|
|
1218
|
+
if (e <= 0) return [];
|
|
1219
|
+
const n = t.map((s) => s.slice(1).reduce((o, l, c) => o + he(s[c], l), 0));
|
|
1220
|
+
let i = n.reduce((s, o) => s + o, 0) * e;
|
|
1221
|
+
const r = [];
|
|
1222
|
+
for (const [s, o] of t.entries()) {
|
|
1223
|
+
if (i <= 0) break;
|
|
1224
|
+
if (n[s] <= i) {
|
|
1225
|
+
r.push(o), i -= n[s];
|
|
1226
|
+
continue;
|
|
1227
|
+
}
|
|
1228
|
+
const l = [o[0]];
|
|
1229
|
+
for (let c = 1; c < o.length; c++) {
|
|
1230
|
+
const f = he(o[c - 1], o[c]);
|
|
1231
|
+
if (f >= i) {
|
|
1232
|
+
const u = f === 0 ? 0 : i / f;
|
|
1233
|
+
l.push([o[c - 1][0] + (o[c][0] - o[c - 1][0]) * u, o[c - 1][1] + (o[c][1] - o[c - 1][1]) * u]);
|
|
1234
|
+
break;
|
|
1235
|
+
}
|
|
1236
|
+
l.push(o[c]), i -= f;
|
|
1237
|
+
}
|
|
1238
|
+
r.push(l), i = 0;
|
|
1239
|
+
}
|
|
1240
|
+
return r;
|
|
1241
|
+
}
|
|
1242
|
+
function Gt(t, e) {
|
|
1243
|
+
const n = e.align ?? "center", i = n === "left" ? 0 : n === "right" ? -t.width : -t.width / 2, r = e.baseline === "baseline" ? 0 : (t.height - t.depth) / 2;
|
|
1244
|
+
return [e.x + i * e.size, e.y + r * e.size];
|
|
1245
|
+
}
|
|
1246
|
+
function Oe(t, e, n) {
|
|
1247
|
+
const [i, r] = e, s = n.size, o = n.opacity ?? 1;
|
|
1248
|
+
return t.map((l) => {
|
|
1249
|
+
const c = l.color ?? n.color, f = o * (l.opacity ?? 1), u = `${n.idPrefix}${l.id}`;
|
|
1250
|
+
if (l.kind === "glyph")
|
|
1251
|
+
return {
|
|
1252
|
+
type: "text",
|
|
1253
|
+
id: u,
|
|
1254
|
+
text: l.text,
|
|
1255
|
+
x: i + (l.x + l.width / 2) * s,
|
|
1256
|
+
y: r + l.y * s,
|
|
1257
|
+
size: l.size * s,
|
|
1258
|
+
font: tn,
|
|
1259
|
+
italic: l.italic,
|
|
1260
|
+
color: c,
|
|
1261
|
+
align: "center",
|
|
1262
|
+
opacity: f
|
|
1263
|
+
};
|
|
1264
|
+
if (l.kind === "rule") {
|
|
1265
|
+
const h = i + l.x * s, d = r + l.y * s, y = l.width * s, a = Math.max(1, l.height * s);
|
|
1266
|
+
return { type: "line", id: u, points: [[h, d], [h + y, d], [h + y, d + a], [h, d + a]], closed: !0, fill: c, opacity: f };
|
|
1267
|
+
}
|
|
1268
|
+
return { type: "line", id: u, points: l.points.map(([h, d]) => [i + h * s, r + d * s]), stroke: c, width: Math.max(1, l.width * s), opacity: f };
|
|
1269
|
+
});
|
|
1270
|
+
}
|
|
1271
|
+
function vt(t, e) {
|
|
1272
|
+
return Oe(t.pieces, Gt(t, e), e);
|
|
1273
|
+
}
|
|
1274
|
+
function yn(t, e) {
|
|
1275
|
+
const n = Math.max(Math.abs(t.x[0]), Math.abs(t.x[1]), Math.abs(t.y[0]), Math.abs(t.y[1])), [i, r, s, o] = e, l = Math.hypot(i, r, s, o), c = l === 0 ? 0 : Math.abs(gt(e)) / l;
|
|
1276
|
+
return Math.min(n * 12, n / Math.max(c, 0.08)) + 1;
|
|
1277
|
+
}
|
|
1278
|
+
function fe(t, e, n) {
|
|
1279
|
+
const i = yn(t, e), r = Math.min(120, Math.ceil(i / n)), s = [];
|
|
1280
|
+
for (let o = -r; o <= r; o++) {
|
|
1281
|
+
const l = o * n;
|
|
1282
|
+
s.push({ line: [B(e, [l, -i]), B(e, [l, i])].map(t.toScreen), axis: o === 0 }), s.push({ line: [B(e, [-i, l]), B(e, [i, l])].map(t.toScreen), axis: o === 0 });
|
|
1283
|
+
}
|
|
1284
|
+
return s;
|
|
1285
|
+
}
|
|
1286
|
+
function gn(t, e) {
|
|
1287
|
+
const { plane: n, theme: i } = e, r = t.step ?? 1, s = [], o = 1.4 * n.scale;
|
|
1288
|
+
if (t.ghost)
|
|
1289
|
+
for (const [c, { line: f }] of fe(n, [1, 0, 0, 1], r).entries())
|
|
1290
|
+
s.push({ type: "line", id: `${t.id}.ghost.${c}`, points: f, stroke: i.ghost, width: o, opacity: e.opacity });
|
|
1291
|
+
const l = F(i, t.color, i.grid);
|
|
1292
|
+
for (const [c, { line: f, axis: u }] of fe(n, st(e.values, t.transform), r).entries()) {
|
|
1293
|
+
const [h] = et([f], e.draw);
|
|
1294
|
+
h && s.push({ type: "line", id: `${t.id}.${c}`, points: h, stroke: u ? i.ink : l, width: u ? o * 1.6 : o, opacity: e.opacity });
|
|
1295
|
+
}
|
|
1296
|
+
return s;
|
|
1297
|
+
}
|
|
1298
|
+
function bn(t, e) {
|
|
1299
|
+
const { plane: n, theme: i } = e, r = F(i, t.color, i.axes), s = 2 * n.scale, o = e.opacity, l = et([[n.toScreen([n.x[0], 0]), n.toScreen([n.x[1], 0])]], e.draw), c = et([[n.toScreen([0, n.y[0]]), n.toScreen([0, n.y[1]])]], e.draw), f = [...l, ...c].map((p, m) => ({ type: "line", id: `${t.id}.${m ? "y" : "x"}`, points: p, stroke: r, width: s, opacity: o })), u = t.ticks ?? 1;
|
|
1300
|
+
if (u <= 0 || e.draw < 1) return f;
|
|
1301
|
+
const h = 5 * n.scale, d = 13 * n.scale, y = (p, m) => {
|
|
1302
|
+
const b = [];
|
|
1303
|
+
for (let w = Math.ceil(p / u) * u; w <= m + 1e-9; w += u) Math.abs(w) > 1e-9 && b.push(Number(w.toFixed(6)));
|
|
1304
|
+
return b;
|
|
1305
|
+
}, a = (p) => String(p).replace("-", "−");
|
|
1306
|
+
for (const p of y(n.x[0], n.x[1])) {
|
|
1307
|
+
const [m, b] = n.toScreen([p, 0]);
|
|
1308
|
+
f.push({ type: "line", id: `${t.id}.tick.x${p}`, points: [[m, b - h], [m, b + h]], stroke: r, width: s * 0.8, opacity: o }), t.labels !== !1 && f.push({ type: "text", id: `${t.id}.label.x${p}`, text: a(p), x: m, y: b + h + d * 1.1, size: d, font: de, color: r, align: "center", opacity: o * 0.8 });
|
|
1309
|
+
}
|
|
1310
|
+
for (const p of y(n.y[0], n.y[1])) {
|
|
1311
|
+
const [m, b] = n.toScreen([0, p]);
|
|
1312
|
+
f.push({ type: "line", id: `${t.id}.tick.y${p}`, points: [[m - h, b], [m + h, b]], stroke: r, width: s * 0.8, opacity: o }), t.labels !== !1 && f.push({ type: "text", id: `${t.id}.label.y${p}`, text: a(p), x: m - h - d * 0.4, y: b + d * 0.35, size: d, font: de, color: r, align: "right", opacity: o * 0.8 });
|
|
1313
|
+
}
|
|
1314
|
+
return f;
|
|
1315
|
+
}
|
|
1316
|
+
function Tt(t, e, n, i, r, s) {
|
|
1317
|
+
const o = 26 * s.plane.scale, [l, c] = we(n), f = ut(t, { values: s.scope, style: "inline" }), u = o * (0.55 + Math.abs(l) * f.width * 0.5);
|
|
1318
|
+
return vt(f, { x: e[0] + l * u, y: e[1] + c * u, size: o, color: i, opacity: s.opacity, idPrefix: `${r}.label.` });
|
|
1319
|
+
}
|
|
1320
|
+
function Le(t, e, n, i, r, s) {
|
|
1321
|
+
const o = s.plane.toScreen(e), l = s.plane.toScreen(n), [c] = et([[o, l]], s.draw);
|
|
1322
|
+
if (!c) return [];
|
|
1323
|
+
const f = Math.hypot(l[0] - o[0], l[1] - o[1]), u = Math.min(r * 3.6, f * 0.45);
|
|
1324
|
+
return [{ type: "arrow", id: t, points: c, stroke: i, width: r, head: u, opacity: s.opacity }];
|
|
1325
|
+
}
|
|
1326
|
+
function vn(t, e) {
|
|
1327
|
+
const n = st(e.values, t.transform), i = B(n, t.from === void 0 ? [0, 0] : pt(e.values, t.from)), r = B(n, pt(e.values, t.to)), s = F(e.theme, t.color, e.theme.colors.yellow), o = Le(t.id, i, r, s, (t.width ?? 4) * e.plane.scale, e);
|
|
1328
|
+
if (t.label && e.draw >= 1) {
|
|
1329
|
+
const l = e.plane.toScreen(r), c = e.plane.toScreen(i), f = Xt(l, c);
|
|
1330
|
+
if (t.labelAt === "middle") {
|
|
1331
|
+
const u = [(l[0] + c[0]) / 2, (l[1] + c[1]) / 2];
|
|
1332
|
+
o.push(...Tt(t.label, u, [f[1], -f[0]], s, t.id, e));
|
|
1333
|
+
} else
|
|
1334
|
+
o.push(...Tt(t.label, l, f, s, t.id, e));
|
|
1335
|
+
}
|
|
1336
|
+
return o;
|
|
1337
|
+
}
|
|
1338
|
+
function wn(t, e) {
|
|
1339
|
+
const n = st(e.values, t.transform), i = 4.5 * e.plane.scale, r = e.plane.toScreen([0, 0]), s = [], o = [
|
|
1340
|
+
["i", [1, 0], e.theme.colors.green, "\\hat{i}"],
|
|
1341
|
+
["j", [0, 1], e.theme.colors.red, "\\hat{j}"]
|
|
1342
|
+
];
|
|
1343
|
+
for (const [l, c, f, u] of o) {
|
|
1344
|
+
const h = B(n, c);
|
|
1345
|
+
if (s.push(...Le(`${t.id}.${l}`, [0, 0], h, f, i, e)), t.labels !== !1 && e.draw >= 1) {
|
|
1346
|
+
const d = e.plane.toScreen(h);
|
|
1347
|
+
s.push(...Tt(u, d, Xt(d, r), f, `${t.id}.${l}`, e));
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
return s;
|
|
1351
|
+
}
|
|
1352
|
+
function kn(t, e) {
|
|
1353
|
+
const n = t.on !== void 0 ? [e.scope[`${t.id}.x`], e.scope[`${t.id}.y`]] : pt(e.values, t.at);
|
|
1354
|
+
if (!Number.isFinite(n[0]) || !Number.isFinite(n[1])) return [];
|
|
1355
|
+
const i = B(st(e.values, t.transform), n), [r, s] = e.plane.toScreen(i), o = F(e.theme, t.color, e.theme.colors.yellow), l = (t.radius ?? 6) * e.plane.scale * Math.min(1, e.draw * 2), c = t.hollow ? { type: "circle", id: t.id, x: r, y: s, r: l, fill: e.theme.background, stroke: o, width: 2.5 * e.plane.scale, opacity: e.opacity } : { type: "circle", id: t.id, x: r, y: s, r: l, fill: o, opacity: e.opacity }, f = l > 0 ? [c] : [];
|
|
1356
|
+
return t.label && e.draw >= 1 && f.push(...Tt(t.label, [r, s], [0.7, -0.7], o, t.id, e)), f;
|
|
1357
|
+
}
|
|
1358
|
+
const Ot = 480;
|
|
1359
|
+
function Ne(t, e) {
|
|
1360
|
+
const n = [];
|
|
1361
|
+
let i = [];
|
|
1362
|
+
return t.forEach((r, s) => {
|
|
1363
|
+
const o = t[s - 1];
|
|
1364
|
+
(!r || o && Math.abs(r[1] - o[1]) > e * 1.5) && (i.length >= 2 && n.push(i), i = []), r && i.push(r);
|
|
1365
|
+
}), i.length >= 2 && n.push(i), n;
|
|
1366
|
+
}
|
|
1367
|
+
function Fe(t, e) {
|
|
1368
|
+
const n = Math.max(e.plane.width, e.plane.height) * 20;
|
|
1369
|
+
if (!(!Number.isFinite(t[0]) || !Number.isFinite(t[1])))
|
|
1370
|
+
return [Math.max(-n, Math.min(n, t[0])), Math.max(-n, Math.min(n, t[1]))];
|
|
1371
|
+
}
|
|
1372
|
+
function Ie(t, e, n, i, r, s) {
|
|
1373
|
+
return et(e, r.draw).map((o, l) => ({ type: "line", id: `${t}.${l}`, points: o, stroke: n, width: i, opacity: r.opacity, ...s ? { dash: s } : {} }));
|
|
1374
|
+
}
|
|
1375
|
+
function Ce(t, e, n, i, r, s, o, l) {
|
|
1376
|
+
const c = Array.from({ length: Ot + 1 }, (f, u) => {
|
|
1377
|
+
const h = n + (i - n) * u / Ot;
|
|
1378
|
+
return Fe(o.plane.toScreen([h, e(h)]), o);
|
|
1379
|
+
});
|
|
1380
|
+
return Ie(t, Ne(c, o.plane.height), r, s, o, l);
|
|
1381
|
+
}
|
|
1382
|
+
function $n(t, e) {
|
|
1383
|
+
const n = t.derivative ? Yt(t.expr, t.derivative) : D(t.expr, "x"), [i, r] = t.domain ?? e.plane.x, s = F(e.theme, t.color, e.theme.colors.blue), o = t.dashed ? [10 * e.plane.scale, 8 * e.plane.scale] : void 0;
|
|
1384
|
+
return Ce(t.id, (l) => n(l, e.scope), i, r, s, (t.width ?? 3.5) * e.plane.scale, e, o);
|
|
1385
|
+
}
|
|
1386
|
+
function xn(t, e) {
|
|
1387
|
+
const n = D(t.x, "t"), i = D(t.y, "t"), r = st(e.values, t.transform), [s, o] = t.t ?? [0, Math.PI * 2], l = Array.from({ length: Ot + 1 }, (f, u) => {
|
|
1388
|
+
const h = s + (o - s) * u / Ot;
|
|
1389
|
+
return Fe(e.plane.toScreen(B(r, [n(h, e.scope), i(h, e.scope)])), e);
|
|
1390
|
+
}), c = F(e.theme, t.color, e.theme.colors.blue);
|
|
1391
|
+
return Ie(t.id, Ne(l, e.plane.height), c, (t.width ?? 3.5) * e.plane.scale, e);
|
|
1392
|
+
}
|
|
1393
|
+
const Mn = [[0, 0], [1, 0], [1, 1], [0, 1]];
|
|
1394
|
+
function Sn(t, e) {
|
|
1395
|
+
const n = st(e.values, t.transform), i = (t.points ?? Mn).map((l) => B(n, l)), r = i.map(e.plane.toScreen), s = F(e.theme, t.color, e.theme.colors.yellow), o = [
|
|
1396
|
+
{ type: "line", id: `${t.id}.fill`, points: r, closed: !0, fill: s, opacity: e.opacity * (t.fillOpacity ?? 0.3) * e.draw },
|
|
1397
|
+
...et([[...r, r[0]]], e.draw).map((l) => ({ type: "line", id: `${t.id}.outline`, points: l, stroke: s, width: 2.5 * e.plane.scale, opacity: e.opacity }))
|
|
1398
|
+
];
|
|
1399
|
+
if (t.label && e.draw >= 1) {
|
|
1400
|
+
const l = i.reduce((u, h) => [u[0] + h[0] / i.length, u[1] + h[1] / i.length], [0, 0]), [c, f] = e.plane.toScreen(l);
|
|
1401
|
+
o.push(...vt(ut(t.label, { values: e.scope, style: "inline" }), { x: c, y: f, size: 24 * e.plane.scale, color: e.theme.ink, opacity: e.opacity, idPrefix: `${t.id}.label.` }));
|
|
1402
|
+
}
|
|
1403
|
+
return o;
|
|
1404
|
+
}
|
|
1405
|
+
function En(t, e) {
|
|
1406
|
+
const n = t.eigen ? ke(st(e.values, t.eigen)).map((o) => o.vector) : t.along !== void 0 ? [pt(e.values, t.along)] : [], i = Math.max(Math.abs(e.plane.x[0]), Math.abs(e.plane.x[1]), Math.abs(e.plane.y[0]), Math.abs(e.plane.y[1])) * 1.5, r = F(e.theme, t.color, e.theme.colors.yellow), s = (t.width ?? 2.5) * e.plane.scale;
|
|
1407
|
+
return n.flatMap((o, l) => {
|
|
1408
|
+
const c = Math.hypot(o[0], o[1]);
|
|
1409
|
+
if (c === 0) return [];
|
|
1410
|
+
const f = qt(o, i / c), u = [qt(f, -1), f].map(e.plane.toScreen);
|
|
1411
|
+
return et([u], e.draw).map((h) => ({
|
|
1412
|
+
type: "line",
|
|
1413
|
+
id: `${t.id}.${l}`,
|
|
1414
|
+
points: h,
|
|
1415
|
+
stroke: r,
|
|
1416
|
+
width: s,
|
|
1417
|
+
opacity: e.opacity,
|
|
1418
|
+
...t.dashed ? { dash: [10 * e.plane.scale, 8 * e.plane.scale] } : {}
|
|
1419
|
+
}));
|
|
1420
|
+
});
|
|
1421
|
+
}
|
|
1422
|
+
function Ht(t, e, n, i) {
|
|
1423
|
+
const r = n.size * 0.35;
|
|
1424
|
+
let s = 1 / 0, o = 1 / 0, l = -1 / 0, c = -1 / 0;
|
|
1425
|
+
for (const f of e) {
|
|
1426
|
+
const [u, h] = Gt(f, n);
|
|
1427
|
+
s = Math.min(s, u - r), l = Math.max(l, u + f.width * n.size + r), o = Math.min(o, h - f.height * n.size - r), c = Math.max(c, h + f.depth * n.size + r);
|
|
1428
|
+
}
|
|
1429
|
+
return [{
|
|
1430
|
+
type: "line",
|
|
1431
|
+
id: `${t}.backdrop`,
|
|
1432
|
+
points: [[s, o], [l, o], [l, c], [s, c]],
|
|
1433
|
+
closed: !0,
|
|
1434
|
+
fill: i.theme.background,
|
|
1435
|
+
opacity: (n.opacity ?? 1) * 0.85
|
|
1436
|
+
}];
|
|
1437
|
+
}
|
|
1438
|
+
function Pe(t, e, n) {
|
|
1439
|
+
const i = pt(n.values, t);
|
|
1440
|
+
return e ? i : n.plane.toScreen(i);
|
|
1441
|
+
}
|
|
1442
|
+
function An(t, e) {
|
|
1443
|
+
const [n, i] = Pe(t.at, t.screen, e), r = (t.size ?? 28) * e.plane.scale, s = F(e.theme, t.color, e.theme.ink), o = { x: n, y: i, size: r, color: s, align: t.align, opacity: e.opacity * e.draw, idPrefix: `${t.id}.` }, l = ut(t.tex, { values: e.scope });
|
|
1444
|
+
return [...t.backdrop ?? t.screen ?? !1 ? Ht(t.id, [l], o, e) : [], ...vt(l, o)];
|
|
1445
|
+
}
|
|
1446
|
+
function Tn(t, e) {
|
|
1447
|
+
const [n, i] = Pe(t.at, t.screen, e), r = (t.size ?? 36) * e.plane.scale, s = F(e.theme, t.color, e.theme.ink), o = t.steps.length - 1, l = Math.min(o, Math.max(0, e.prop("step") ?? t.step ?? 0)), c = Math.floor(l), f = l - c, u = { x: n, y: i, size: r, color: s, align: t.align, opacity: e.opacity * e.draw, idPrefix: `${t.id}.` }, h = (m) => ut(t.steps[m], { values: e.scope }), d = t.backdrop ?? t.screen ?? !1;
|
|
1448
|
+
if (f === 0 || c >= o) {
|
|
1449
|
+
const m = h(c);
|
|
1450
|
+
return [...d ? Ht(t.id, [m], u, e) : [], ...vt(m, u)];
|
|
1451
|
+
}
|
|
1452
|
+
const y = (m) => {
|
|
1453
|
+
const [b, w] = Gt(m, u);
|
|
1454
|
+
return { layout: m, x: b / r, y: w / r };
|
|
1455
|
+
}, a = h(c), p = h(c + 1);
|
|
1456
|
+
return [
|
|
1457
|
+
...d ? Ht(t.id, [a, p], u, e) : [],
|
|
1458
|
+
...Oe(pn(y(a), y(p), f), [0, 0], u)
|
|
1459
|
+
];
|
|
1460
|
+
}
|
|
1461
|
+
function On(t, e) {
|
|
1462
|
+
const n = D(t.expr), i = N(e.values, t.at), r = t.h === void 0 ? 0 : N(e.values, t.h), s = n(i, e.scope), o = e.scope[`${t.id}.slope`];
|
|
1463
|
+
if (!Number.isFinite(s) || !Number.isFinite(o)) return [];
|
|
1464
|
+
const l = t.length !== void 0 ? t.length / 2 : Math.max(Math.abs(e.plane.x[0] - i), Math.abs(e.plane.x[1] - i)), c = [[i - l, s - o * l], [i + l, s + o * l]], f = F(e.theme, t.color, e.theme.colors.yellow), u = e.plane.toScreen([i, s]), h = c.map(e.plane.toScreen), d = [h[0], h[1]].flatMap(
|
|
1465
|
+
(y, a) => et([[u, y]], e.draw).map((p) => ({ type: "line", id: `${t.id}.${a}`, points: p, stroke: f, width: (t.width ?? 3) * e.plane.scale, opacity: e.opacity }))
|
|
1466
|
+
);
|
|
1467
|
+
if (t.points !== !1) {
|
|
1468
|
+
const y = 6 * e.plane.scale;
|
|
1469
|
+
if (d.push({ type: "circle", id: `${t.id}.point`, x: u[0], y: u[1], r: y, fill: f, opacity: e.opacity }), Math.abs(r) > 1e-9) {
|
|
1470
|
+
const [a, p] = e.plane.toScreen([i + r, n(i + r, e.scope)]);
|
|
1471
|
+
Number.isFinite(p) && d.push({ type: "circle", id: `${t.id}.secant`, x: a, y: p, r: y, fill: f, opacity: e.opacity });
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
if (t.label && e.draw >= 1) {
|
|
1475
|
+
const y = 24 * e.plane.scale, a = ut(t.label, { values: e.scope, style: "inline" });
|
|
1476
|
+
d.push(...vt(a, { x: u[0], y: u[1] - y * 1.6, size: y, color: f, opacity: e.opacity, idPrefix: `${t.id}.label.` }));
|
|
1477
|
+
}
|
|
1478
|
+
return d;
|
|
1479
|
+
}
|
|
1480
|
+
function Ln(t, e) {
|
|
1481
|
+
const n = D(t.expr), i = N(e.values, t.from), r = N(e.values, t.to), s = Me(N(e.values, t.n)), o = xe((u) => n(u, e.scope), i, r, s, t.rule ?? "left"), l = F(e.theme, t.color, e.theme.colors.blue), c = Math.ceil(o.length * e.draw), f = Math.max(0.5, Math.min(1.5, 60 / s)) * e.plane.scale;
|
|
1482
|
+
return o.slice(0, c).flatMap(([u, h, d, y], a) => {
|
|
1483
|
+
if (!Number.isFinite(d) || !Number.isFinite(y)) return [];
|
|
1484
|
+
const p = [[u, 0], [h, 0], [h, y], [u, d]].map(e.plane.toScreen), b = (d + y) / 2 < 0 ? F(e.theme, "red", e.theme.colors.red) : l;
|
|
1485
|
+
return [{ type: "line", id: `${t.id}.${a}`, points: p, closed: !0, fill: b, stroke: b, width: f, opacity: e.opacity * (t.fillOpacity ?? 0.35) }];
|
|
1486
|
+
});
|
|
1487
|
+
}
|
|
1488
|
+
function Nn(t, e) {
|
|
1489
|
+
const n = D(t.expr), i = t.lower ? D(t.lower) : () => 0, r = N(e.values, t.from), s = r + (N(e.values, t.to) - r) * e.draw;
|
|
1490
|
+
if (r === s) return [];
|
|
1491
|
+
const o = 240, l = F(e.theme, t.color, e.theme.colors.blue), c = F(e.theme, t.negativeColor, e.theme.colors.red), f = e.opacity * (t.fillOpacity ?? 0.4), u = [];
|
|
1492
|
+
let h = [], d = [], y;
|
|
1493
|
+
const a = () => {
|
|
1494
|
+
h.length >= 2 && u.push({ type: "line", id: `${t.id}.${u.length}`, points: [...h, ...d.reverse()].map(e.plane.toScreen), closed: !0, fill: y ? l : c, opacity: f }), h = [], d = [];
|
|
1495
|
+
};
|
|
1496
|
+
let p;
|
|
1497
|
+
for (let m = 0; m <= o; m++) {
|
|
1498
|
+
const b = r + (s - r) * m / o, w = n(b, e.scope), $ = i(b, e.scope);
|
|
1499
|
+
if (!Number.isFinite(w) || !Number.isFinite($)) {
|
|
1500
|
+
a(), p = void 0;
|
|
1501
|
+
continue;
|
|
1502
|
+
}
|
|
1503
|
+
const g = w - $ >= 0;
|
|
1504
|
+
if (p && y !== void 0 && g !== y) {
|
|
1505
|
+
const k = p.fy - p.gy, v = w - $, x = k / (k - v), M = p.x + (b - p.x) * x, T = p.gy + ($ - p.gy) * x;
|
|
1506
|
+
h.push([M, T]), d.push([M, T]), a(), h.push([M, T]), d.push([M, T]);
|
|
1507
|
+
}
|
|
1508
|
+
y = g, h.push([b, w]), d.push([b, $]), p = { x: b, fy: w, gy: $ };
|
|
1509
|
+
}
|
|
1510
|
+
return a(), u;
|
|
1511
|
+
}
|
|
1512
|
+
const Fn = Array.from({ length: 22 }, (t, e) => e).map((t) => t === 0 ? 1 : Array.from({ length: t }, (e, n) => n + 1).reduce((e, n) => e * n, 1));
|
|
1513
|
+
function In(t, e) {
|
|
1514
|
+
const n = N(e.values, t.at), i = e.scope[`${t.id}.order`], r = Math.floor(i), s = i - r, o = Array.from({ length: Math.min(20, r + 1) + 1 }, (h, d) => Yt(t.expr, d)(n, e.scope) / Fn[d]), l = (h) => {
|
|
1515
|
+
let d = 0, y = 1;
|
|
1516
|
+
for (let a = 0; a <= r; a++)
|
|
1517
|
+
d += o[a] * y, y *= h - n;
|
|
1518
|
+
return s > 0 && r < 20 ? d + s * o[r + 1] * y : d;
|
|
1519
|
+
}, c = F(e.theme, t.color, e.theme.colors.yellow), [f, u] = t.domain ?? e.plane.x;
|
|
1520
|
+
return Ce(t.id, (h) => l(h), f, u, c, (t.width ?? 3.5) * e.plane.scale, e);
|
|
1521
|
+
}
|
|
1522
|
+
function Cn(t, e) {
|
|
1523
|
+
switch (t.kind) {
|
|
1524
|
+
case "grid":
|
|
1525
|
+
return gn(t, e);
|
|
1526
|
+
case "axes":
|
|
1527
|
+
return bn(t, e);
|
|
1528
|
+
case "vector":
|
|
1529
|
+
return vn(t, e);
|
|
1530
|
+
case "basis":
|
|
1531
|
+
return wn(t, e);
|
|
1532
|
+
case "point":
|
|
1533
|
+
return kn(t, e);
|
|
1534
|
+
case "function":
|
|
1535
|
+
return $n(t, e);
|
|
1536
|
+
case "parametric":
|
|
1537
|
+
return xn(t, e);
|
|
1538
|
+
case "polygon":
|
|
1539
|
+
return Sn(t, e);
|
|
1540
|
+
case "line":
|
|
1541
|
+
return En(t, e);
|
|
1542
|
+
case "label":
|
|
1543
|
+
return An(t, e);
|
|
1544
|
+
case "equation":
|
|
1545
|
+
return Tn(t, e);
|
|
1546
|
+
case "tangent":
|
|
1547
|
+
return On(t, e);
|
|
1548
|
+
case "riemann":
|
|
1549
|
+
return Ln(t, e);
|
|
1550
|
+
case "area":
|
|
1551
|
+
return Nn(t, e);
|
|
1552
|
+
case "taylor":
|
|
1553
|
+
return In(t, e);
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
const pe = (t) => Math.min(1, Math.max(0, t));
|
|
1557
|
+
function Lt(t, e = {}) {
|
|
1558
|
+
const n = Ut[t.theme ?? "dark"], i = Te(t), r = { ...t.values }, s = /* @__PURE__ */ new Map();
|
|
1559
|
+
for (const [u, h] of Object.entries(e))
|
|
1560
|
+
u.indexOf(".") < 0 ? typeof h != "string" && (r[u] = h) : typeof h == "number" && s.set(u, h);
|
|
1561
|
+
const o = $e(r), l = { ...o, ...Se(t.items, r, o) }, c = [];
|
|
1562
|
+
for (const u of t.items) {
|
|
1563
|
+
const h = (a) => s.get(`${u.id}.${a}`), d = pe(h("draw") ?? u.draw ?? 1), y = pe(h("opacity") ?? u.opacity ?? 1);
|
|
1564
|
+
d <= 0 || y <= 0 || c.push(...Cn(u, { plane: i, theme: n, values: r, scope: l, draw: d, opacity: y, prop: h }));
|
|
1565
|
+
}
|
|
1566
|
+
const f = t.background === "none" ? void 0 : t.background ?? n.background;
|
|
1567
|
+
return { width: t.width, height: t.height, ...f ? { background: f } : {}, shapes: c };
|
|
1568
|
+
}
|
|
1569
|
+
function qe(t, e) {
|
|
1570
|
+
if (t.length < 2) return { shaft: t, head: void 0 };
|
|
1571
|
+
const n = t[t.length - 1], i = t[t.length - 2], r = n[0] - i[0], s = n[1] - i[1], o = Math.hypot(r, s);
|
|
1572
|
+
if (o === 0 || e <= 0) return { shaft: t, head: void 0 };
|
|
1573
|
+
const l = r / o, c = s / o, f = Math.min(e, o), u = [n[0] - l * f, n[1] - c * f], h = f * 0.55;
|
|
1574
|
+
return {
|
|
1575
|
+
shaft: [...t.slice(0, -1), u],
|
|
1576
|
+
head: [n, [u[0] - c * h, u[1] + l * h], [u[0] + c * h, u[1] - l * h]]
|
|
1577
|
+
};
|
|
1578
|
+
}
|
|
1579
|
+
function It(t, e, n = !1) {
|
|
1580
|
+
t.beginPath(), e.forEach(([i, r], s) => s === 0 ? t.moveTo(i, r) : t.lineTo(i, r)), n && t.closePath();
|
|
1581
|
+
}
|
|
1582
|
+
function Pn(t, e) {
|
|
1583
|
+
switch (t.globalAlpha = e.opacity ?? 1, e.type) {
|
|
1584
|
+
case "line":
|
|
1585
|
+
if (e.points.length < 2) return;
|
|
1586
|
+
It(t, e.points, e.closed), e.fill && (t.fillStyle = e.fill, t.fill()), e.stroke && (e.width ?? 1) > 0 && (t.strokeStyle = e.stroke, t.lineWidth = e.width ?? 1, t.setLineDash(e.dash ?? []), t.stroke(), t.setLineDash([]));
|
|
1587
|
+
return;
|
|
1588
|
+
case "arrow": {
|
|
1589
|
+
const { shaft: n, head: i } = qe(e.points, e.head);
|
|
1590
|
+
t.strokeStyle = e.stroke, t.fillStyle = e.stroke, t.lineWidth = e.width, n.length >= 2 && (It(t, n), t.stroke()), i && (It(t, i, !0), t.fill());
|
|
1591
|
+
return;
|
|
1592
|
+
}
|
|
1593
|
+
case "circle":
|
|
1594
|
+
t.beginPath(), t.arc(e.x, e.y, e.r, 0, Math.PI * 2), e.fill && (t.fillStyle = e.fill, t.fill()), e.stroke && (t.strokeStyle = e.stroke, t.lineWidth = e.width ?? 1, t.stroke());
|
|
1595
|
+
return;
|
|
1596
|
+
case "text":
|
|
1597
|
+
t.font = `${e.italic ? "italic " : ""}${e.size}px ${e.font}`, t.textAlign = e.align, t.textBaseline = "alphabetic", t.fillStyle = e.color, t.fillText(e.text, e.x, e.y);
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
function qn(t, e) {
|
|
1601
|
+
t.save(), t.beginPath(), t.rect(0, 0, e.width, e.height), t.clip(), e.background && (t.fillStyle = e.background, t.fillRect(0, 0, e.width, e.height)), t.lineCap = "round", t.lineJoin = "round";
|
|
1602
|
+
for (const n of e.shapes) Pn(t, n);
|
|
1603
|
+
t.restore();
|
|
1604
|
+
}
|
|
1605
|
+
const L = (t) => String(Math.round(t * 100) / 100), W = (t) => t.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """), Ct = (t, e = !1) => t.map(([n, i], r) => `${r ? "L" : "M"}${L(n)} ${L(i)}`).join("") + (e ? "Z" : ""), xt = (t) => t.opacity !== void 0 && t.opacity < 1 ? ` opacity="${L(t.opacity)}"` : "";
|
|
1606
|
+
function zn(t) {
|
|
1607
|
+
switch (t.type) {
|
|
1608
|
+
case "line": {
|
|
1609
|
+
if (t.points.length < 2) return "";
|
|
1610
|
+
const e = t.fill ? ` fill="${W(t.fill)}"` : ' fill="none"', n = t.stroke && (t.width ?? 1) > 0 ? ` stroke="${W(t.stroke)}" stroke-width="${L(t.width ?? 1)}"` : "", i = t.dash?.length ? ` stroke-dasharray="${t.dash.map(L).join(" ")}"` : "";
|
|
1611
|
+
return `<path d="${Ct(t.points, t.closed)}"${e}${n}${i}${xt(t)}/>`;
|
|
1612
|
+
}
|
|
1613
|
+
case "arrow": {
|
|
1614
|
+
const { shaft: e, head: n } = qe(t.points, t.head), i = W(t.stroke), r = e.length >= 2 ? `<path d="${Ct(e)}" fill="none" stroke="${i}" stroke-width="${L(t.width)}"/>` : "", s = n ? `<path d="${Ct(n, !0)}" fill="${i}"/>` : "";
|
|
1615
|
+
return `<g${xt(t)}>${r}${s}</g>`;
|
|
1616
|
+
}
|
|
1617
|
+
case "circle": {
|
|
1618
|
+
const e = t.fill ? ` fill="${W(t.fill)}"` : ' fill="none"', n = t.stroke ? ` stroke="${W(t.stroke)}" stroke-width="${L(t.width ?? 1)}"` : "";
|
|
1619
|
+
return `<circle cx="${L(t.x)}" cy="${L(t.y)}" r="${L(t.r)}"${e}${n}${xt(t)}/>`;
|
|
1620
|
+
}
|
|
1621
|
+
case "text": {
|
|
1622
|
+
const e = t.align === "center" ? "middle" : t.align === "right" ? "end" : "start", n = t.italic ? ' font-style="italic"' : "";
|
|
1623
|
+
return `<text x="${L(t.x)}" y="${L(t.y)}" font-size="${L(t.size)}" font-family="${W(t.font)}"${n} text-anchor="${e}" fill="${W(t.color)}"${xt(t)}>${W(t.text)}</text>`;
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
function Kt(t) {
|
|
1628
|
+
return `${t.background ? `<rect width="${L(t.width)}" height="${L(t.height)}" fill="${W(t.background)}"/>` : ""}<g stroke-linecap="round" stroke-linejoin="round">${t.shapes.map(zn).join("")}</g>`;
|
|
1629
|
+
}
|
|
1630
|
+
function jn(t, e = {}) {
|
|
1631
|
+
const n = e.title ? `<title>${W(e.title)}</title>` : "", i = e.title ? ` role="img" aria-label="${W(e.title)}"` : "";
|
|
1632
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${L(t.width)} ${L(t.height)}" width="${L(t.width)}" height="${L(t.height)}"${i}>${n}${Kt(t)}</svg>`;
|
|
1633
|
+
}
|
|
1634
|
+
function Dn(t) {
|
|
1635
|
+
const e = {};
|
|
1636
|
+
for (const [n, i] of Object.entries(t.values ?? {})) {
|
|
1637
|
+
const r = typeof i == "number" ? "number" : i.length === 2 ? "[x, y] vector" : "[a, b, c, d] matrix";
|
|
1638
|
+
e[n] = { description: `The value ${n} (a ${r})`, kind: typeof i == "number" ? "number" : "list" };
|
|
1639
|
+
}
|
|
1640
|
+
for (const n of t.items)
|
|
1641
|
+
e[`${n.id}.draw`] = { description: `How much of the ${n.kind} "${n.id}" is drawn`, unit: "0..1", min: 0, max: 1 }, e[`${n.id}.opacity`] = { description: `Opacity of the ${n.kind} "${n.id}"`, unit: "0..1", min: 0, max: 1 }, n.kind === "equation" && (e[`${n.id}.step`] = { description: `Which step of "${n.id}" shows; in between, terms glide`, unit: "step", min: 0, max: n.steps.length - 1 });
|
|
1642
|
+
return e;
|
|
1643
|
+
}
|
|
1644
|
+
function pi(t, e = {}) {
|
|
1645
|
+
bt(t);
|
|
1646
|
+
const n = Dn(t), i = { ...t.values ?? {} };
|
|
1647
|
+
return {
|
|
1648
|
+
type: "custom",
|
|
1649
|
+
x: e.x ?? 0,
|
|
1650
|
+
y: e.y ?? 0,
|
|
1651
|
+
width: t.width,
|
|
1652
|
+
height: t.height,
|
|
1653
|
+
props: i,
|
|
1654
|
+
acceptsProp: (r) => Object.prototype.hasOwnProperty.call(n, r),
|
|
1655
|
+
draw(r, s) {
|
|
1656
|
+
qn(r, Lt(t, s.props ?? {}));
|
|
1657
|
+
},
|
|
1658
|
+
about: {
|
|
1659
|
+
kind: "math figure",
|
|
1660
|
+
summary: `A ${t.width}×${t.height} maths figure with ${t.items.map((r) => `${r.kind} "${r.id}"`).join(", ")}. Animate its values by name and each item's draw / opacity (an equation's step).`,
|
|
1661
|
+
props: n
|
|
1662
|
+
}
|
|
1663
|
+
};
|
|
1664
|
+
}
|
|
1665
|
+
function mi(t, e, n, i = {}) {
|
|
1666
|
+
const r = new Set(n.items.map((c) => c.id)), s = (c) => {
|
|
1667
|
+
if (!r.has(c)) throw new Error(`mathSteps: no item "${c}" (items: ${[...r].join(", ")})`);
|
|
1668
|
+
}, o = { ...n.values ?? {} };
|
|
1669
|
+
for (const c of n.items)
|
|
1670
|
+
o[`${c.id}.draw`] = c.draw ?? 1, o[`${c.id}.opacity`] = c.opacity ?? 1, c.kind === "equation" && (o[`${c.id}.step`] = c.step ?? 0);
|
|
1671
|
+
for (const c of i.hidden ?? [])
|
|
1672
|
+
s(c), o[`${c}.draw`] = 0;
|
|
1673
|
+
t.initial(e, o);
|
|
1674
|
+
const l = {
|
|
1675
|
+
toValue(c, f, u) {
|
|
1676
|
+
if (!(c in o)) throw new Error(`mathSteps: no value "${c}" (values: ${Object.keys(n.values ?? {}).join(", ")})`);
|
|
1677
|
+
return t.to(e, { [c]: f }, { duration: 1200, ...u }), l;
|
|
1678
|
+
},
|
|
1679
|
+
applyMatrix(c, f = {}) {
|
|
1680
|
+
const { name: u = "M", ...h } = f;
|
|
1681
|
+
if (c.length !== 4 || !c.every(Number.isFinite)) throw new Error("mathSteps.applyMatrix: expected [a, b, c, d]");
|
|
1682
|
+
return l.toValue(u, c, { duration: 1600, ...h });
|
|
1683
|
+
},
|
|
1684
|
+
drawOn(c, f) {
|
|
1685
|
+
return s(c), t.to(e, { [`${c}.draw`]: 1 }, { duration: 900, ...f }), l;
|
|
1686
|
+
},
|
|
1687
|
+
fadeIn(c, f) {
|
|
1688
|
+
return s(c), t.set(e, { [`${c}.draw`]: 1, [`${c}.opacity`]: 0 }), t.to(e, { [`${c}.opacity`]: 1 }, { duration: 500, ...f }), l;
|
|
1689
|
+
},
|
|
1690
|
+
fadeOut(c, f) {
|
|
1691
|
+
return s(c), t.to(e, { [`${c}.opacity`]: 0 }, { duration: 500, ...f }), l;
|
|
1692
|
+
},
|
|
1693
|
+
showStep(c, f, u) {
|
|
1694
|
+
return s(c), t.to(e, { [`${c}.step`]: f }, { duration: 900, ...u }), l;
|
|
1695
|
+
}
|
|
1696
|
+
};
|
|
1697
|
+
return l;
|
|
1698
|
+
}
|
|
1699
|
+
class me {
|
|
1700
|
+
media;
|
|
1701
|
+
offset;
|
|
1702
|
+
driftTolerance;
|
|
1703
|
+
constructor(e, n = {}) {
|
|
1704
|
+
this.media = e, this.offset = n.offset ?? 0, this.driftTolerance = Math.max(0, n.driftTolerance ?? 0.15);
|
|
1705
|
+
}
|
|
1706
|
+
/** Map a timeline time (ms) to the media's time (seconds), never negative. */
|
|
1707
|
+
targetTime(e) {
|
|
1708
|
+
return Math.max(0, e / 1e3 + this.offset);
|
|
1709
|
+
}
|
|
1710
|
+
/**
|
|
1711
|
+
* Reconcile the media with the timeline for the current frame.
|
|
1712
|
+
*
|
|
1713
|
+
* @param timelineTimeMs current timeline time in milliseconds
|
|
1714
|
+
* @param isPlaying whether the timeline is playing
|
|
1715
|
+
*/
|
|
1716
|
+
update(e, n) {
|
|
1717
|
+
const i = this.targetTime(e);
|
|
1718
|
+
n ? (this.media.paused && this.safePlay(), Math.abs(this.media.currentTime - i) > this.driftTolerance && (this.media.currentTime = i)) : (this.media.paused || this.media.pause(), this.media.currentTime !== i && (this.media.currentTime = i));
|
|
1719
|
+
}
|
|
1720
|
+
/** Hard-align the media to a timeline time (used on explicit seeks). */
|
|
1721
|
+
seek(e) {
|
|
1722
|
+
this.media.currentTime = this.targetTime(e);
|
|
1723
|
+
}
|
|
1724
|
+
/** Mirror the timeline playback rate onto the media. */
|
|
1725
|
+
setRate(e) {
|
|
1726
|
+
this.media.playbackRate = e;
|
|
1727
|
+
}
|
|
1728
|
+
/** Pause the media and release it. */
|
|
1729
|
+
dispose() {
|
|
1730
|
+
this.media.paused || this.media.pause();
|
|
1731
|
+
}
|
|
1732
|
+
safePlay() {
|
|
1733
|
+
const e = this.media.play();
|
|
1734
|
+
e && typeof e.catch == "function" && e.catch(() => {
|
|
1735
|
+
});
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
function _n(t, e, n, i, r) {
|
|
1739
|
+
const s = n - r;
|
|
1740
|
+
if (s < 0) {
|
|
1741
|
+
e.paused || e.pause(), e.currentTime = 0;
|
|
1742
|
+
return;
|
|
1743
|
+
}
|
|
1744
|
+
t.update(s, i);
|
|
1745
|
+
}
|
|
1746
|
+
class Rn {
|
|
1747
|
+
container;
|
|
1748
|
+
timeline = null;
|
|
1749
|
+
adapter;
|
|
1750
|
+
animationFrameId;
|
|
1751
|
+
lastTime;
|
|
1752
|
+
options;
|
|
1753
|
+
targets = {};
|
|
1754
|
+
isDestroyed = !1;
|
|
1755
|
+
mediaSync;
|
|
1756
|
+
mediaTargets = [];
|
|
1757
|
+
symbolInstances = [];
|
|
1758
|
+
scenarioList = [];
|
|
1759
|
+
scenarioId;
|
|
1760
|
+
authored = /* @__PURE__ */ new Map();
|
|
1761
|
+
markerList = [];
|
|
1762
|
+
listeners = /* @__PURE__ */ new Set();
|
|
1763
|
+
/** Where the playhead was when marker crossings were last worked out */
|
|
1764
|
+
playhead = { time: 0, iteration: 0, direction: "forward" };
|
|
1765
|
+
/** Stop at the next marker crossed (step mode, or `next()`) */
|
|
1766
|
+
stepping = !1;
|
|
1767
|
+
lastMarkerId = null;
|
|
1768
|
+
reducedQuery;
|
|
1769
|
+
onReducedChange = () => this.applyReducedMotion();
|
|
1770
|
+
visibilityObserver;
|
|
1771
|
+
onScreen = !0;
|
|
1772
|
+
/** Playback was paused by leaving the screen, and resumes on return */
|
|
1773
|
+
pausedByVisibility = !1;
|
|
1774
|
+
/** `autoplay` waits for the first time the container is seen */
|
|
1775
|
+
autoplayWhenSeen = !1;
|
|
1776
|
+
onDocumentVisibility = () => this.updateVisibility();
|
|
1777
|
+
constructor(e, n = {}) {
|
|
1778
|
+
if (typeof e == "string") {
|
|
1779
|
+
const i = document.querySelector(e);
|
|
1780
|
+
if (!i)
|
|
1781
|
+
throw new Error(`Container not found: ${e}`);
|
|
1782
|
+
this.container = i;
|
|
1783
|
+
} else
|
|
1784
|
+
this.container = e;
|
|
1785
|
+
this.options = n, this.adapter = new re();
|
|
1786
|
+
}
|
|
1787
|
+
/**
|
|
1788
|
+
* Load animation from a URL or JSON object.
|
|
1789
|
+
*/
|
|
1790
|
+
async load(e) {
|
|
1791
|
+
this.scenarioList = [], this.scenarioId = void 0, await this.loadSource(e);
|
|
1792
|
+
}
|
|
1793
|
+
async loadSource(e) {
|
|
1794
|
+
let n;
|
|
1795
|
+
if (typeof e == "string") {
|
|
1796
|
+
const i = await fetch(e);
|
|
1797
|
+
if (!i.ok)
|
|
1798
|
+
throw new Error(`Failed to load animation: ${i.statusText}`);
|
|
1799
|
+
n = await i.json();
|
|
1800
|
+
} else
|
|
1801
|
+
n = e;
|
|
1802
|
+
this.useDefinition(n), this.showInitialFrame(), this.watchReducedMotion(), this.watchVisibility(), this.options.autoplay && !this.reducedMotion && (this.options.playWhenVisible && !this.onScreen ? this.autoplayWhenSeen = !0 : this.play());
|
|
1803
|
+
}
|
|
1804
|
+
/**
|
|
1805
|
+
* Make a definition the current timeline: targets, symbols and media bound, no
|
|
1806
|
+
* frame drawn and nothing played. The definition itself is not modified, so a
|
|
1807
|
+
* scenario's timeline can be used again.
|
|
1808
|
+
*/
|
|
1809
|
+
useDefinition(e) {
|
|
1810
|
+
const n = { ...e.config };
|
|
1811
|
+
this.options.speed !== void 0 && (n.speed = this.options.speed), this.options.loop !== void 0 && (n.loop = this.options.loop), this.options.alternate !== void 0 && (n.alternate = this.options.alternate), this.timeline = ie({ ...e, config: n }), this.markerList = this.timeline.markers, this.lastMarkerId = null, this.options.onComplete && (this.timeline.onComplete = this.options.onComplete), this.options.onUpdate && (this.timeline.onUpdate = this.options.onUpdate), this.autoRegisterTargets(), this.setupSymbolInstances(), this.scanMedia();
|
|
1812
|
+
}
|
|
1813
|
+
// --- scenarios: the reader chooses which timeline plays --------------------
|
|
1814
|
+
/**
|
|
1815
|
+
* Load several timelines for the same markup, and show one. The reader switches
|
|
1816
|
+
* with `setScenario`; the embed's controls and `data-tinyfly-choose` hotspots
|
|
1817
|
+
* call it.
|
|
1818
|
+
*/
|
|
1819
|
+
async loadScenarios(e, n = {}) {
|
|
1820
|
+
if (e.length === 0) throw new Error("tinyfly: loadScenarios needs at least one scenario");
|
|
1821
|
+
const i = /* @__PURE__ */ new Set();
|
|
1822
|
+
for (const s of e) {
|
|
1823
|
+
if (i.has(s.id)) throw new Error(`tinyfly: scenario id "${s.id}" is used more than once`);
|
|
1824
|
+
i.add(s.id);
|
|
1825
|
+
}
|
|
1826
|
+
const r = n.initial === void 0 ? e[0] : e.find((s) => s.id === n.initial);
|
|
1827
|
+
if (!r) throw new Error(`tinyfly: there is no scenario "${n.initial}"`);
|
|
1828
|
+
this.scenarioList = [...e], this.scenarioId = r.id, await this.loadSource(r.timeline);
|
|
1829
|
+
}
|
|
1830
|
+
/** The scenarios to choose from (empty for a single timeline). */
|
|
1831
|
+
get scenarios() {
|
|
1832
|
+
return this.scenarioList.map((e) => ({ id: e.id, label: e.label ?? e.id }));
|
|
1833
|
+
}
|
|
1834
|
+
/** The id of the scenario showing, if scenarios were loaded. */
|
|
1835
|
+
get scenario() {
|
|
1836
|
+
return this.scenarioId;
|
|
1837
|
+
}
|
|
1838
|
+
/**
|
|
1839
|
+
* Switch to another scenario. Whatever the previous one drew is undone first.
|
|
1840
|
+
* The reader stays at the same step when the new scenario has a marker with the
|
|
1841
|
+
* same id, stays at the end if they were at the end, and otherwise starts from
|
|
1842
|
+
* the beginning. Playback continues if it was playing. Under reduced motion the
|
|
1843
|
+
* new scenario's final frame shows. Returns false for an unknown id.
|
|
1844
|
+
*/
|
|
1845
|
+
setScenario(e) {
|
|
1846
|
+
const n = this.scenarioList.find((l) => l.id === e);
|
|
1847
|
+
if (!n || !this.timeline) return !1;
|
|
1848
|
+
if (e === this.scenarioId) return !0;
|
|
1849
|
+
const i = this.isPlaying, r = this.currentTime >= this.duration - 0.5, s = this.currentMarker?.id;
|
|
1850
|
+
this.stopAnimationLoop(), this.stepping = !1, this.pausedByVisibility = !1, this.restoreAuthored(), this.scenarioId = e, this.useDefinition(n.timeline), this.lastMarkerId = null;
|
|
1851
|
+
let o = 0;
|
|
1852
|
+
return this.reducedMotion || r ? o = this.duration : s !== void 0 && (o = this.markers.find((l) => l.id === s)?.time ?? 0), this.seek(o), i && !this.reducedMotion ? (this.stepping = this.options.stepMode === !0, this.startPlaying()) : this.notify(), !0;
|
|
1853
|
+
}
|
|
1854
|
+
/** Remember how an element was authored, the first time it becomes a target. */
|
|
1855
|
+
rememberAuthored(e) {
|
|
1856
|
+
if (this.authored.has(e)) return;
|
|
1857
|
+
const n = { style: e.getAttribute("style") };
|
|
1858
|
+
e.children.length === 0 && (n.text = e.textContent ?? "");
|
|
1859
|
+
const i = e.tagName.toLowerCase() === "path" ? e : e.querySelector("path");
|
|
1860
|
+
i && (n.path = { element: i, d: i.getAttribute("d") }), this.authored.set(e, n);
|
|
1861
|
+
}
|
|
1862
|
+
/** Put every target back the way it was authored. */
|
|
1863
|
+
restoreAuthored() {
|
|
1864
|
+
for (const [e, n] of this.authored) {
|
|
1865
|
+
n.style === null ? e.removeAttribute("style") : e.setAttribute("style", n.style), n.text !== void 0 && e.textContent !== n.text && (e.textContent = n.text), n.path && (n.path.d === null ? n.path.element.removeAttribute("d") : n.path.element.setAttribute("d", n.path.d));
|
|
1866
|
+
const i = e.dataset;
|
|
1867
|
+
i && delete i.shineBase;
|
|
1868
|
+
}
|
|
1869
|
+
}
|
|
1870
|
+
// --- teaching: frames, markers, reduced motion, visibility -----------------
|
|
1871
|
+
/**
|
|
1872
|
+
* Be told whenever the time, play state or current marker may have changed —
|
|
1873
|
+
* for controls and captions. Returns a function that stops it.
|
|
1874
|
+
*/
|
|
1875
|
+
subscribe(e) {
|
|
1876
|
+
return this.listeners.add(e), () => this.listeners.delete(e);
|
|
1877
|
+
}
|
|
1878
|
+
notify() {
|
|
1879
|
+
for (const e of this.listeners) e();
|
|
1880
|
+
}
|
|
1881
|
+
/** Whether the reader asked for reduced motion (and the player respects it). */
|
|
1882
|
+
get reducedMotion() {
|
|
1883
|
+
return this.reducedQuery?.matches === !0;
|
|
1884
|
+
}
|
|
1885
|
+
/** The timeline's markers, in time order. */
|
|
1886
|
+
get markers() {
|
|
1887
|
+
return this.markerList;
|
|
1888
|
+
}
|
|
1889
|
+
/** The last marker at or before the playhead. */
|
|
1890
|
+
get currentMarker() {
|
|
1891
|
+
const e = this.currentTime;
|
|
1892
|
+
let n;
|
|
1893
|
+
for (const i of this.markers)
|
|
1894
|
+
if (i.time <= e + 0.5) n = i;
|
|
1895
|
+
else break;
|
|
1896
|
+
return n;
|
|
1897
|
+
}
|
|
1898
|
+
/**
|
|
1899
|
+
* Caption for a marker (default: the current one) in a language (default: the
|
|
1900
|
+
* container's closest `lang`, then the document's), falling back to the
|
|
1901
|
+
* marker's own label.
|
|
1902
|
+
*/
|
|
1903
|
+
caption(e, n) {
|
|
1904
|
+
const i = e ? this.markers.find((o) => o.id === e) : this.currentMarker;
|
|
1905
|
+
if (!i) return;
|
|
1906
|
+
const r = n ?? this.language(), s = (o) => o?.[r]?.[i.id] ?? o?.[r.split("-")[0]]?.[i.id];
|
|
1907
|
+
return s(this.options.captions) ?? s(this.timeline?.captions) ?? i.label;
|
|
1908
|
+
}
|
|
1909
|
+
/** Move to the next marker: animated, or a jump under reduced motion. At the last marker, to the end. */
|
|
1910
|
+
next() {
|
|
1911
|
+
if (!this.timeline) return;
|
|
1912
|
+
const e = this.currentTime, n = this.markers.find((i) => i.time > e + 0.5);
|
|
1913
|
+
if (this.reducedMotion) {
|
|
1914
|
+
this.seek(n ? n.time : this.duration);
|
|
1915
|
+
return;
|
|
1916
|
+
}
|
|
1917
|
+
e >= this.duration - 0.5 || (this.stepping = n !== void 0, this.startPlaying());
|
|
1918
|
+
}
|
|
1919
|
+
/** Jump back to the previous marker (or the start). */
|
|
1920
|
+
prev() {
|
|
1921
|
+
if (!this.timeline) return;
|
|
1922
|
+
const e = this.currentTime, n = [...this.markers].reverse().find((i) => i.time < e - 0.5);
|
|
1923
|
+
this.pause(), this.seek(n ? n.time : 0);
|
|
1924
|
+
}
|
|
1925
|
+
/** Jump to a marker by id, paused there. */
|
|
1926
|
+
goToMarker(e) {
|
|
1927
|
+
const n = this.markers.find((i) => i.id === e);
|
|
1928
|
+
n && (this.pause(), this.seek(n.time));
|
|
1929
|
+
}
|
|
1930
|
+
language() {
|
|
1931
|
+
return this.container.closest("[lang]")?.getAttribute("lang") || (typeof document < "u" ? document.documentElement.lang : "") || "en";
|
|
1932
|
+
}
|
|
1933
|
+
showInitialFrame() {
|
|
1934
|
+
if (!this.timeline) return;
|
|
1935
|
+
const e = this.options.initialFrame ?? "start";
|
|
1936
|
+
if (e === "none") return;
|
|
1937
|
+
const n = e === "end" ? this.timeline.duration : e === "start" ? 0 : e;
|
|
1938
|
+
this.timeline.seek(Math.max(0, Math.min(this.timeline.duration, n))), this.applyState();
|
|
1939
|
+
}
|
|
1940
|
+
watchReducedMotion() {
|
|
1941
|
+
this.options.respectReducedMotion === !1 || typeof window > "u" || typeof window.matchMedia != "function" || (this.reducedQuery ??= window.matchMedia("(prefers-reduced-motion: reduce)"), this.reducedQuery.addEventListener?.("change", this.onReducedChange), this.reducedQuery.matches && this.applyReducedMotion());
|
|
1942
|
+
}
|
|
1943
|
+
/** Under reduced motion: stop, and show the finished state. */
|
|
1944
|
+
applyReducedMotion() {
|
|
1945
|
+
!this.reducedMotion || !this.timeline || (this.autoplayWhenSeen = !1, this.pause(), this.seek(this.timeline.duration));
|
|
1946
|
+
}
|
|
1947
|
+
watchVisibility() {
|
|
1948
|
+
if (!(!this.options.playWhenVisible || typeof window > "u")) {
|
|
1949
|
+
if (typeof IntersectionObserver == "function") {
|
|
1950
|
+
this.visibilityObserver?.disconnect(), this.visibilityObserver = new IntersectionObserver((n) => {
|
|
1951
|
+
for (const i of n) this.onScreen = i.isIntersecting;
|
|
1952
|
+
this.updateVisibility();
|
|
1953
|
+
}), this.visibilityObserver.observe(this.container);
|
|
1954
|
+
const e = this.container.getBoundingClientRect?.();
|
|
1955
|
+
e && (e.width > 0 || e.height > 0) && (this.onScreen = e.bottom > 0 && e.top < window.innerHeight && e.right > 0 && e.left < window.innerWidth);
|
|
1956
|
+
}
|
|
1957
|
+
document.addEventListener("visibilitychange", this.onDocumentVisibility);
|
|
1958
|
+
}
|
|
1959
|
+
}
|
|
1960
|
+
updateVisibility() {
|
|
1961
|
+
const e = this.onScreen && document.visibilityState !== "hidden";
|
|
1962
|
+
!e && this.isPlaying ? (this.pausedByVisibility = !0, this.pause()) : e && (this.pausedByVisibility || this.autoplayWhenSeen) && !this.reducedMotion && (this.pausedByVisibility = !1, this.autoplayWhenSeen = !1, this.startPlaying());
|
|
1963
|
+
}
|
|
1964
|
+
/** Report the current marker if it changed since the last frame. */
|
|
1965
|
+
announceMarker() {
|
|
1966
|
+
if (!this.options.onMarker) return;
|
|
1967
|
+
const e = this.currentMarker, n = e?.id;
|
|
1968
|
+
n !== this.lastMarkerId && (this.lastMarkerId = n, this.options.onMarker(e));
|
|
1969
|
+
}
|
|
1970
|
+
/**
|
|
1971
|
+
* Find embedded media elements (`[data-tinyfly-media]`) in the container and
|
|
1972
|
+
* bind each to the timeline. Emitted by the editor's export for audio/video
|
|
1973
|
+
* scene elements; the `data-tinyfly-start` attribute sets when each begins.
|
|
1974
|
+
*/
|
|
1975
|
+
scanMedia() {
|
|
1976
|
+
this.mediaTargets = [], this.container.querySelectorAll("[data-tinyfly-media]").forEach((n) => {
|
|
1977
|
+
const i = n, r = Number(i.getAttribute("data-tinyfly-start") ?? "0") || 0, s = i.getAttribute("data-volume");
|
|
1978
|
+
s !== null && (i.volume = Math.max(0, Math.min(1, Number(s) || 0))), this.mediaTargets.push({ el: i, startTime: r, sync: new me(i) });
|
|
1979
|
+
});
|
|
1980
|
+
}
|
|
1981
|
+
/** Sync all discovered media targets to a timeline time. */
|
|
1982
|
+
syncAllMedia(e, n) {
|
|
1983
|
+
for (const i of this.mediaTargets)
|
|
1984
|
+
_n(i.sync, i.el, e, n, i.startTime);
|
|
1985
|
+
}
|
|
1986
|
+
/**
|
|
1987
|
+
* Load animation from inline JSON string.
|
|
1988
|
+
*/
|
|
1989
|
+
loadFromString(e) {
|
|
1990
|
+
const n = JSON.parse(e);
|
|
1991
|
+
this.load(n);
|
|
1992
|
+
}
|
|
1993
|
+
/**
|
|
1994
|
+
* Register a target element by name.
|
|
1995
|
+
*/
|
|
1996
|
+
registerTarget(e, n) {
|
|
1997
|
+
if (typeof n == "string") {
|
|
1998
|
+
const i = this.container.querySelector(n);
|
|
1999
|
+
i && (this.rememberAuthored(i), this.targets[e] = i, this.adapter.registerTarget(e, i));
|
|
2000
|
+
} else
|
|
2001
|
+
this.rememberAuthored(n), this.targets[e] = n, this.adapter.registerTarget(e, n);
|
|
2002
|
+
}
|
|
2003
|
+
/**
|
|
2004
|
+
* Auto-register targets using data-tinyfly attribute.
|
|
2005
|
+
*/
|
|
2006
|
+
autoRegisterTargets() {
|
|
2007
|
+
this.container.querySelectorAll("[data-tinyfly]").forEach((n) => {
|
|
2008
|
+
const i = n.closest("[data-tinyfly-symbol]");
|
|
2009
|
+
if (i && i !== n) return;
|
|
2010
|
+
const r = n.getAttribute("data-tinyfly");
|
|
2011
|
+
r && this.registerTarget(r, n);
|
|
2012
|
+
}), this.timeline && new Set(this.timeline.tracks.map((i) => i.target)).forEach((i) => {
|
|
2013
|
+
if (!this.targets[i]) {
|
|
2014
|
+
const r = this.container.querySelector(`[data-tinyfly="${i}"]`) || this.container.querySelector(`.${i}`) || this.container.querySelector(`#${i}`);
|
|
2015
|
+
r && this.registerTarget(i, r);
|
|
2016
|
+
}
|
|
2017
|
+
});
|
|
2018
|
+
}
|
|
2019
|
+
/**
|
|
2020
|
+
* Bind each embedded symbol instance (`[data-tinyfly-symbol="id"]`) to its
|
|
2021
|
+
* symbol's nested timeline: a private adapter drives the instance's inner
|
|
2022
|
+
* elements, looped over the symbol's duration and synced to the main playhead.
|
|
2023
|
+
*/
|
|
2024
|
+
setupSymbolInstances() {
|
|
2025
|
+
this.symbolInstances = [];
|
|
2026
|
+
const e = this.options.symbols;
|
|
2027
|
+
if (!e || e.length === 0) return;
|
|
2028
|
+
const n = new Map(e.map((i) => [i.id, i]));
|
|
2029
|
+
this.container.querySelectorAll("[data-tinyfly-symbol]").forEach((i) => {
|
|
2030
|
+
const r = i.getAttribute("data-tinyfly-symbol");
|
|
2031
|
+
if (!r) return;
|
|
2032
|
+
const s = n.get(r);
|
|
2033
|
+
if (!s || !s.timeline.tracks?.length) return;
|
|
2034
|
+
const o = new re();
|
|
2035
|
+
i.querySelectorAll("[data-tinyfly]").forEach((l) => {
|
|
2036
|
+
const c = l.getAttribute("data-tinyfly");
|
|
2037
|
+
c && o.registerTarget(c, l);
|
|
2038
|
+
}), this.symbolInstances.push({ adapter: o, timeline: ie(s.timeline) });
|
|
2039
|
+
});
|
|
2040
|
+
}
|
|
2041
|
+
/**
|
|
2042
|
+
* Attach an audio/video element (or any {@link SyncableMedia}) that should
|
|
2043
|
+
* stay in sync with the animation timeline. The timeline remains the clock;
|
|
2044
|
+
* the media follows its play/pause/seek and rate, with drift corrected as it
|
|
2045
|
+
* plays. Pass `{ offset }` to start the media at a timeline offset.
|
|
2046
|
+
*/
|
|
2047
|
+
attachMedia(e, n) {
|
|
2048
|
+
this.mediaSync = new me(e, n), this.timeline && (this.mediaSync.setRate(this.timeline.speed), this.mediaSync.update(this.timeline.currentTime, this.isPlaying));
|
|
2049
|
+
}
|
|
2050
|
+
/** Detach and pause the currently synced media, if any. */
|
|
2051
|
+
detachMedia() {
|
|
2052
|
+
this.mediaSync?.dispose(), this.mediaSync = void 0;
|
|
2053
|
+
}
|
|
2054
|
+
/**
|
|
2055
|
+
* Start or resume playback.
|
|
2056
|
+
*/
|
|
2057
|
+
play() {
|
|
2058
|
+
!this.timeline || this.isDestroyed || (this.autoplayWhenSeen = !1, this.stepping = this.options.stepMode === !0, this.startPlaying());
|
|
2059
|
+
}
|
|
2060
|
+
startPlaying() {
|
|
2061
|
+
if (!this.timeline || this.isDestroyed) return;
|
|
2062
|
+
const e = this.timeline.playbackState === "idle";
|
|
2063
|
+
this.timeline.play(), e && this.timeline.currentTime === 0 && this.applyState(), this.playhead = { time: this.timeline.currentTime, iteration: this.timeline.loopIteration, direction: this.timeline.direction }, this.mediaSync?.update(this.timeline.currentTime, !0), this.syncAllMedia(this.timeline.currentTime, !0), this.startAnimationLoop(), this.notify();
|
|
2064
|
+
}
|
|
2065
|
+
/**
|
|
2066
|
+
* Pause playback.
|
|
2067
|
+
*/
|
|
2068
|
+
pause() {
|
|
2069
|
+
this.timeline && (this.stepping = !1, this.timeline.pause(), this.notify(), this.mediaSync?.update(this.timeline.currentTime, !1), this.syncAllMedia(this.timeline.currentTime, !1), this.stopAnimationLoop());
|
|
2070
|
+
}
|
|
2071
|
+
/**
|
|
2072
|
+
* Stop playback and reset to beginning.
|
|
2073
|
+
*/
|
|
2074
|
+
stop() {
|
|
2075
|
+
this.timeline && (this.timeline.stop(), this.stopAnimationLoop(), this.applyState(), this.mediaSync?.update(this.timeline.currentTime, !1), this.syncAllMedia(this.timeline.currentTime, !1));
|
|
2076
|
+
}
|
|
2077
|
+
/**
|
|
2078
|
+
* Seek to a specific time (in milliseconds).
|
|
2079
|
+
*/
|
|
2080
|
+
seek(e) {
|
|
2081
|
+
this.timeline && (this.timeline.seek(e), this.applyState(), this.playhead = { time: this.timeline.currentTime, iteration: this.timeline.loopIteration, direction: this.timeline.direction }, this.mediaSync?.seek(this.timeline.currentTime), this.syncAllMedia(this.timeline.currentTime, this.isPlaying));
|
|
2082
|
+
}
|
|
2083
|
+
/**
|
|
2084
|
+
* Set playback speed.
|
|
2085
|
+
*/
|
|
2086
|
+
setSpeed(e) {
|
|
2087
|
+
this.timeline && (this.timeline.speed = e, this.mediaSync?.setRate(e));
|
|
2088
|
+
}
|
|
2089
|
+
/**
|
|
2090
|
+
* Reverse playback direction.
|
|
2091
|
+
*/
|
|
2092
|
+
reverse() {
|
|
2093
|
+
this.timeline && this.timeline.reverse();
|
|
2094
|
+
}
|
|
2095
|
+
/**
|
|
2096
|
+
* Get current playback time.
|
|
2097
|
+
*/
|
|
2098
|
+
get currentTime() {
|
|
2099
|
+
return this.timeline?.currentTime ?? 0;
|
|
2100
|
+
}
|
|
2101
|
+
/**
|
|
2102
|
+
* The animated values at the playhead, for renderers that draw from values
|
|
2103
|
+
* rather than DOM elements (a canvas, or an SVG rebuilt each frame). Read it
|
|
2104
|
+
* from a `subscribe` listener. Undefined before a timeline loads.
|
|
2105
|
+
*/
|
|
2106
|
+
get state() {
|
|
2107
|
+
return this.timeline?.getStateAtTime(this.timeline.currentTime);
|
|
2108
|
+
}
|
|
2109
|
+
/**
|
|
2110
|
+
* Get total duration.
|
|
2111
|
+
*/
|
|
2112
|
+
get duration() {
|
|
2113
|
+
return this.timeline?.duration ?? 0;
|
|
2114
|
+
}
|
|
2115
|
+
/**
|
|
2116
|
+
* Check if currently playing.
|
|
2117
|
+
*/
|
|
2118
|
+
get isPlaying() {
|
|
2119
|
+
return this.timeline?.playbackState === "playing";
|
|
2120
|
+
}
|
|
2121
|
+
/**
|
|
2122
|
+
* Clean up resources.
|
|
2123
|
+
*/
|
|
2124
|
+
destroy() {
|
|
2125
|
+
this.isDestroyed = !0, this.stopAnimationLoop(), this.reducedQuery?.removeEventListener?.("change", this.onReducedChange), this.visibilityObserver?.disconnect(), typeof document < "u" && document.removeEventListener("visibilitychange", this.onDocumentVisibility), this.mediaSync?.dispose(), this.mediaSync = void 0;
|
|
2126
|
+
for (const e of this.mediaTargets)
|
|
2127
|
+
e.el.paused || e.el.pause();
|
|
2128
|
+
this.mediaTargets = [], this.adapter.clearTargets();
|
|
2129
|
+
for (const e of this.symbolInstances) e.adapter.clearTargets();
|
|
2130
|
+
this.symbolInstances = [], this.authored.clear(), this.timeline = null;
|
|
2131
|
+
}
|
|
2132
|
+
startAnimationLoop() {
|
|
2133
|
+
if (this.animationFrameId !== void 0) return;
|
|
2134
|
+
this.lastTime = performance.now();
|
|
2135
|
+
const e = (n) => {
|
|
2136
|
+
if (this.isDestroyed || !this.timeline) return;
|
|
2137
|
+
const i = n - (this.lastTime ?? n);
|
|
2138
|
+
this.lastTime = n;
|
|
2139
|
+
const r = this.playhead;
|
|
2140
|
+
this.timeline.tick(i), this.stopAtMarker(r), this.applyState();
|
|
2141
|
+
const s = this.timeline.playbackState === "playing";
|
|
2142
|
+
this.mediaSync?.update(this.timeline.currentTime, s), this.syncAllMedia(this.timeline.currentTime, s), this.timeline.playbackState === "playing" ? this.animationFrameId = requestAnimationFrame(e) : (this.animationFrameId = void 0, this.notify());
|
|
2143
|
+
};
|
|
2144
|
+
this.animationFrameId = requestAnimationFrame(e);
|
|
2145
|
+
}
|
|
2146
|
+
/**
|
|
2147
|
+
* If this frame crossed a marker it should stop at — the next one while
|
|
2148
|
+
* stepping, or any marker with `pause` — put the playhead exactly there and pause.
|
|
2149
|
+
*/
|
|
2150
|
+
stopAtMarker(e) {
|
|
2151
|
+
const n = this.timeline, i = { time: n.currentTime, iteration: n.loopIteration, direction: n.direction };
|
|
2152
|
+
this.playhead = i;
|
|
2153
|
+
const r = this.markers;
|
|
2154
|
+
if (r.length === 0) return;
|
|
2155
|
+
const { crossings: s } = De(
|
|
2156
|
+
r.map((o) => o.time),
|
|
2157
|
+
e,
|
|
2158
|
+
i,
|
|
2159
|
+
{ duration: n.duration, alternate: n.config.alternate === !0, holding: n.repeatDelayRemaining > 0 }
|
|
2160
|
+
);
|
|
2161
|
+
for (const o of s) {
|
|
2162
|
+
if (o.kind !== "event") continue;
|
|
2163
|
+
const l = r[o.index];
|
|
2164
|
+
if (this.stepping || l.pause) {
|
|
2165
|
+
this.stepping = !1, n.pause(), n.seek(l.time), this.playhead = { time: l.time, iteration: n.loopIteration, direction: n.direction };
|
|
2166
|
+
return;
|
|
2167
|
+
}
|
|
2168
|
+
}
|
|
2169
|
+
}
|
|
2170
|
+
stopAnimationLoop() {
|
|
2171
|
+
this.animationFrameId !== void 0 && (cancelAnimationFrame(this.animationFrameId), this.animationFrameId = void 0);
|
|
2172
|
+
}
|
|
2173
|
+
applyState() {
|
|
2174
|
+
if (!this.timeline) return;
|
|
2175
|
+
const e = this.timeline.currentTime;
|
|
2176
|
+
this.adapter.applyState(this.timeline.getStateAtTime(e)), this.announceMarker(), this.notify();
|
|
2177
|
+
for (const n of this.symbolInstances) {
|
|
2178
|
+
const i = n.timeline.duration;
|
|
2179
|
+
n.adapter.applyState(n.timeline.getStateAtTime(i > 0 ? e % i : e));
|
|
2180
|
+
}
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
2183
|
+
const Vn = {
|
|
2184
|
+
play: "Play",
|
|
2185
|
+
pause: "Pause",
|
|
2186
|
+
prev: "Previous step",
|
|
2187
|
+
next: "Next step",
|
|
2188
|
+
restart: "Restart",
|
|
2189
|
+
scrub: "Position",
|
|
2190
|
+
speed: "Speed",
|
|
2191
|
+
reveal: "Reveal",
|
|
2192
|
+
step: "Step",
|
|
2193
|
+
of: "of",
|
|
2194
|
+
stepFormat: "{index} / {total}",
|
|
2195
|
+
scenario: "Scenario",
|
|
2196
|
+
fullscreen: "Full screen",
|
|
2197
|
+
exitFullscreen: "Exit full screen"
|
|
2198
|
+
}, ye = "tinyfly-controls-style", Wn = `
|
|
2199
|
+
.tf-ctl { --tf-ctl-fg: #1d1d1f; --tf-ctl-bg: #f4f2ee; --tf-ctl-accent: #c2410c; --tf-ctl-radius: 8px;
|
|
2200
|
+
font: 14px/1.4 var(--tf-ctl-font, system-ui, sans-serif); color: var(--tf-ctl-fg); margin-top: 8px; }
|
|
2201
|
+
.tf-ctl-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 6px; border-radius: var(--tf-ctl-radius); background: var(--tf-ctl-bg); }
|
|
2202
|
+
.tf-ctl-btn { min-width: 36px; height: 36px; padding: 0 10px; border: 0; border-radius: calc(var(--tf-ctl-radius) - 2px); background: transparent; color: inherit; font: inherit; cursor: pointer; }
|
|
2203
|
+
.tf-ctl-btn:hover { background: color-mix(in srgb, var(--tf-ctl-fg) 8%, transparent); }
|
|
2204
|
+
.tf-ctl-btn:focus-visible, .tf-ctl-scrub:focus-visible, .tf-ctl-speed:focus-visible { outline: 2px solid var(--tf-ctl-accent); outline-offset: 2px; }
|
|
2205
|
+
.tf-ctl-btn[disabled] { opacity: 0.4; cursor: default; }
|
|
2206
|
+
.tf-ctl-primary { background: var(--tf-ctl-accent); color: #fff; }
|
|
2207
|
+
.tf-ctl-primary:hover { background: var(--tf-ctl-accent); filter: brightness(1.08); }
|
|
2208
|
+
.tf-ctl-scrub { flex: 1 1 120px; min-width: 80px; accent-color: var(--tf-ctl-accent); }
|
|
2209
|
+
.tf-ctl-speed { height: 32px; border: 0; border-radius: 6px; background: transparent; color: inherit; font: inherit; }
|
|
2210
|
+
.tf-ctl-step { font-variant-numeric: tabular-nums; opacity: 0.75; padding: 0 4px; }
|
|
2211
|
+
.tf-ctl-caption { margin: 6px 2px 0; min-height: 1.4em; }
|
|
2212
|
+
.tf-ctl-question { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 6px 2px 0; font-weight: 600; }
|
|
2213
|
+
.tf-ctl-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
|
|
2214
|
+
/* The hidden attribute only hides through the browser's default stylesheet; any rule
|
|
2215
|
+
above that sets display would override it, so enforce it for everything here. */
|
|
2216
|
+
.tf-ctl [hidden] { display: none !important; }
|
|
2217
|
+
.tf-ctl-choices { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin: 8px 0 0; padding: 0; border: 0; }
|
|
2218
|
+
.tf-ctl-choices legend { float: left; margin-right: 4px; padding: 0; font-weight: 600; }
|
|
2219
|
+
.tf-ctl-choice { position: relative; display: inline-flex; }
|
|
2220
|
+
.tf-ctl-choice input { position: absolute; opacity: 0; width: 1px; height: 1px; }
|
|
2221
|
+
.tf-ctl-choice span { display: inline-block; padding: 6px 12px; border-radius: var(--tf-ctl-radius); background: var(--tf-ctl-bg); cursor: pointer; }
|
|
2222
|
+
.tf-ctl-choice input:checked + span { background: var(--tf-ctl-accent); color: #fff; }
|
|
2223
|
+
.tf-ctl-choice input:focus-visible + span { outline: 2px solid var(--tf-ctl-accent); outline-offset: 2px; }
|
|
2224
|
+
.tf-ctl-choice-slider { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 8px 0 0; }
|
|
2225
|
+
.tf-ctl-choice-slider span { font-weight: 600; }
|
|
2226
|
+
.tf-ctl-choice-slider input { flex: 1 1 140px; accent-color: var(--tf-ctl-accent); }
|
|
2227
|
+
.tf-ctl-choice-slider input:focus-visible { outline: 2px solid var(--tf-ctl-accent); outline-offset: 2px; }
|
|
2228
|
+
.tf-ctl-choice-slider output { font-variant-numeric: tabular-nums; min-width: 4em; }
|
|
2229
|
+
.tf-ctl-fullscreen { margin-left: auto; display: inline-flex; align-items: center; justify-content: center; }
|
|
2230
|
+
.tf-ctl-fullscreen svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
|
|
2231
|
+
/* Full screen, by either route. The figure becomes a column: the drawing takes the room
|
|
2232
|
+
that's left after the controls and captions. !important because the host page's own
|
|
2233
|
+
figure rules (a max-width, a min-width that makes a diagram scroll on phones) would
|
|
2234
|
+
otherwise keep the drawing at its in-page size. */
|
|
2235
|
+
.tf-fullscreen { box-sizing: border-box !important; display: flex !important; flex-direction: column; width: 100% !important; max-width: none !important; height: 100vh; height: 100dvh; margin: 0 !important; padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)) !important; overflow: auto !important; background: var(--tf-fullscreen-bg, #fff); }
|
|
2236
|
+
.tf-fullscreen > * { flex: none; }
|
|
2237
|
+
.tf-fullscreen > svg, .tf-fullscreen > canvas { flex: 1 1 0; min-height: 0; width: 100% !important; min-width: 0 !important; height: 100% !important; }
|
|
2238
|
+
.tf-fullscreen-overlay { position: fixed !important; inset: 0; z-index: 2147483000; }
|
|
2239
|
+
/* A short landscape screen (a phone on its side): stacking the controls under the drawing
|
|
2240
|
+
leaves the drawing a thin strip, so put them in a column beside it instead. */
|
|
2241
|
+
@media (orientation: landscape) and (max-height: 520px) {
|
|
2242
|
+
.tf-fullscreen { display: grid !important; grid-template-columns: minmax(0, 1fr) minmax(200px, 34%); grid-auto-rows: min-content; column-gap: 12px; align-content: start; }
|
|
2243
|
+
.tf-fullscreen > svg, .tf-fullscreen > canvas { grid-column: 1; grid-row: 1 / span 12; height: calc(100vh - 24px) !important; height: calc(100dvh - 24px) !important; }
|
|
2244
|
+
.tf-fullscreen > :not(svg):not(canvas) { grid-column: 2; }
|
|
2245
|
+
}
|
|
2246
|
+
`;
|
|
2247
|
+
let Bn = 0;
|
|
2248
|
+
function Un(t) {
|
|
2249
|
+
if (t.getElementById(ye)) return;
|
|
2250
|
+
const e = t.createElement("style");
|
|
2251
|
+
e.id = ye, e.textContent = Wn, t.head.appendChild(e);
|
|
2252
|
+
}
|
|
2253
|
+
function Hn(t, e, n = {}) {
|
|
2254
|
+
const i = e.ownerDocument;
|
|
2255
|
+
Un(i);
|
|
2256
|
+
const r = { ...Vn, ...n.labels }, s = n.speeds ?? [0.5, 1, 2], o = () => t.markers.length > 0, l = () => t.markers.some((S) => S.label !== void 0 || t.caption(S.id) !== void 0), c = i.createElement("div");
|
|
2257
|
+
c.className = "tf-ctl";
|
|
2258
|
+
const f = i.createElement("div");
|
|
2259
|
+
f.className = "tf-ctl-bar", f.setAttribute("role", "group");
|
|
2260
|
+
const u = (S, C, nt, Q = "") => {
|
|
2261
|
+
const U = i.createElement("button");
|
|
2262
|
+
return U.type = "button", U.className = `tf-ctl-btn ${Q}`.trim(), U.setAttribute("aria-label", S), U.title = S, U.textContent = C, U.addEventListener("click", nt), U;
|
|
2263
|
+
}, h = u(r.restart, "⟲", () => {
|
|
2264
|
+
t.pause(), t.seek(0);
|
|
2265
|
+
}), d = u(r.prev, "|◀", () => t.prev()), y = u(r.play, "▶", () => t.isPlaying ? t.pause() : p(), "tf-ctl-primary"), a = u(r.next, "▶|", () => t.next()), p = () => {
|
|
2266
|
+
t.currentTime >= t.duration - 0.5 && t.seek(0), t.play();
|
|
2267
|
+
}, m = i.createElement("input");
|
|
2268
|
+
m.type = "range", m.className = "tf-ctl-scrub", m.min = "0", m.max = "1000", m.step = "1", m.setAttribute("aria-label", r.scrub), m.addEventListener("input", () => {
|
|
2269
|
+
t.pause(), t.seek(Number(m.value) / 1e3 * t.duration);
|
|
2270
|
+
});
|
|
2271
|
+
const b = i.createElement("span");
|
|
2272
|
+
b.className = "tf-ctl-step";
|
|
2273
|
+
const w = i.createElement("select");
|
|
2274
|
+
w.className = "tf-ctl-speed", w.setAttribute("aria-label", r.speed);
|
|
2275
|
+
for (const S of s) {
|
|
2276
|
+
const C = i.createElement("option");
|
|
2277
|
+
C.value = String(S), C.textContent = `${S}×`, S === 1 && (C.selected = !0), w.appendChild(C);
|
|
2278
|
+
}
|
|
2279
|
+
w.addEventListener("change", () => t.setSpeed(Number(w.value))), f.append(h, d, y, a, m, b), s.length > 0 && f.append(w), c.append(f);
|
|
2280
|
+
const $ = n.fullscreen ? Gn(e, i, r) : void 0;
|
|
2281
|
+
$ && f.append($.button);
|
|
2282
|
+
const g = i.createElement("p");
|
|
2283
|
+
g.className = "tf-ctl-caption", g.setAttribute("aria-live", "polite"), n.captions !== !1 && c.append(g);
|
|
2284
|
+
const k = i.createElement("div");
|
|
2285
|
+
k.className = "tf-ctl-question", k.hidden = !0;
|
|
2286
|
+
const v = i.createElement("span"), x = u(r.reveal, r.reveal, () => t.play(), "tf-ctl-primary");
|
|
2287
|
+
k.append(v, x), c.append(k);
|
|
2288
|
+
const M = Xn(t, i, r.scenario, n.scenarioControl ?? "buttons");
|
|
2289
|
+
M && c.append(M.element);
|
|
2290
|
+
const T = n.mount;
|
|
2291
|
+
T ? T.appendChild(c) : e.insertAdjacentElement("afterend", c);
|
|
2292
|
+
const P = () => {
|
|
2293
|
+
const S = t.isPlaying;
|
|
2294
|
+
y.textContent = S ? "❚❚" : "▶", y.setAttribute("aria-label", S ? r.pause : r.play), y.title = S ? r.pause : r.play;
|
|
2295
|
+
const C = t.duration;
|
|
2296
|
+
i.activeElement !== m && (m.value = String(C > 0 ? Math.round(t.currentTime / C * 1e3) : 0));
|
|
2297
|
+
const nt = t.markers;
|
|
2298
|
+
if (d.hidden = a.hidden = b.hidden = nt.length === 0, nt.length > 0) {
|
|
2299
|
+
const Q = t.currentMarker, U = Q ? nt.indexOf(Q) + 1 : 0;
|
|
2300
|
+
b.textContent = r.stepFormat.replace("{index}", String(U)).replace("{total}", String(nt.length)), b.setAttribute("aria-label", `${r.step} ${U} ${r.of} ${nt.length}`), d.disabled = t.currentTime <= 0.5, a.disabled = t.currentTime >= C - 0.5;
|
|
2301
|
+
const ee = t.caption() ?? "";
|
|
2302
|
+
g.textContent !== ee && (g.textContent = ee), g.hidden = !l();
|
|
2303
|
+
const ne = !S && Q?.question !== void 0 && Math.abs(t.currentTime - Q.time) < 1;
|
|
2304
|
+
k.hidden = !ne, ne && v.textContent !== Q.question && (v.textContent = Q.question);
|
|
2305
|
+
} else
|
|
2306
|
+
k.hidden = !0, g.hidden = !0;
|
|
2307
|
+
M?.update();
|
|
2308
|
+
}, K = t.subscribe(P);
|
|
2309
|
+
P();
|
|
2310
|
+
const j = n.keyboardScope ?? e;
|
|
2311
|
+
!j.hasAttribute("tabindex") && j.tabIndex < 0 && (j.tabIndex = 0);
|
|
2312
|
+
const te = /* @__PURE__ */ new WeakSet(), kt = (S) => {
|
|
2313
|
+
if (te.has(S) || (te.add(S), S.defaultPrevented || S.altKey || S.ctrlKey || S.metaKey)) return;
|
|
2314
|
+
const C = S.target;
|
|
2315
|
+
if (!(C.tagName === "INPUT" || C.tagName === "SELECT") && !(S.key === " " && C.tagName === "BUTTON"))
|
|
2316
|
+
switch (S.key) {
|
|
2317
|
+
case " ":
|
|
2318
|
+
S.preventDefault(), t.isPlaying ? t.pause() : p();
|
|
2319
|
+
break;
|
|
2320
|
+
case "ArrowRight":
|
|
2321
|
+
if (!o()) return;
|
|
2322
|
+
S.preventDefault(), t.next();
|
|
2323
|
+
break;
|
|
2324
|
+
case "ArrowLeft":
|
|
2325
|
+
if (!o()) return;
|
|
2326
|
+
S.preventDefault(), t.prev();
|
|
2327
|
+
break;
|
|
2328
|
+
case "Home":
|
|
2329
|
+
S.preventDefault(), t.pause(), t.seek(0);
|
|
2330
|
+
break;
|
|
2331
|
+
case "f":
|
|
2332
|
+
case "F":
|
|
2333
|
+
if (!$) return;
|
|
2334
|
+
S.preventDefault(), $.active ? $.exit() : $.enter();
|
|
2335
|
+
break;
|
|
2336
|
+
}
|
|
2337
|
+
};
|
|
2338
|
+
return j.addEventListener("keydown", kt), c.addEventListener("keydown", kt), {
|
|
2339
|
+
element: c,
|
|
2340
|
+
fullscreen: $ && {
|
|
2341
|
+
get active() {
|
|
2342
|
+
return $.active;
|
|
2343
|
+
},
|
|
2344
|
+
enter: $.enter,
|
|
2345
|
+
exit: $.exit
|
|
2346
|
+
},
|
|
2347
|
+
destroy() {
|
|
2348
|
+
$?.destroy(), K(), j.removeEventListener("keydown", kt), c.removeEventListener("keydown", kt), c.remove();
|
|
2349
|
+
}
|
|
2350
|
+
};
|
|
2351
|
+
}
|
|
2352
|
+
function Xn(t, e, n, i) {
|
|
2353
|
+
const r = t.scenarios;
|
|
2354
|
+
if (r.length < 2) return;
|
|
2355
|
+
if (i === "slider") {
|
|
2356
|
+
const u = e.createElement("div");
|
|
2357
|
+
u.className = "tf-ctl-choice-slider";
|
|
2358
|
+
const h = e.createElement("span");
|
|
2359
|
+
h.textContent = n, h.setAttribute("aria-hidden", "true");
|
|
2360
|
+
const d = e.createElement("input");
|
|
2361
|
+
d.type = "range", d.min = "0", d.max = String(r.length - 1), d.step = "1", d.setAttribute("aria-label", n);
|
|
2362
|
+
const y = e.createElement("output");
|
|
2363
|
+
return y.setAttribute("aria-hidden", "true"), d.addEventListener("input", () => {
|
|
2364
|
+
const p = r[Number(d.value)];
|
|
2365
|
+
p && t.setScenario(p.id);
|
|
2366
|
+
}), u.append(h, d, y), { element: u, update: () => {
|
|
2367
|
+
const p = Math.max(0, r.findIndex((b) => b.id === t.scenario));
|
|
2368
|
+
e.activeElement !== d && (d.value = String(p));
|
|
2369
|
+
const m = r[p].label;
|
|
2370
|
+
y.textContent !== m && (y.textContent = m), d.setAttribute("aria-valuetext", m);
|
|
2371
|
+
} };
|
|
2372
|
+
}
|
|
2373
|
+
const s = e.createElement("fieldset");
|
|
2374
|
+
s.className = "tf-ctl-choices";
|
|
2375
|
+
const o = e.createElement("legend");
|
|
2376
|
+
o.textContent = n, s.append(o);
|
|
2377
|
+
const l = `tf-ctl-scenario-${++Bn}`, c = r.map((u) => {
|
|
2378
|
+
const h = e.createElement("label");
|
|
2379
|
+
h.className = "tf-ctl-choice";
|
|
2380
|
+
const d = e.createElement("input");
|
|
2381
|
+
d.type = "radio", d.name = l, d.value = u.id, d.addEventListener("change", () => {
|
|
2382
|
+
d.checked && t.setScenario(u.id);
|
|
2383
|
+
});
|
|
2384
|
+
const y = e.createElement("span");
|
|
2385
|
+
return y.textContent = u.label, h.append(d, y), s.append(h), d;
|
|
2386
|
+
});
|
|
2387
|
+
return { element: s, update: () => {
|
|
2388
|
+
for (const u of c) {
|
|
2389
|
+
const h = u.value === t.scenario;
|
|
2390
|
+
u.checked !== h && (u.checked = h);
|
|
2391
|
+
}
|
|
2392
|
+
} };
|
|
2393
|
+
}
|
|
2394
|
+
const Yn = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 9V4h5M20 9V4h-5M4 15v5h5M20 15v5h-5"/></svg>', Zn = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9 4v5H4M15 4v5h5M9 20v-5H4M15 20v-5h5"/></svg>';
|
|
2395
|
+
function Gn(t, e, n) {
|
|
2396
|
+
const i = e, r = t, s = e.createElement("button");
|
|
2397
|
+
s.type = "button", s.className = "tf-ctl-btn tf-ctl-fullscreen";
|
|
2398
|
+
let o, l = "";
|
|
2399
|
+
const c = () => {
|
|
2400
|
+
const p = o !== void 0;
|
|
2401
|
+
s.innerHTML = p ? Zn : Yn;
|
|
2402
|
+
const m = p ? n.exitFullscreen : n.fullscreen;
|
|
2403
|
+
s.setAttribute("aria-label", m), s.title = m, s.setAttribute("aria-pressed", String(p)), t.classList.toggle("tf-fullscreen", p), t.classList.toggle("tf-fullscreen-overlay", o === "overlay");
|
|
2404
|
+
}, f = () => i.fullscreenElement ?? i.webkitFullscreenElement ?? null, u = () => {
|
|
2405
|
+
f() === t ? o = "native" : o === "native" && (o = void 0), c();
|
|
2406
|
+
}, h = (p) => {
|
|
2407
|
+
p.key === "Escape" && a();
|
|
2408
|
+
}, d = () => {
|
|
2409
|
+
o = "overlay", l = e.documentElement.style.overflow, e.documentElement.style.overflow = "hidden", e.addEventListener("keydown", h), c();
|
|
2410
|
+
};
|
|
2411
|
+
async function y() {
|
|
2412
|
+
if (o) return;
|
|
2413
|
+
const p = r.requestFullscreen?.bind(r) ?? r.webkitRequestFullscreen?.bind(r), m = i.fullscreenEnabled ?? i.webkitFullscreenEnabled ?? !1;
|
|
2414
|
+
if (p && m)
|
|
2415
|
+
try {
|
|
2416
|
+
if (await p(), f() === t) {
|
|
2417
|
+
o = "native", c();
|
|
2418
|
+
return;
|
|
2419
|
+
}
|
|
2420
|
+
} catch {
|
|
2421
|
+
}
|
|
2422
|
+
d();
|
|
2423
|
+
}
|
|
2424
|
+
async function a() {
|
|
2425
|
+
if (o === "overlay")
|
|
2426
|
+
e.removeEventListener("keydown", h), e.documentElement.style.overflow = l, o = void 0, c();
|
|
2427
|
+
else if (o === "native") {
|
|
2428
|
+
o = void 0, c();
|
|
2429
|
+
const p = i.exitFullscreen?.bind(i) ?? i.webkitExitFullscreen?.bind(i);
|
|
2430
|
+
f() === t && p && await p();
|
|
2431
|
+
}
|
|
2432
|
+
}
|
|
2433
|
+
return s.addEventListener("click", () => {
|
|
2434
|
+
o ? a() : y();
|
|
2435
|
+
}), e.addEventListener("fullscreenchange", u), e.addEventListener("webkitfullscreenchange", u), c(), {
|
|
2436
|
+
button: s,
|
|
2437
|
+
get active() {
|
|
2438
|
+
return o !== void 0;
|
|
2439
|
+
},
|
|
2440
|
+
enter: y,
|
|
2441
|
+
exit: a,
|
|
2442
|
+
destroy() {
|
|
2443
|
+
a(), e.removeEventListener("fullscreenchange", u), e.removeEventListener("webkitfullscreenchange", u), s.remove();
|
|
2444
|
+
}
|
|
2445
|
+
};
|
|
2446
|
+
}
|
|
2447
|
+
const ge = "tinyfly-choices-style", Kn = `
|
|
2448
|
+
[data-tinyfly-choose] { cursor: pointer; }
|
|
2449
|
+
[data-tinyfly-choose]:focus-visible { outline: 2px solid var(--tf-ctl-accent, #c2410c); outline-offset: 2px; }
|
|
2450
|
+
`;
|
|
2451
|
+
function Qn(t) {
|
|
2452
|
+
if (t.getElementById(ge)) return;
|
|
2453
|
+
const e = t.createElement("style");
|
|
2454
|
+
e.id = ge, e.textContent = Kn, t.head.appendChild(e);
|
|
2455
|
+
}
|
|
2456
|
+
function Jn(t, e) {
|
|
2457
|
+
const n = Array.from(e.querySelectorAll("[data-tinyfly-choose]"));
|
|
2458
|
+
if (n.length === 0) return () => {
|
|
2459
|
+
};
|
|
2460
|
+
Qn(e.ownerDocument);
|
|
2461
|
+
const i = [], r = [];
|
|
2462
|
+
for (const l of n) {
|
|
2463
|
+
const c = l.getAttribute("data-tinyfly-choose") ?? "", f = [], u = (a, p) => {
|
|
2464
|
+
l.hasAttribute(a) || (l.setAttribute(a, p), f.push(a));
|
|
2465
|
+
};
|
|
2466
|
+
u("role", "button"), u("tabindex", "0");
|
|
2467
|
+
const h = t.scenarios.find((a) => a.id === c)?.label;
|
|
2468
|
+
h !== void 0 && u("aria-label", h), l.setAttribute("aria-pressed", "false"), f.push("aria-pressed"), i.push({ element: l, attributes: f });
|
|
2469
|
+
const d = () => t.setScenario(c), y = (a) => {
|
|
2470
|
+
const p = a.key;
|
|
2471
|
+
p !== "Enter" && p !== " " || (a.preventDefault(), d());
|
|
2472
|
+
};
|
|
2473
|
+
l.addEventListener("click", d), l.addEventListener("keydown", y), r.push(() => {
|
|
2474
|
+
l.removeEventListener("click", d), l.removeEventListener("keydown", y);
|
|
2475
|
+
});
|
|
2476
|
+
}
|
|
2477
|
+
const s = () => {
|
|
2478
|
+
for (const { element: l } of i)
|
|
2479
|
+
l.setAttribute("aria-pressed", String(l.getAttribute("data-tinyfly-choose") === t.scenario));
|
|
2480
|
+
}, o = t.subscribe(s);
|
|
2481
|
+
return s(), () => {
|
|
2482
|
+
o();
|
|
2483
|
+
for (const l of r) l();
|
|
2484
|
+
for (const { element: l, attributes: c } of i) for (const f of c) l.removeAttribute(f);
|
|
2485
|
+
};
|
|
2486
|
+
}
|
|
2487
|
+
const be = /* @__PURE__ */ new WeakMap(), ti = /* @__PURE__ */ new WeakMap();
|
|
2488
|
+
let ei = 0;
|
|
2489
|
+
function ht(t, e, n) {
|
|
2490
|
+
if (t)
|
|
2491
|
+
try {
|
|
2492
|
+
return JSON.parse(t);
|
|
2493
|
+
} catch (i) {
|
|
2494
|
+
console.warn(`tinyfly: invalid ${e} JSON on`, n, i);
|
|
2495
|
+
return;
|
|
2496
|
+
}
|
|
2497
|
+
}
|
|
2498
|
+
async function ni(t, e = {}) {
|
|
2499
|
+
const n = be.get(t);
|
|
2500
|
+
if (n) return n;
|
|
2501
|
+
const i = Array.from(t.querySelectorAll("script[data-tinyfly-timeline]")), r = i[0], s = t.getAttribute("data-src"), o = ri(t.getAttribute("data-markers")), l = i.length > 1 || r?.hasAttribute("data-scenario") ? ii(i, o, t) : void 0;
|
|
2502
|
+
if (l && l.length === 0) return;
|
|
2503
|
+
let c = r && !l ? ht(r.textContent, "timeline", t) : void 0;
|
|
2504
|
+
if (!c && s && o) {
|
|
2505
|
+
const y = await fetch(s);
|
|
2506
|
+
y.ok && (c = await y.json());
|
|
2507
|
+
}
|
|
2508
|
+
if (c && o && (c = ze(c, o)), !c && !s && !l) {
|
|
2509
|
+
console.warn('tinyfly: embed has no timeline (a <script type="application/json" data-tinyfly-timeline> or data-src)', t);
|
|
2510
|
+
return;
|
|
2511
|
+
}
|
|
2512
|
+
const f = ht(t.querySelector("script[data-tinyfly-captions]")?.textContent, "captions", t), u = {
|
|
2513
|
+
playWhenVisible: !0,
|
|
2514
|
+
...e.player,
|
|
2515
|
+
...f && { captions: f },
|
|
2516
|
+
...ht(t.getAttribute("data-options"), "data-options", t)
|
|
2517
|
+
};
|
|
2518
|
+
si(t);
|
|
2519
|
+
const h = new Rn(t, u), d = { element: t, player: h };
|
|
2520
|
+
if (be.set(t, d), t.setAttribute("data-tinyfly-mounted", ""), l) {
|
|
2521
|
+
const y = t.getAttribute("data-scenario") ?? void 0;
|
|
2522
|
+
await h.loadScenarios(l, { initial: l.some((a) => a.id === y) ? y : void 0 }), ti.set(t, Jn(h, t));
|
|
2523
|
+
} else
|
|
2524
|
+
await h.load(c ?? s);
|
|
2525
|
+
if (t.getAttribute("data-controls") !== "false") {
|
|
2526
|
+
const y = ht(t.getAttribute("data-labels"), "data-labels", t), a = t.querySelector("figcaption"), p = t.getAttribute("data-scenario-legend"), m = t.getAttribute("data-scenario-control");
|
|
2527
|
+
d.controls = Hn(h, t, {
|
|
2528
|
+
...e.controls,
|
|
2529
|
+
...t.getAttribute("data-fullscreen") === "true" ? { fullscreen: !0 } : {},
|
|
2530
|
+
...m === "slider" || m === "buttons" ? { scenarioControl: m } : {},
|
|
2531
|
+
labels: { ...e.controls?.labels, ...y, ...p ? { scenario: p } : {} },
|
|
2532
|
+
// Inside the figure, before its figcaption, so the caption stays last.
|
|
2533
|
+
mount: void 0
|
|
2534
|
+
}), a ? t.insertBefore(d.controls.element, a) : t.appendChild(d.controls.element);
|
|
2535
|
+
}
|
|
2536
|
+
return d;
|
|
2537
|
+
}
|
|
2538
|
+
function ii(t, e, n) {
|
|
2539
|
+
const i = [];
|
|
2540
|
+
return t.forEach((r, s) => {
|
|
2541
|
+
const o = ht(r.textContent, "timeline", n);
|
|
2542
|
+
if (!o) return;
|
|
2543
|
+
const l = r.getAttribute("data-scenario") || `scenario-${s + 1}`;
|
|
2544
|
+
if (i.some((f) => f.id === l)) {
|
|
2545
|
+
console.warn(`tinyfly: scenario id "${l}" is used more than once; the later one is skipped`, n);
|
|
2546
|
+
return;
|
|
2547
|
+
}
|
|
2548
|
+
const c = r.getAttribute("data-scenario-label") ?? void 0;
|
|
2549
|
+
i.push({ id: l, label: c, timeline: e ? ze(o, e) : o });
|
|
2550
|
+
}), i;
|
|
2551
|
+
}
|
|
2552
|
+
function ze(t, e) {
|
|
2553
|
+
return t.config.markers?.length ? t : { ...t, config: { ...t.config, markers: e.map((n, i) => ({ id: `step-${i + 1}`, time: n })) } };
|
|
2554
|
+
}
|
|
2555
|
+
function ri(t) {
|
|
2556
|
+
if (!t) return;
|
|
2557
|
+
const e = t.split(/[\s,]+/).filter(Boolean).map(Number).filter((n) => Number.isFinite(n) && n >= 0).sort((n, i) => n - i);
|
|
2558
|
+
return e.length > 0 ? e : void 0;
|
|
2559
|
+
}
|
|
2560
|
+
function si(t) {
|
|
2561
|
+
const e = t.querySelector("svg");
|
|
2562
|
+
if (!e || e.hasAttribute("role") || e.hasAttribute("aria-hidden")) return;
|
|
2563
|
+
const n = t.getAttribute("data-alt"), i = t.querySelector("figcaption");
|
|
2564
|
+
e.setAttribute("role", "img"), n ? e.setAttribute("aria-label", n) : i && (i.id ||= `tinyfly-caption-${++ei}`, e.setAttribute("aria-labelledby", i.id));
|
|
2565
|
+
}
|
|
2566
|
+
const oi = "http://www.w3.org/2000/svg", Pt = /* @__PURE__ */ new WeakMap();
|
|
2567
|
+
function ai(t, e) {
|
|
2568
|
+
const n = t?.values.get(e);
|
|
2569
|
+
return n ? Object.fromEntries(n) : {};
|
|
2570
|
+
}
|
|
2571
|
+
async function li(t, e = {}) {
|
|
2572
|
+
const n = Pt.get(t);
|
|
2573
|
+
if (n) return n;
|
|
2574
|
+
const i = t.querySelector("script[data-tinyfly-figure]");
|
|
2575
|
+
let r;
|
|
2576
|
+
try {
|
|
2577
|
+
r = bt(JSON.parse(i?.textContent ?? "null"));
|
|
2578
|
+
} catch (d) {
|
|
2579
|
+
console.warn("tinyfly: math figure is missing or invalid", t, d);
|
|
2580
|
+
return;
|
|
2581
|
+
}
|
|
2582
|
+
const s = t.getAttribute("data-target") ?? "fig";
|
|
2583
|
+
let o = t.querySelector("svg[data-tinyfly-math-svg]");
|
|
2584
|
+
o || (o = t.ownerDocument.createElementNS(oi, "svg"), o.setAttribute("data-tinyfly-math-svg", ""), o.setAttribute("viewBox", `0 0 ${r.width} ${r.height}`), o.setAttribute("style", "display:block;width:100%;height:auto"), t.insertBefore(o, t.querySelector("figcaption")));
|
|
2585
|
+
const l = (d) => {
|
|
2586
|
+
o.innerHTML = Kt(Lt(r, d));
|
|
2587
|
+
};
|
|
2588
|
+
if (l({}), !(t.querySelector("script[data-tinyfly-timeline]") || t.hasAttribute("data-src"))) {
|
|
2589
|
+
const d = { element: t, figure: r, redraw: () => l({}) };
|
|
2590
|
+
return Pt.set(t, d), d;
|
|
2591
|
+
}
|
|
2592
|
+
const f = await ni(t, e);
|
|
2593
|
+
if (!f) return;
|
|
2594
|
+
const u = () => l(ai(f.player.state, s));
|
|
2595
|
+
f.player.subscribe(u), u();
|
|
2596
|
+
const h = { ...f, figure: r, redraw: u };
|
|
2597
|
+
return Pt.set(t, h), h;
|
|
2598
|
+
}
|
|
2599
|
+
async function yi(t = document, e = {}) {
|
|
2600
|
+
const n = Array.from(t.querySelectorAll("[data-tinyfly-math]"));
|
|
2601
|
+
return (await Promise.all(n.map((r) => li(r, e)))).filter((r) => r !== void 0);
|
|
2602
|
+
}
|
|
2603
|
+
const ci = "http://www.w3.org/2000/svg";
|
|
2604
|
+
function gi(t, e, n = {}) {
|
|
2605
|
+
bt(e);
|
|
2606
|
+
const i = { ...e.values ?? {} };
|
|
2607
|
+
for (const g of n.sliders ?? [])
|
|
2608
|
+
if (typeof i[g.value] != "number") throw new Error(`mathExplorable: slider value "${g.value}" is not a number value of the figure`);
|
|
2609
|
+
for (const g of n.drag ?? []) {
|
|
2610
|
+
if ("vector" in g && !tt(i[g.vector])) throw new Error(`mathExplorable: "${g.vector}" is not a vector value of the figure`);
|
|
2611
|
+
if ("matrix" in g && !lt(i[g.matrix])) throw new Error(`mathExplorable: "${g.matrix}" is not a matrix value of the figure`);
|
|
2612
|
+
}
|
|
2613
|
+
const r = Te(e), s = t.ownerDocument, o = s.createElementNS(ci, "svg");
|
|
2614
|
+
o.setAttribute("viewBox", `0 0 ${e.width} ${e.height}`), o.setAttribute("style", "display:block;width:100%;height:auto;touch-action:none"), o.setAttribute("role", "img"), t.appendChild(o);
|
|
2615
|
+
const l = n.drag ?? [], c = (g) => {
|
|
2616
|
+
if ("vector" in g) return i[g.vector];
|
|
2617
|
+
const [k, v, x, M] = i[g.matrix];
|
|
2618
|
+
return g.column === 0 ? [k, x] : [v, M];
|
|
2619
|
+
}, f = () => {
|
|
2620
|
+
const g = l.map((k) => {
|
|
2621
|
+
const [v, x] = r.toScreen(c(k));
|
|
2622
|
+
return `<circle cx="${v}" cy="${x}" r="${14 * r.scale}" fill="rgba(255,255,255,0.08)" stroke="rgba(255,255,255,0.55)" stroke-width="${1.5 * r.scale}" style="cursor:grab"/>`;
|
|
2623
|
+
});
|
|
2624
|
+
o.innerHTML = Kt(Lt(e, i)) + g.join(""), n.onChange?.({ ...i });
|
|
2625
|
+
}, u = (g, k) => {
|
|
2626
|
+
if (!(g in i)) throw new Error(`mathExplorable: no value "${g}"`);
|
|
2627
|
+
i[g] = k;
|
|
2628
|
+
const v = h.get(g);
|
|
2629
|
+
v && typeof k == "number" && (v.value = String(k), d.get(g).textContent = String(k)), f();
|
|
2630
|
+
}, h = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map();
|
|
2631
|
+
let y;
|
|
2632
|
+
if (n.sliders?.length) {
|
|
2633
|
+
y = s.createElement("div"), y.setAttribute("style", "display:flex;flex-wrap:wrap;gap:8px 18px;align-items:center;padding:8px 0;font:14px system-ui,sans-serif");
|
|
2634
|
+
for (const g of n.sliders) {
|
|
2635
|
+
const k = s.createElement("label");
|
|
2636
|
+
k.setAttribute("style", "display:flex;align-items:center;gap:6px");
|
|
2637
|
+
const v = s.createElement("input");
|
|
2638
|
+
v.type = "range", v.min = String(g.min), v.max = String(g.max), v.step = String(g.step ?? (g.max - g.min) / 100), v.value = String(i[g.value]), v.setAttribute("aria-label", g.label ?? g.value);
|
|
2639
|
+
const x = s.createElement("output");
|
|
2640
|
+
x.textContent = String(i[g.value]), v.addEventListener("input", () => u(g.value, Number(v.value))), k.append(g.label ?? g.value, v, x), y.appendChild(k), h.set(g.value, v), d.set(g.value, x);
|
|
2641
|
+
}
|
|
2642
|
+
t.appendChild(y);
|
|
2643
|
+
}
|
|
2644
|
+
const a = n.snap ?? 0.1, p = (g) => {
|
|
2645
|
+
const k = o.getBoundingClientRect(), v = (g.clientX - k.left) / (k.width || e.width) * e.width, x = (g.clientY - k.top) / (k.height || e.height) * e.height, [M, T] = r.toWorld([v, x]), P = (K) => a > 0 ? Math.round(K / a) * a : K;
|
|
2646
|
+
return [Number(P(M).toFixed(6)), Number(P(T).toFixed(6))];
|
|
2647
|
+
};
|
|
2648
|
+
let m;
|
|
2649
|
+
const b = (g) => {
|
|
2650
|
+
const k = o.getBoundingClientRect(), v = e.width / (k.width || e.width), x = [(g.clientX - k.left) * v, (g.clientY - k.top) * v];
|
|
2651
|
+
let M;
|
|
2652
|
+
for (const T of l) {
|
|
2653
|
+
const [P, K] = r.toScreen(c(T)), j = Math.hypot(P - x[0], K - x[1]);
|
|
2654
|
+
j < 30 * r.scale * Math.max(1, v) && (!M || j < M.distance) && (M = { handle: T, distance: j });
|
|
2655
|
+
}
|
|
2656
|
+
M && (m = M.handle, o.setPointerCapture?.(g.pointerId), g.preventDefault());
|
|
2657
|
+
}, w = (g) => {
|
|
2658
|
+
if (!m) return;
|
|
2659
|
+
const [k, v] = p(g);
|
|
2660
|
+
if ("vector" in m)
|
|
2661
|
+
u(m.vector, [k, v]);
|
|
2662
|
+
else {
|
|
2663
|
+
const [x, M, T, P] = i[m.matrix];
|
|
2664
|
+
u(m.matrix, m.column === 0 ? [k, M, v, P] : [x, k, T, v]);
|
|
2665
|
+
}
|
|
2666
|
+
}, $ = () => {
|
|
2667
|
+
m = void 0;
|
|
2668
|
+
};
|
|
2669
|
+
return o.addEventListener("pointerdown", b), o.addEventListener("pointermove", w), o.addEventListener("pointerup", $), o.addEventListener("pointercancel", $), f(), {
|
|
2670
|
+
get values() {
|
|
2671
|
+
return { ...i };
|
|
2672
|
+
},
|
|
2673
|
+
set: u,
|
|
2674
|
+
destroy() {
|
|
2675
|
+
o.removeEventListener("pointerdown", b), o.removeEventListener("pointermove", w), o.removeEventListener("pointerup", $), o.removeEventListener("pointercancel", $), y?.remove();
|
|
2676
|
+
}
|
|
2677
|
+
};
|
|
2678
|
+
}
|
|
2679
|
+
const wt = (t, e, n) => ({
|
|
2680
|
+
width: t.width ?? 600,
|
|
2681
|
+
height: t.height ?? 600,
|
|
2682
|
+
...t.theme ? { theme: t.theme } : {},
|
|
2683
|
+
...t.x ? { x: t.x } : {},
|
|
2684
|
+
values: e,
|
|
2685
|
+
items: n
|
|
2686
|
+
}), Qt = "\\begin{bmatrix} \\val{M.a} & \\val{M.b} \\\\ \\val{M.c} & \\val{M.d} \\end{bmatrix}", Jt = (t) => [(t.width ?? 600) * 0.05, (t.height ?? 600) * 0.1];
|
|
2687
|
+
function bi(t = {}) {
|
|
2688
|
+
return wt(t, { M: t.matrix ?? yt }, [
|
|
2689
|
+
{ id: "grid", kind: "grid", transform: "M", ghost: !0 },
|
|
2690
|
+
{ id: "square", kind: "polygon", transform: "M", color: "yellow", fillOpacity: 0.25 },
|
|
2691
|
+
{ id: "basis", kind: "basis", transform: "M" },
|
|
2692
|
+
{ id: "matrix", kind: "label", tex: Qt, at: Jt(t), screen: !0, align: "left" }
|
|
2693
|
+
]);
|
|
2694
|
+
}
|
|
2695
|
+
function vi(t = {}) {
|
|
2696
|
+
return wt(t, { M: t.matrix ?? yt }, [
|
|
2697
|
+
{ id: "grid", kind: "grid", transform: "M", ghost: !0 },
|
|
2698
|
+
{ id: "square", kind: "polygon", transform: "M", color: "yellow", fillOpacity: 0.35, label: "\\det = \\val{M.det}" },
|
|
2699
|
+
{ id: "basis", kind: "basis", transform: "M" },
|
|
2700
|
+
{ id: "matrix", kind: "label", tex: Qt, at: Jt(t), screen: !0, align: "left" }
|
|
2701
|
+
]);
|
|
2702
|
+
}
|
|
2703
|
+
function wi(t) {
|
|
2704
|
+
return wt(t, { M: yt, A: t.matrix }, [
|
|
2705
|
+
{ id: "grid", kind: "grid", transform: "M", ghost: !0 },
|
|
2706
|
+
{ id: "eigen", kind: "line", eigen: "A", color: "yellow", width: 3 },
|
|
2707
|
+
{ id: "basis", kind: "basis", transform: "M" },
|
|
2708
|
+
{ id: "matrix", kind: "label", tex: Qt, at: Jt(t), screen: !0, align: "left" }
|
|
2709
|
+
]);
|
|
2710
|
+
}
|
|
2711
|
+
function ki(t) {
|
|
2712
|
+
const e = [t.u[0] + t.v[0], t.u[1] + t.v[1]];
|
|
2713
|
+
return wt(t, { u: t.u, v: t.v, s: e }, [
|
|
2714
|
+
{ id: "grid", kind: "grid", ghost: !1, color: "grey", opacity: 0.5 },
|
|
2715
|
+
{ id: "axes", kind: "axes" },
|
|
2716
|
+
{ id: "arrowU", kind: "vector", to: "u", color: "green", label: "\\vec{u}", labelAt: "middle" },
|
|
2717
|
+
{ id: "arrowV", kind: "vector", from: "u", to: "s", color: "red", label: "\\vec{v}", labelAt: "middle" },
|
|
2718
|
+
{ id: "sum", kind: "vector", to: "s", color: "yellow", label: "\\vec{u} + \\vec{v}" }
|
|
2719
|
+
]);
|
|
2720
|
+
}
|
|
2721
|
+
function $i(t) {
|
|
2722
|
+
const e = [
|
|
2723
|
+
{ id: "axes", kind: "axes" },
|
|
2724
|
+
{ id: "spanU", kind: "line", along: "u", color: "green", dashed: !0 },
|
|
2725
|
+
{ id: "arrowU", kind: "vector", to: "u", color: "green", label: "\\vec{u}" }
|
|
2726
|
+
], n = { u: t.u };
|
|
2727
|
+
return t.v && (n.v = t.v, e.splice(2, 0, { id: "spanV", kind: "line", along: "v", color: "red", dashed: !0 }), e.push({ id: "arrowV", kind: "vector", to: "v", color: "red", label: "\\vec{v}" })), wt(t, n, e);
|
|
2728
|
+
}
|
|
2729
|
+
const dt = (t, e, n) => ({
|
|
2730
|
+
width: t.width ?? 600,
|
|
2731
|
+
height: t.height ?? 600,
|
|
2732
|
+
...t.theme ? { theme: t.theme } : {},
|
|
2733
|
+
...t.x ? { x: t.x } : {},
|
|
2734
|
+
...t.y ? { y: t.y } : {},
|
|
2735
|
+
values: e,
|
|
2736
|
+
items: [{ id: "axes", kind: "axes" }, { id: "f", kind: "function", expr: t.expr, color: "blue" }, ...n]
|
|
2737
|
+
});
|
|
2738
|
+
function xi(t) {
|
|
2739
|
+
return dt(t, { a: t.at ?? 1 }, [
|
|
2740
|
+
{ id: "tangent", kind: "tangent", expr: t.expr, at: "a", color: "yellow", label: "\\text{slope} = \\val{tangent.slope}" }
|
|
2741
|
+
]);
|
|
2742
|
+
}
|
|
2743
|
+
function Mi(t) {
|
|
2744
|
+
return dt(t, { a: t.at ?? 1, h: t.h ?? 2 }, [
|
|
2745
|
+
{ id: "secant", kind: "tangent", expr: t.expr, at: "a", h: "h", color: "yellow", label: "\\frac{\\Delta y}{\\Delta x} = \\val{secant.slope}" }
|
|
2746
|
+
]);
|
|
2747
|
+
}
|
|
2748
|
+
function Si(t) {
|
|
2749
|
+
const e = dt(t, { n: t.n ?? 4 }, [
|
|
2750
|
+
{ id: "sum", kind: "riemann", expr: t.expr, from: t.from, to: t.to, n: "n", rule: t.rule ?? "left", color: "teal" }
|
|
2751
|
+
]);
|
|
2752
|
+
return e.items.push(e.items.splice(1, 1)[0]), e.items.push({
|
|
2753
|
+
id: "readout",
|
|
2754
|
+
kind: "label",
|
|
2755
|
+
screen: !0,
|
|
2756
|
+
align: "left",
|
|
2757
|
+
size: 24,
|
|
2758
|
+
at: [(t.width ?? 600) * 0.05, (t.height ?? 600) * 0.08],
|
|
2759
|
+
tex: "n = \\val{sum.n:0} \\quad \\text{sum} = \\val{sum.sum:3} \\quad \\int = \\val{sum.exact:3}"
|
|
2760
|
+
}), e;
|
|
2761
|
+
}
|
|
2762
|
+
function Ei(t) {
|
|
2763
|
+
const e = dt(t, { b: t.to }, [
|
|
2764
|
+
{ id: "area", kind: "area", expr: t.expr, from: t.from, to: "b", color: "teal" }
|
|
2765
|
+
]);
|
|
2766
|
+
return e.items.push(e.items.splice(1, 1)[0]), e.items.push({
|
|
2767
|
+
id: "readout",
|
|
2768
|
+
kind: "label",
|
|
2769
|
+
screen: !0,
|
|
2770
|
+
align: "left",
|
|
2771
|
+
size: 26,
|
|
2772
|
+
at: [(t.width ?? 600) * 0.05, (t.height ?? 600) * 0.09],
|
|
2773
|
+
tex: `\\int_{${t.from}}^{\\val{b:2}} f(x)\\,dx = \\val{area.value:3}`
|
|
2774
|
+
}), e;
|
|
2775
|
+
}
|
|
2776
|
+
function Ai(t) {
|
|
2777
|
+
const e = dt(t, { order: t.order ?? 1 }, [
|
|
2778
|
+
{ id: "taylor", kind: "taylor", expr: t.expr, at: t.at ?? 0, order: "order", color: "yellow" },
|
|
2779
|
+
{ id: "centre", kind: "point", on: t.expr, x: t.at ?? 0, color: "yellow" }
|
|
2780
|
+
]);
|
|
2781
|
+
return e.items.push({
|
|
2782
|
+
id: "readout",
|
|
2783
|
+
kind: "label",
|
|
2784
|
+
screen: !0,
|
|
2785
|
+
align: "left",
|
|
2786
|
+
size: 26,
|
|
2787
|
+
at: [(t.width ?? 600) * 0.05, (t.height ?? 600) * 0.09],
|
|
2788
|
+
tex: "\\text{degree } \\val{taylor.degree:0}"
|
|
2789
|
+
}), e;
|
|
2790
|
+
}
|
|
2791
|
+
function Ti(t) {
|
|
2792
|
+
const e = dt(t, { p: t.from ?? t.at - 2 }, [
|
|
2793
|
+
{ id: "hole", kind: "point", at: [t.at, t.limit], hollow: !0, color: "blue", radius: 7 },
|
|
2794
|
+
{ id: "moving", kind: "point", on: t.expr, x: "p", color: "yellow" }
|
|
2795
|
+
]);
|
|
2796
|
+
return e.items.push({
|
|
2797
|
+
id: "readout",
|
|
2798
|
+
kind: "label",
|
|
2799
|
+
screen: !0,
|
|
2800
|
+
align: "left",
|
|
2801
|
+
size: 26,
|
|
2802
|
+
at: [(t.width ?? 600) * 0.05, (t.height ?? 600) * 0.09],
|
|
2803
|
+
tex: "f(\\val{moving.x:3}) = \\val{moving.y:4}"
|
|
2804
|
+
}), e;
|
|
2805
|
+
}
|
|
2806
|
+
function Oi(t) {
|
|
2807
|
+
return bt(t);
|
|
2808
|
+
}
|
|
2809
|
+
function Li(t, e = {}, n = {}) {
|
|
2810
|
+
return jn(Lt(bt(t), e), n);
|
|
2811
|
+
}
|
|
2812
|
+
export {
|
|
2813
|
+
Et as CONSTANTS,
|
|
2814
|
+
ot as ExpressionError,
|
|
2815
|
+
zt as FUNCTIONS,
|
|
2816
|
+
ue as ITEM_KINDS,
|
|
2817
|
+
tn as MATH_FONT,
|
|
2818
|
+
Ut as MATH_THEMES,
|
|
2819
|
+
Qt as MATRIX_TEX,
|
|
2820
|
+
Dt as SYMBOLS,
|
|
2821
|
+
Qe as TEX_COMMANDS,
|
|
2822
|
+
Ke as TexError,
|
|
2823
|
+
Ei as areaFigure,
|
|
2824
|
+
D as compile,
|
|
2825
|
+
Yt as compileDerivative,
|
|
2826
|
+
vi as determinantFigure,
|
|
2827
|
+
G as differentiate,
|
|
2828
|
+
jn as displayListToSvg,
|
|
2829
|
+
Kt as displayListToSvgContent,
|
|
2830
|
+
qn as drawDisplayList,
|
|
2831
|
+
wi as eigenFigure,
|
|
2832
|
+
at as evaluate,
|
|
2833
|
+
Ze as expressionNames,
|
|
2834
|
+
$t as expressionToString,
|
|
2835
|
+
se as integrate,
|
|
2836
|
+
Lt as layoutFigure,
|
|
2837
|
+
ut as layoutTex,
|
|
2838
|
+
Ti as limitFigure,
|
|
2839
|
+
bi as linearMapFigure,
|
|
2840
|
+
fi as mat2,
|
|
2841
|
+
gi as mathExplorable,
|
|
2842
|
+
Oi as mathFigure,
|
|
2843
|
+
Dn as mathFigureProps,
|
|
2844
|
+
Li as mathFigureToSvg,
|
|
2845
|
+
mi as mathSteps,
|
|
2846
|
+
pi as mathTarget,
|
|
2847
|
+
pn as morphEquation,
|
|
2848
|
+
yi as mountAllMath,
|
|
2849
|
+
li as mountMath,
|
|
2850
|
+
Ge as nthDerivative,
|
|
2851
|
+
At as parseExpression,
|
|
2852
|
+
Je as parseTex,
|
|
2853
|
+
Te as planeOf,
|
|
2854
|
+
Si as riemannFigure,
|
|
2855
|
+
Mi as secantFigure,
|
|
2856
|
+
$i as spanFigure,
|
|
2857
|
+
xi as tangentFigure,
|
|
2858
|
+
Ai as taylorFigure,
|
|
2859
|
+
vt as texToShapes,
|
|
2860
|
+
et as trimPolylines,
|
|
2861
|
+
bt as validateMathFigure,
|
|
2862
|
+
hi as vec2,
|
|
2863
|
+
ki as vectorSumFigure
|
|
2864
|
+
};
|