@celljs/ta-plot 0.0.0-stage → 4.15.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +12 -0
- package/README.md +11 -2
- package/extension/index.mjs +121 -0
- package/package.json +55 -4
- package/web/index.js +1320 -0
package/web/index.js
ADDED
|
@@ -0,0 +1,1320 @@
|
|
|
1
|
+
// web-src/plot-protocol.ts
|
|
2
|
+
var LANGUAGE = "ta-plot";
|
|
3
|
+
var ALIASES = ["plot"];
|
|
4
|
+
|
|
5
|
+
// ../ta-protocol/esm/common/language.js
|
|
6
|
+
var TaLanguageUtil;
|
|
7
|
+
(function(TaLanguageUtil2) {
|
|
8
|
+
TaLanguageUtil2.ALL = ["zh-CN", "zh-TW", "en-US", "ja-JP"];
|
|
9
|
+
TaLanguageUtil2.NAMES = { "zh-CN": "简体中文", "zh-TW": "繁體中文", "en-US": "English", "ja-JP": "日本語" };
|
|
10
|
+
function of(tag) {
|
|
11
|
+
const said = (tag !== null && tag !== undefined ? tag : "").toLowerCase().replace(/_/g, "-");
|
|
12
|
+
if (/^zh(\b|-)/.test(said)) {
|
|
13
|
+
return /-hant\b/.test(said) || !/-hans\b/.test(said) && /-(tw|hk|mo)\b/.test(said) ? "zh-TW" : "zh-CN";
|
|
14
|
+
}
|
|
15
|
+
return /^ja(\b|-)/.test(said) ? "ja-JP" : "en-US";
|
|
16
|
+
}
|
|
17
|
+
TaLanguageUtil2.of = of;
|
|
18
|
+
function server(env = typeof process === "undefined" ? {} : process.env) {
|
|
19
|
+
return of(env.TA_LANGUAGE || env.LC_ALL || env.LC_MESSAGES || env.LANG);
|
|
20
|
+
}
|
|
21
|
+
TaLanguageUtil2.server = server;
|
|
22
|
+
function pick(language, words) {
|
|
23
|
+
return words[of(language)];
|
|
24
|
+
}
|
|
25
|
+
TaLanguageUtil2.pick = pick;
|
|
26
|
+
function say(words) {
|
|
27
|
+
return words[server()];
|
|
28
|
+
}
|
|
29
|
+
TaLanguageUtil2.say = say;
|
|
30
|
+
function chinese(language) {
|
|
31
|
+
return of(language).startsWith("zh");
|
|
32
|
+
}
|
|
33
|
+
TaLanguageUtil2.chinese = chinese;
|
|
34
|
+
function cjk(language) {
|
|
35
|
+
return of(language) !== "en-US";
|
|
36
|
+
}
|
|
37
|
+
TaLanguageUtil2.cjk = cjk;
|
|
38
|
+
function list(language, items) {
|
|
39
|
+
return items.join(cjk(language) ? "、" : ", ");
|
|
40
|
+
}
|
|
41
|
+
TaLanguageUtil2.list = list;
|
|
42
|
+
})(TaLanguageUtil || (TaLanguageUtil = {}));
|
|
43
|
+
|
|
44
|
+
// web-src/i18n.ts
|
|
45
|
+
var EN = {
|
|
46
|
+
drawing: "Drawing the plot…",
|
|
47
|
+
failed: "This plot cannot be drawn: {reason}"
|
|
48
|
+
};
|
|
49
|
+
var ZH = {
|
|
50
|
+
drawing: "正在画图…",
|
|
51
|
+
failed: "这张图画不出来:{reason}"
|
|
52
|
+
};
|
|
53
|
+
var ZH_TW = {
|
|
54
|
+
drawing: "正在畫圖…",
|
|
55
|
+
failed: "這張圖畫不出來:{reason}"
|
|
56
|
+
};
|
|
57
|
+
var JA = {
|
|
58
|
+
drawing: "グラフを描画中…",
|
|
59
|
+
failed: "このグラフは描けません:{reason}"
|
|
60
|
+
};
|
|
61
|
+
var WORDS = { "zh-CN": ZH, "zh-TW": ZH_TW, "en-US": EN, "ja-JP": JA };
|
|
62
|
+
var PlotWords;
|
|
63
|
+
((PlotWords) => {
|
|
64
|
+
function of(language) {
|
|
65
|
+
return TaLanguageUtil.pick(language, WORDS);
|
|
66
|
+
}
|
|
67
|
+
PlotWords.of = of;
|
|
68
|
+
})(PlotWords ||= {});
|
|
69
|
+
|
|
70
|
+
// web-src/plot-expression.ts
|
|
71
|
+
var FUNCTIONS = new Map(Object.entries({
|
|
72
|
+
sin: [Math.sin, 1],
|
|
73
|
+
cos: [Math.cos, 1],
|
|
74
|
+
tan: [Math.tan, 1],
|
|
75
|
+
asin: [Math.asin, 1],
|
|
76
|
+
acos: [Math.acos, 1],
|
|
77
|
+
atan: [Math.atan, 1],
|
|
78
|
+
atan2: [Math.atan2, 2],
|
|
79
|
+
sinh: [Math.sinh, 1],
|
|
80
|
+
cosh: [Math.cosh, 1],
|
|
81
|
+
tanh: [Math.tanh, 1],
|
|
82
|
+
asinh: [Math.asinh, 1],
|
|
83
|
+
acosh: [Math.acosh, 1],
|
|
84
|
+
atanh: [Math.atanh, 1],
|
|
85
|
+
sqrt: [Math.sqrt, 1],
|
|
86
|
+
cbrt: [Math.cbrt, 1],
|
|
87
|
+
abs: [Math.abs, 1],
|
|
88
|
+
exp: [Math.exp, 1],
|
|
89
|
+
log: [Math.log, 1],
|
|
90
|
+
ln: [Math.log, 1],
|
|
91
|
+
log2: [Math.log2, 1],
|
|
92
|
+
log10: [Math.log10, 1],
|
|
93
|
+
pow: [Math.pow, 2],
|
|
94
|
+
min: [Math.min, undefined],
|
|
95
|
+
max: [Math.max, undefined],
|
|
96
|
+
floor: [Math.floor, 1],
|
|
97
|
+
ceil: [Math.ceil, 1],
|
|
98
|
+
round: [Math.round, 1],
|
|
99
|
+
sign: [Math.sign, 1],
|
|
100
|
+
hypot: [Math.hypot, undefined]
|
|
101
|
+
}));
|
|
102
|
+
var CONSTANTS = new Map(Object.entries({ pi: Math.PI, PI: Math.PI, e: Math.E, E: Math.E }));
|
|
103
|
+
var TOKEN = /\s*(?:(\d+(?:\.\d*)?(?:[eE][+-]?\d+)?|\.\d+(?:[eE][+-]?\d+)?)|([A-Za-z_]\w*)|(\*\*|[-+*/%^(),]))/y;
|
|
104
|
+
var MAX_LENGTH = 2000;
|
|
105
|
+
var PlotExpression;
|
|
106
|
+
((PlotExpression) => {
|
|
107
|
+
function compile(text, names) {
|
|
108
|
+
return new PlotParser(String(text), names).parse();
|
|
109
|
+
}
|
|
110
|
+
PlotExpression.compile = compile;
|
|
111
|
+
})(PlotExpression ||= {});
|
|
112
|
+
|
|
113
|
+
class PlotParser {
|
|
114
|
+
text;
|
|
115
|
+
names;
|
|
116
|
+
tokens;
|
|
117
|
+
at = 0;
|
|
118
|
+
uses = new Set;
|
|
119
|
+
constructor(text, names) {
|
|
120
|
+
this.text = text;
|
|
121
|
+
this.names = names;
|
|
122
|
+
this.tokens = PlotParser.tokenize(text);
|
|
123
|
+
}
|
|
124
|
+
parse() {
|
|
125
|
+
const evaluate = this.sum();
|
|
126
|
+
if (this.at < this.tokens.length) {
|
|
127
|
+
throw this.unexpected();
|
|
128
|
+
}
|
|
129
|
+
return { evaluate, uses: this.uses };
|
|
130
|
+
}
|
|
131
|
+
sum() {
|
|
132
|
+
let left = this.product();
|
|
133
|
+
for (let operator = this.operator("+", "-");operator; operator = this.operator("+", "-")) {
|
|
134
|
+
const a = left;
|
|
135
|
+
const b = this.product();
|
|
136
|
+
left = operator === "+" ? (scope) => a(scope) + b(scope) : (scope) => a(scope) - b(scope);
|
|
137
|
+
}
|
|
138
|
+
return left;
|
|
139
|
+
}
|
|
140
|
+
product() {
|
|
141
|
+
let left = this.unary();
|
|
142
|
+
for (let operator = this.operator("*", "/", "%");operator; operator = this.operator("*", "/", "%")) {
|
|
143
|
+
const a = left;
|
|
144
|
+
const b = this.unary();
|
|
145
|
+
left = operator === "*" ? (scope) => a(scope) * b(scope) : operator === "/" ? (scope) => a(scope) / b(scope) : (scope) => a(scope) % b(scope);
|
|
146
|
+
}
|
|
147
|
+
return left;
|
|
148
|
+
}
|
|
149
|
+
unary() {
|
|
150
|
+
const sign = this.operator("-", "+");
|
|
151
|
+
if (sign) {
|
|
152
|
+
const value = this.unary();
|
|
153
|
+
return sign === "-" ? (scope) => -value(scope) : value;
|
|
154
|
+
}
|
|
155
|
+
return this.power();
|
|
156
|
+
}
|
|
157
|
+
power() {
|
|
158
|
+
const base = this.primary();
|
|
159
|
+
if (this.operator("^", "**")) {
|
|
160
|
+
const exponent = this.unary();
|
|
161
|
+
return (scope) => base(scope) ** exponent(scope);
|
|
162
|
+
}
|
|
163
|
+
return base;
|
|
164
|
+
}
|
|
165
|
+
primary() {
|
|
166
|
+
const token = this.tokens[this.at++];
|
|
167
|
+
if (!token) {
|
|
168
|
+
throw new Error(`Unfinished expression: ${this.text}`);
|
|
169
|
+
}
|
|
170
|
+
if (token.kind === "number") {
|
|
171
|
+
const value = token.value;
|
|
172
|
+
return () => value;
|
|
173
|
+
}
|
|
174
|
+
if (token.kind === "operator") {
|
|
175
|
+
if (token.value !== "(") {
|
|
176
|
+
this.at--;
|
|
177
|
+
throw this.unexpected();
|
|
178
|
+
}
|
|
179
|
+
const inner = this.sum();
|
|
180
|
+
this.expect(")");
|
|
181
|
+
return inner;
|
|
182
|
+
}
|
|
183
|
+
return this.operator("(") ? this.call(token.value) : this.variable(token.value);
|
|
184
|
+
}
|
|
185
|
+
variable(name) {
|
|
186
|
+
const index = this.names.indexOf(name);
|
|
187
|
+
if (index >= 0) {
|
|
188
|
+
this.uses.add(name);
|
|
189
|
+
return (scope) => scope[index];
|
|
190
|
+
}
|
|
191
|
+
const constant = CONSTANTS.get(name);
|
|
192
|
+
if (constant !== undefined) {
|
|
193
|
+
const value = constant;
|
|
194
|
+
return () => value;
|
|
195
|
+
}
|
|
196
|
+
throw new Error(`Unknown name: ${name}`);
|
|
197
|
+
}
|
|
198
|
+
call(name) {
|
|
199
|
+
const known = FUNCTIONS.get(name);
|
|
200
|
+
if (!known) {
|
|
201
|
+
throw new Error(`Unknown function: ${name}`);
|
|
202
|
+
}
|
|
203
|
+
const [fn, arity] = known;
|
|
204
|
+
const args = [this.sum()];
|
|
205
|
+
while (this.operator(",")) {
|
|
206
|
+
args.push(this.sum());
|
|
207
|
+
}
|
|
208
|
+
this.expect(")");
|
|
209
|
+
if (arity !== undefined && args.length !== arity) {
|
|
210
|
+
throw new Error(`${name} takes ${arity} argument${arity === 1 ? "" : "s"}`);
|
|
211
|
+
}
|
|
212
|
+
if (args.length === 1) {
|
|
213
|
+
const [a] = args;
|
|
214
|
+
return (scope) => fn(a(scope));
|
|
215
|
+
}
|
|
216
|
+
if (args.length === 2) {
|
|
217
|
+
const [a, b] = args;
|
|
218
|
+
return (scope) => fn(a(scope), b(scope));
|
|
219
|
+
}
|
|
220
|
+
return (scope) => fn(...args.map((arg) => arg(scope)));
|
|
221
|
+
}
|
|
222
|
+
operator(...operators) {
|
|
223
|
+
const token = this.tokens[this.at];
|
|
224
|
+
if (token?.kind === "operator" && operators.includes(token.value)) {
|
|
225
|
+
this.at++;
|
|
226
|
+
return token.value;
|
|
227
|
+
}
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
expect(operator) {
|
|
231
|
+
if (!this.operator(operator)) {
|
|
232
|
+
throw this.unexpected(operator);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
unexpected(expected) {
|
|
236
|
+
const token = this.tokens[this.at];
|
|
237
|
+
const found = token ? `"${token.value}"` : "the end";
|
|
238
|
+
return new Error(`${expected ? `Expected "${expected}" but found` : "Unexpected"} ${found} in: ${this.text}`);
|
|
239
|
+
}
|
|
240
|
+
static tokenize(text) {
|
|
241
|
+
if (text.length > MAX_LENGTH) {
|
|
242
|
+
throw new Error("Expression too long");
|
|
243
|
+
}
|
|
244
|
+
const tokens = [];
|
|
245
|
+
let at = 0;
|
|
246
|
+
while (at < text.length) {
|
|
247
|
+
TOKEN.lastIndex = at;
|
|
248
|
+
const match = TOKEN.exec(text);
|
|
249
|
+
if (!match) {
|
|
250
|
+
if (!text.slice(at).trim()) {
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
throw new Error(`Unsupported character "${text.slice(at).trim()[0]}" in: ${text}`);
|
|
254
|
+
}
|
|
255
|
+
at = TOKEN.lastIndex;
|
|
256
|
+
if (match[1] !== undefined) {
|
|
257
|
+
tokens.push({ kind: "number", value: Number(match[1]) });
|
|
258
|
+
} else if (match[2] !== undefined) {
|
|
259
|
+
tokens.push({ kind: "name", value: match[2] });
|
|
260
|
+
} else {
|
|
261
|
+
tokens.push({ kind: "operator", value: match[3] });
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return tokens;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// web-src/plot-spec.ts
|
|
269
|
+
var PALETTE = ["#4f8cff", "#ff7a59", "#2bb673", "#b36bff", "#f5b400", "#00b8d9"];
|
|
270
|
+
var VARIABLES = ["x", "y", "s", "t"];
|
|
271
|
+
var MAX_CURVES = 12;
|
|
272
|
+
var MAX_SURFACES = 4;
|
|
273
|
+
var MAX_POINTS = 24;
|
|
274
|
+
var PlotSpec;
|
|
275
|
+
((PlotSpec) => {
|
|
276
|
+
function read(code) {
|
|
277
|
+
const spec = object(JSON.parse(code));
|
|
278
|
+
if (!spec) {
|
|
279
|
+
throw new Error("A plot is a JSON object");
|
|
280
|
+
}
|
|
281
|
+
const x = range(spec.x, [-5, 5]);
|
|
282
|
+
const raw = object(spec.params) ?? {};
|
|
283
|
+
const names = Object.keys(raw).filter((name) => /^[A-Za-z_]\w*$/.test(name) && !VARIABLES.includes(name));
|
|
284
|
+
const scope2 = [...VARIABLES, ...names];
|
|
285
|
+
const moving = new Set(["t"]);
|
|
286
|
+
const params = names.map((name, index) => {
|
|
287
|
+
const slider2 = PlotSpec.slider(raw[name]);
|
|
288
|
+
if (slider2) {
|
|
289
|
+
return { name, slider: slider2 };
|
|
290
|
+
}
|
|
291
|
+
const value = PlotExpression.compile(expression(raw[name], name), scope2);
|
|
292
|
+
for (const used of value.uses) {
|
|
293
|
+
if (names.indexOf(used) >= index && !PlotSpec.slider(raw[used])) {
|
|
294
|
+
throw new Error(`Param ${name} uses ${used}, which is not before it`);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if ([...value.uses].some((used) => moving.has(used))) {
|
|
298
|
+
moving.add(name);
|
|
299
|
+
}
|
|
300
|
+
return { name, value };
|
|
301
|
+
});
|
|
302
|
+
const moves = (compiled) => !!compiled && [...compiled.uses].some((used) => moving.has(used));
|
|
303
|
+
let color = 0;
|
|
304
|
+
const curves = list(spec.curves).slice(0, MAX_CURVES).map((curve) => {
|
|
305
|
+
const parametric = curve.x !== undefined || curve.z !== undefined;
|
|
306
|
+
const family = typeof curve.family === "string" ? { param: curve.family } : object(curve.family);
|
|
307
|
+
const index = family ? params.findIndex((param) => param.slider && param.name === family.param) : -1;
|
|
308
|
+
const count = family && Number.isFinite(family.count) ? Math.round(family.count) : 9;
|
|
309
|
+
return {
|
|
310
|
+
label: text(curve.label),
|
|
311
|
+
dash: !!curve.dash,
|
|
312
|
+
color: typeof curve.color === "string" && curve.color.length < 40 ? curve.color : PALETTE[color++ % PALETTE.length],
|
|
313
|
+
range: parametric ? range(curve.s, [-3, 3]) : x,
|
|
314
|
+
x: parametric ? PlotExpression.compile(expression(curve.x, "x"), scope2) : undefined,
|
|
315
|
+
y: PlotExpression.compile(expression(curve.y, "y"), scope2),
|
|
316
|
+
z: curve.z === undefined ? undefined : PlotExpression.compile(expression(curve.z, "z"), scope2),
|
|
317
|
+
family: index < 0 ? undefined : { index, count: Math.min(Math.max(count, 2), 41) }
|
|
318
|
+
};
|
|
319
|
+
});
|
|
320
|
+
const surfaces = list(spec.surfaces).slice(0, MAX_SURFACES).map((surface) => ({
|
|
321
|
+
label: text(surface.label),
|
|
322
|
+
color: typeof surface.color === "string" && surface.color.length < 40 ? surface.color : undefined,
|
|
323
|
+
z: PlotExpression.compile(expression(surface.z, "z"), scope2)
|
|
324
|
+
}));
|
|
325
|
+
if (!curves.length && !surfaces.length) {
|
|
326
|
+
throw new Error("A plot has no curves");
|
|
327
|
+
}
|
|
328
|
+
const points = list(spec.points).slice(0, MAX_POINTS).map((point) => ({
|
|
329
|
+
label: text(point.label),
|
|
330
|
+
x: PlotExpression.compile(expression(point.x, "x"), scope2),
|
|
331
|
+
y: PlotExpression.compile(expression(point.y, "y"), scope2),
|
|
332
|
+
z: point.z === undefined ? undefined : PlotExpression.compile(expression(point.z, "z"), scope2)
|
|
333
|
+
}));
|
|
334
|
+
const space = surfaces.length > 0 || curves.some((curve) => curve.z) || points.some((point) => point.z);
|
|
335
|
+
return {
|
|
336
|
+
title: text(spec.title),
|
|
337
|
+
x,
|
|
338
|
+
y: range(spec.y, [-5, 5]),
|
|
339
|
+
z: spec.z === undefined ? undefined : range(spec.z, [-5, 5]),
|
|
340
|
+
params,
|
|
341
|
+
curves,
|
|
342
|
+
surfaces,
|
|
343
|
+
points,
|
|
344
|
+
space,
|
|
345
|
+
dot: !!spec.dot,
|
|
346
|
+
moving: params.some((param) => moving.has(param.name)) || curves.some((curve) => moves(curve.x) || moves(curve.y) || moves(curve.z)) || surfaces.some((surface) => moves(surface.z)) || points.some((point) => moves(point.x) || moves(point.y) || moves(point.z))
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
PlotSpec.read = read;
|
|
350
|
+
function slider(value) {
|
|
351
|
+
const spec = object(value);
|
|
352
|
+
if (!spec || !Number.isFinite(spec.min) || !Number.isFinite(spec.max) || spec.min === spec.max) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const [min, max] = [spec.min, spec.max].sort((a, b) => a - b);
|
|
356
|
+
const step = Number.isFinite(spec.step) && spec.step > 0 ? spec.step : niceStep(max - min) / 10;
|
|
357
|
+
const start = Number.isFinite(spec.value) ? Math.min(Math.max(spec.value, min), max) : (min + max) / 2;
|
|
358
|
+
return { min, max, step, value: start, label: text(spec.label) };
|
|
359
|
+
}
|
|
360
|
+
PlotSpec.slider = slider;
|
|
361
|
+
function values(plot, t, sliders = []) {
|
|
362
|
+
const scope2 = [0, 0, 0, t];
|
|
363
|
+
plot.params.forEach((param, index) => {
|
|
364
|
+
scope2.push(param.slider ? sliders[index] ?? param.slider.value : Number.NaN);
|
|
365
|
+
});
|
|
366
|
+
plot.params.forEach((param, index) => {
|
|
367
|
+
if (param.value) {
|
|
368
|
+
scope2[VARIABLES.length + index] = param.value.evaluate(scope2);
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
return scope2.slice(VARIABLES.length);
|
|
372
|
+
}
|
|
373
|
+
PlotSpec.values = values;
|
|
374
|
+
function scope(x, y, s, t, values2) {
|
|
375
|
+
return [x, y, s, t, ...values2];
|
|
376
|
+
}
|
|
377
|
+
PlotSpec.scope = scope;
|
|
378
|
+
function niceStep(span) {
|
|
379
|
+
const raw = span / 8;
|
|
380
|
+
const power = 10 ** Math.floor(Math.log10(raw));
|
|
381
|
+
const unit = raw / power;
|
|
382
|
+
return (unit < 1.5 ? 1 : unit < 3.5 ? 2 : unit < 7.5 ? 5 : 10) * power;
|
|
383
|
+
}
|
|
384
|
+
PlotSpec.niceStep = niceStep;
|
|
385
|
+
function format(value) {
|
|
386
|
+
return Math.abs(value) < 0.000000001 ? "0" : String(Math.round(value * 100) / 100);
|
|
387
|
+
}
|
|
388
|
+
PlotSpec.format = format;
|
|
389
|
+
})(PlotSpec ||= {});
|
|
390
|
+
function object(value) {
|
|
391
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
|
|
392
|
+
}
|
|
393
|
+
function list(value) {
|
|
394
|
+
return Array.isArray(value) ? value.map(object).filter((item) => !!item) : [];
|
|
395
|
+
}
|
|
396
|
+
function text(value) {
|
|
397
|
+
return typeof value === "string" && value ? value : undefined;
|
|
398
|
+
}
|
|
399
|
+
function range(value, fallback) {
|
|
400
|
+
return Array.isArray(value) && value.length === 2 && value.every(Number.isFinite) && value[0] < value[1] ? [value[0], value[1]] : fallback;
|
|
401
|
+
}
|
|
402
|
+
function expression(value, what) {
|
|
403
|
+
if (typeof value === "string" || typeof value === "number" && Number.isFinite(value)) {
|
|
404
|
+
return value;
|
|
405
|
+
}
|
|
406
|
+
throw new Error(`No expression for ${what}`);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// web-src/plot-painter.ts
|
|
410
|
+
var DRAW_IN = 1.1;
|
|
411
|
+
var STAGGER = 0.25;
|
|
412
|
+
var SAMPLES = 480;
|
|
413
|
+
|
|
414
|
+
class PlotPainter {
|
|
415
|
+
context;
|
|
416
|
+
plot;
|
|
417
|
+
width = 0;
|
|
418
|
+
height = 0;
|
|
419
|
+
ink = (alpha) => `rgba(128,128,128,${alpha})`;
|
|
420
|
+
constructor(context, plot) {
|
|
421
|
+
this.context = context;
|
|
422
|
+
this.plot = plot;
|
|
423
|
+
}
|
|
424
|
+
get intro() {
|
|
425
|
+
return this.plot.curves.length * STAGGER + DRAW_IN + 0.6;
|
|
426
|
+
}
|
|
427
|
+
paint(t, sliders) {
|
|
428
|
+
const { context, plot, width, height } = this;
|
|
429
|
+
const values = PlotSpec.values(plot, t, sliders);
|
|
430
|
+
context.clearRect(0, 0, width, height);
|
|
431
|
+
context.font = "11px system-ui, sans-serif";
|
|
432
|
+
this.grid();
|
|
433
|
+
plot.curves.forEach((curve, index) => {
|
|
434
|
+
const shown = Math.min(Math.max((t - index * STAGGER) / DRAW_IN, 0), 1);
|
|
435
|
+
if (shown <= 0) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
const eased = 1 - (1 - shown) ** 3;
|
|
439
|
+
if (curve.family) {
|
|
440
|
+
const { index: param, count } = curve.family;
|
|
441
|
+
const slider = plot.params[param].slider;
|
|
442
|
+
for (let k = 0;k < count; k++) {
|
|
443
|
+
const others = sliders.slice();
|
|
444
|
+
others[param] = slider.min + (slider.max - slider.min) * k / (count - 1);
|
|
445
|
+
this.stroke(curve, PlotSpec.values(plot, t, others), t, eased, 0.28);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
this.stroke(curve, values, t, eased, 0);
|
|
449
|
+
if (plot.dot && !curve.dash && shown >= 1) {
|
|
450
|
+
this.dot(curve, values, t, index);
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
this.points(values, t);
|
|
454
|
+
return values;
|
|
455
|
+
}
|
|
456
|
+
x(x) {
|
|
457
|
+
const [x0, x1] = this.plot.x;
|
|
458
|
+
return (x - x0) / (x1 - x0) * this.width;
|
|
459
|
+
}
|
|
460
|
+
y(y) {
|
|
461
|
+
const [y0, y1] = this.plot.y;
|
|
462
|
+
return this.height - (y - y0) / (y1 - y0) * this.height;
|
|
463
|
+
}
|
|
464
|
+
grid() {
|
|
465
|
+
const { context, width, height } = this;
|
|
466
|
+
const [x0, x1] = this.plot.x;
|
|
467
|
+
const [y0, y1] = this.plot.y;
|
|
468
|
+
const step = PlotSpec.niceStep(Math.max(x1 - x0, y1 - y0));
|
|
469
|
+
context.lineWidth = 1;
|
|
470
|
+
context.strokeStyle = this.ink(0.08);
|
|
471
|
+
context.beginPath();
|
|
472
|
+
for (let gx = Math.ceil(x0 / step) * step;gx <= x1; gx += step) {
|
|
473
|
+
context.moveTo(this.x(gx), 0);
|
|
474
|
+
context.lineTo(this.x(gx), height);
|
|
475
|
+
}
|
|
476
|
+
for (let gy = Math.ceil(y0 / step) * step;gy <= y1; gy += step) {
|
|
477
|
+
context.moveTo(0, this.y(gy));
|
|
478
|
+
context.lineTo(width, this.y(gy));
|
|
479
|
+
}
|
|
480
|
+
context.stroke();
|
|
481
|
+
const axisX = Math.min(Math.max(this.x(0), 0), width);
|
|
482
|
+
const axisY = Math.min(Math.max(this.y(0), 0), height);
|
|
483
|
+
context.strokeStyle = this.ink(0.45);
|
|
484
|
+
context.beginPath();
|
|
485
|
+
context.moveTo(0, axisY);
|
|
486
|
+
context.lineTo(width, axisY);
|
|
487
|
+
context.moveTo(axisX, 0);
|
|
488
|
+
context.lineTo(axisX, height);
|
|
489
|
+
context.stroke();
|
|
490
|
+
context.fillStyle = this.ink(0.5);
|
|
491
|
+
context.textAlign = "center";
|
|
492
|
+
context.textBaseline = "top";
|
|
493
|
+
for (let gx = Math.ceil(x0 / step) * step;gx <= x1; gx += step) {
|
|
494
|
+
const px = this.x(gx);
|
|
495
|
+
if (Math.abs(gx) > 0.000000001 && px > 10 && px < width - 10) {
|
|
496
|
+
context.fillText(PlotSpec.format(gx), px, Math.min(axisY + 3, height - 14));
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
context.textAlign = "right";
|
|
500
|
+
context.textBaseline = "middle";
|
|
501
|
+
for (let gy = Math.ceil(y0 / step) * step;gy <= y1; gy += step) {
|
|
502
|
+
const py = this.y(gy);
|
|
503
|
+
if (Math.abs(gy) > 0.000000001 && py > 8 && py < height - 8) {
|
|
504
|
+
context.fillText(PlotSpec.format(gy), Math.max(axisX - 4, 24), py);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
stroke(curve, values, t, eased, faint) {
|
|
509
|
+
const { context } = this;
|
|
510
|
+
const [y0, y1] = this.plot.y;
|
|
511
|
+
const span = y1 - y0;
|
|
512
|
+
const [s0, s1] = curve.range;
|
|
513
|
+
const count = Math.max(2, Math.round(SAMPLES * eased));
|
|
514
|
+
const scope = PlotSpec.scope(0, 0, 0, t, values);
|
|
515
|
+
context.save();
|
|
516
|
+
context.strokeStyle = curve.color;
|
|
517
|
+
context.lineWidth = curve.dash || faint ? 1.25 : 2.5;
|
|
518
|
+
context.lineJoin = "round";
|
|
519
|
+
context.lineCap = "round";
|
|
520
|
+
if (faint) {
|
|
521
|
+
context.globalAlpha = faint;
|
|
522
|
+
}
|
|
523
|
+
if (curve.dash) {
|
|
524
|
+
context.setLineDash([6, 5]);
|
|
525
|
+
} else if (!faint) {
|
|
526
|
+
context.shadowColor = curve.color;
|
|
527
|
+
context.shadowBlur = 6;
|
|
528
|
+
}
|
|
529
|
+
context.beginPath();
|
|
530
|
+
let open = false;
|
|
531
|
+
let last;
|
|
532
|
+
for (let i = 0;i <= count; i++) {
|
|
533
|
+
const s = s0 + (s1 - s0) * eased * i / count;
|
|
534
|
+
scope[0] = s;
|
|
535
|
+
scope[2] = s;
|
|
536
|
+
const x = curve.x ? curve.x.evaluate(scope) : s;
|
|
537
|
+
const y = curve.y.evaluate(scope);
|
|
538
|
+
if (!Number.isFinite(x) || !Number.isFinite(y) || Math.abs(y - y0 - span / 2) > span * 4) {
|
|
539
|
+
open = false;
|
|
540
|
+
last = undefined;
|
|
541
|
+
continue;
|
|
542
|
+
}
|
|
543
|
+
if (open && last !== undefined && Math.abs(y - last) > span) {
|
|
544
|
+
open = false;
|
|
545
|
+
}
|
|
546
|
+
if (open) {
|
|
547
|
+
context.lineTo(this.x(x), this.y(y));
|
|
548
|
+
} else {
|
|
549
|
+
context.moveTo(this.x(x), this.y(y));
|
|
550
|
+
}
|
|
551
|
+
open = true;
|
|
552
|
+
last = y;
|
|
553
|
+
}
|
|
554
|
+
context.stroke();
|
|
555
|
+
context.restore();
|
|
556
|
+
}
|
|
557
|
+
dot(curve, values, t, index) {
|
|
558
|
+
const { context } = this;
|
|
559
|
+
const [s0, s1] = curve.range;
|
|
560
|
+
const s = s0 + (s1 - s0) * (0.5 + 0.5 * Math.sin(t * 0.9 + index * 1.3));
|
|
561
|
+
const scope = PlotSpec.scope(s, 0, s, t, values);
|
|
562
|
+
const x = curve.x ? curve.x.evaluate(scope) : s;
|
|
563
|
+
const y = curve.y.evaluate(scope);
|
|
564
|
+
if (!Number.isFinite(x) || !Number.isFinite(y)) {
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
context.save();
|
|
568
|
+
context.fillStyle = curve.color;
|
|
569
|
+
context.shadowColor = curve.color;
|
|
570
|
+
context.shadowBlur = 14;
|
|
571
|
+
context.beginPath();
|
|
572
|
+
context.arc(this.x(x), this.y(y), 5, 0, Math.PI * 2);
|
|
573
|
+
context.fill();
|
|
574
|
+
context.restore();
|
|
575
|
+
}
|
|
576
|
+
points(values, t) {
|
|
577
|
+
const { context, plot, width } = this;
|
|
578
|
+
const settled = Math.min(Math.max((t - plot.curves.length * STAGGER) / 0.6, 0), 1);
|
|
579
|
+
if (settled <= 0) {
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
const scope = PlotSpec.scope(0, 0, 0, t, values);
|
|
583
|
+
context.globalAlpha = settled;
|
|
584
|
+
for (const point of plot.points) {
|
|
585
|
+
const x = point.x.evaluate(scope);
|
|
586
|
+
const y = point.y.evaluate(scope);
|
|
587
|
+
if (!Number.isFinite(x) || !Number.isFinite(y)) {
|
|
588
|
+
continue;
|
|
589
|
+
}
|
|
590
|
+
const px = this.x(x);
|
|
591
|
+
const py = this.y(y);
|
|
592
|
+
context.fillStyle = this.ink(0.85);
|
|
593
|
+
context.beginPath();
|
|
594
|
+
context.arc(px, py, 3.5, 0, Math.PI * 2);
|
|
595
|
+
context.fill();
|
|
596
|
+
if (point.label) {
|
|
597
|
+
const right = px > width - 80;
|
|
598
|
+
context.textAlign = right ? "right" : "left";
|
|
599
|
+
context.textBaseline = "bottom";
|
|
600
|
+
context.fillText(point.label, px + (right ? -6 : 6), py - 4);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
context.globalAlpha = 1;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
// web-src/plot-scene.ts
|
|
608
|
+
var GRID = 56;
|
|
609
|
+
var SAMPLES2 = 360;
|
|
610
|
+
var HEIGHT = 0.75;
|
|
611
|
+
var RAMP = ["#3b4cc0", "#4f8cff", "#2bb6a3", "#9ccc3c", "#f5b400", "#ff7a59"];
|
|
612
|
+
|
|
613
|
+
class PlotScene {
|
|
614
|
+
context;
|
|
615
|
+
plot;
|
|
616
|
+
input;
|
|
617
|
+
THREE;
|
|
618
|
+
root;
|
|
619
|
+
controls;
|
|
620
|
+
surfaces = [];
|
|
621
|
+
curves = [];
|
|
622
|
+
points = [];
|
|
623
|
+
dots = [];
|
|
624
|
+
labels = [];
|
|
625
|
+
frame;
|
|
626
|
+
z;
|
|
627
|
+
drawn = -1;
|
|
628
|
+
shownAt = 0;
|
|
629
|
+
constructor(context, plot, input) {
|
|
630
|
+
this.context = context;
|
|
631
|
+
this.plot = plot;
|
|
632
|
+
this.input = input;
|
|
633
|
+
const THREE = context.THREE;
|
|
634
|
+
this.THREE = THREE;
|
|
635
|
+
const { renderer, scene, element } = context;
|
|
636
|
+
renderer.localClippingEnabled = true;
|
|
637
|
+
this.z = plot.z ?? PlotScene.reach(plot, input.sliders);
|
|
638
|
+
const camera = context.camera;
|
|
639
|
+
camera.fov = 40;
|
|
640
|
+
camera.near = 0.05;
|
|
641
|
+
camera.far = 100;
|
|
642
|
+
camera.position.set(2.7, 2.1, 3.1);
|
|
643
|
+
camera.lookAt(0, 0, 0);
|
|
644
|
+
camera.updateProjectionMatrix();
|
|
645
|
+
this.controls = new context.OrbitControls(camera, element);
|
|
646
|
+
this.controls.enableDamping = true;
|
|
647
|
+
this.controls.enablePan = false;
|
|
648
|
+
this.controls.minDistance = 1.5;
|
|
649
|
+
this.controls.maxDistance = 9;
|
|
650
|
+
this.controls.autoRotateSpeed = 0.6;
|
|
651
|
+
this.controls.addEventListener("start", () => {
|
|
652
|
+
this.controls.autoRotate = false;
|
|
653
|
+
});
|
|
654
|
+
this.controls.enableZoom = false;
|
|
655
|
+
element.addEventListener("wheel", this.wheel, { passive: false });
|
|
656
|
+
scene.add(new THREE.HemisphereLight(16777215, 4478310, 1.6));
|
|
657
|
+
const sun = new THREE.DirectionalLight(16777215, 1.4);
|
|
658
|
+
sun.position.set(2, 4, 3);
|
|
659
|
+
scene.add(sun);
|
|
660
|
+
this.root = new THREE.Group;
|
|
661
|
+
scene.add(this.root);
|
|
662
|
+
this.frame = new THREE.Group;
|
|
663
|
+
this.root.add(this.frame);
|
|
664
|
+
this.build();
|
|
665
|
+
this.page(context.page);
|
|
666
|
+
}
|
|
667
|
+
at(x, y, z, target) {
|
|
668
|
+
const [x0, x1] = this.plot.x;
|
|
669
|
+
const [y0, y1] = this.plot.y;
|
|
670
|
+
const [z0, z1] = this.z;
|
|
671
|
+
return target.set((x - x0) / (x1 - x0) * 2 - 1, ((z - z0) / (z1 - z0) * 2 - 1) * HEIGHT, -((y - y0) / (y1 - y0) * 2 - 1));
|
|
672
|
+
}
|
|
673
|
+
build() {
|
|
674
|
+
const { THREE, plot } = this;
|
|
675
|
+
const clipping = [new THREE.Plane(new THREE.Vector3(0, -1, 0), HEIGHT + 0.001), new THREE.Plane(new THREE.Vector3(0, 1, 0), HEIGHT + 0.001)];
|
|
676
|
+
for (const surface of plot.surfaces) {
|
|
677
|
+
const geometry = new THREE.BufferGeometry;
|
|
678
|
+
geometry.setAttribute("position", new THREE.BufferAttribute(new Float32Array(GRID * GRID * 3), 3));
|
|
679
|
+
geometry.setAttribute("color", new THREE.BufferAttribute(new Float32Array(GRID * GRID * 3), 3));
|
|
680
|
+
const index = [];
|
|
681
|
+
for (let i = 0;i < GRID - 1; i++) {
|
|
682
|
+
for (let j = 0;j < GRID - 1; j++) {
|
|
683
|
+
const a = i * GRID + j;
|
|
684
|
+
index.push(a, a + GRID, a + 1, a + 1, a + GRID, a + GRID + 1);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
geometry.setIndex(index);
|
|
688
|
+
const colored = !surface.color;
|
|
689
|
+
const material = new THREE.MeshStandardMaterial({
|
|
690
|
+
color: surface.color ?? 16777215,
|
|
691
|
+
vertexColors: colored,
|
|
692
|
+
side: THREE.DoubleSide,
|
|
693
|
+
roughness: 0.55,
|
|
694
|
+
metalness: 0.05,
|
|
695
|
+
transparent: true,
|
|
696
|
+
opacity: 0.92,
|
|
697
|
+
clippingPlanes: clipping
|
|
698
|
+
});
|
|
699
|
+
const mesh = new THREE.Mesh(geometry, material);
|
|
700
|
+
const wire = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ color: 0, wireframe: true, transparent: true, opacity: 0.08, clippingPlanes: clipping }));
|
|
701
|
+
mesh.add(wire);
|
|
702
|
+
this.root.add(mesh);
|
|
703
|
+
this.surfaces.push({ geometry, colored, mesh });
|
|
704
|
+
}
|
|
705
|
+
for (const curve of plot.curves) {
|
|
706
|
+
const geometry = new THREE.BufferGeometry;
|
|
707
|
+
geometry.setAttribute("position", new THREE.BufferAttribute(new Float32Array(SAMPLES2 * 2 * 3), 3));
|
|
708
|
+
const material = curve.dash ? new THREE.LineDashedMaterial({ color: curve.color, dashSize: 0.06, gapSize: 0.05, clippingPlanes: clipping }) : new THREE.LineBasicMaterial({ color: curve.color, clippingPlanes: clipping });
|
|
709
|
+
const line = new THREE.LineSegments(geometry, material);
|
|
710
|
+
this.root.add(line);
|
|
711
|
+
this.curves.push({ geometry, line });
|
|
712
|
+
if (plot.dot && !curve.dash) {
|
|
713
|
+
const dot = new THREE.Mesh(new THREE.SphereGeometry(0.035, 16, 12), new THREE.MeshBasicMaterial({ color: curve.color }));
|
|
714
|
+
dot.visible = false;
|
|
715
|
+
this.root.add(dot);
|
|
716
|
+
this.dots.push(dot);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
for (const point of plot.points) {
|
|
720
|
+
const mesh = new THREE.Mesh(new THREE.SphereGeometry(0.028, 16, 12), new THREE.MeshBasicMaterial({ color: 16777215 }));
|
|
721
|
+
mesh.visible = false;
|
|
722
|
+
this.root.add(mesh);
|
|
723
|
+
this.points.push(mesh);
|
|
724
|
+
if (point.label) {
|
|
725
|
+
const label = PlotScene.label(THREE, point.label);
|
|
726
|
+
mesh.add(label);
|
|
727
|
+
label.position.set(0, 0.12, 0);
|
|
728
|
+
this.labels.push(label);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
drawFrame(page) {
|
|
733
|
+
const { THREE, frame, plot } = this;
|
|
734
|
+
for (const child of [...frame.children]) {
|
|
735
|
+
frame.remove(child);
|
|
736
|
+
PlotScene.dispose(child);
|
|
737
|
+
}
|
|
738
|
+
const ink = page.colors.text;
|
|
739
|
+
const edges = new THREE.LineSegments(new THREE.EdgesGeometry(new THREE.BoxGeometry(2, HEIGHT * 2, 2)), new THREE.LineBasicMaterial({ color: ink, transparent: true, opacity: 0.22 }));
|
|
740
|
+
frame.add(edges);
|
|
741
|
+
const floor = [];
|
|
742
|
+
const [x0, x1] = plot.x;
|
|
743
|
+
const [y0, y1] = plot.y;
|
|
744
|
+
const step = PlotSpec.niceStep(Math.max(x1 - x0, y1 - y0));
|
|
745
|
+
const point = new THREE.Vector3;
|
|
746
|
+
for (let gx = Math.ceil(x0 / step) * step;gx <= x1 + 0.000000001; gx += step) {
|
|
747
|
+
floor.push(...this.at(gx, y0, this.z[0], point).toArray(), ...this.at(gx, y1, this.z[0], point).toArray());
|
|
748
|
+
}
|
|
749
|
+
for (let gy = Math.ceil(y0 / step) * step;gy <= y1 + 0.000000001; gy += step) {
|
|
750
|
+
floor.push(...this.at(x0, gy, this.z[0], point).toArray(), ...this.at(x1, gy, this.z[0], point).toArray());
|
|
751
|
+
}
|
|
752
|
+
const grid = new THREE.BufferGeometry;
|
|
753
|
+
grid.setAttribute("position", new THREE.Float32BufferAttribute(floor, 3));
|
|
754
|
+
frame.add(new THREE.LineSegments(grid, new THREE.LineBasicMaterial({ color: ink, transparent: true, opacity: 0.1 })));
|
|
755
|
+
const [z0, z1] = this.z;
|
|
756
|
+
const color = `#${ink.toString(16).padStart(6, "0")}`;
|
|
757
|
+
const marks = [
|
|
758
|
+
["x", x1, y0, z0],
|
|
759
|
+
["y", x1, y1, z0],
|
|
760
|
+
["z", x0, y0, z1],
|
|
761
|
+
[PlotSpec.format(x0), x0, y0, z0],
|
|
762
|
+
[PlotSpec.format(y1), x0, y1, z0],
|
|
763
|
+
[PlotSpec.format(z0), x0, y1, z0],
|
|
764
|
+
[PlotSpec.format(z1), x0, y1, z1]
|
|
765
|
+
];
|
|
766
|
+
const offsets = { x: [0.16, 0, 0.08], y: [0.1, 0, -0.14], z: [-0.08, 0.14, 0.08] };
|
|
767
|
+
marks.forEach(([name, x, y, z], index) => {
|
|
768
|
+
const label = PlotScene.label(THREE, name, color, index < 3 ? 1 : 0.8);
|
|
769
|
+
this.at(x, y, z, label.position);
|
|
770
|
+
const [dx, dy, dz] = offsets[name] ?? [-0.1, 0, 0.08];
|
|
771
|
+
label.position.add(new THREE.Vector3(dx, dy, dz));
|
|
772
|
+
frame.add(label);
|
|
773
|
+
});
|
|
774
|
+
for (const mesh of this.points) {
|
|
775
|
+
mesh.material.color.setHex(ink);
|
|
776
|
+
}
|
|
777
|
+
for (const label of this.labels) {
|
|
778
|
+
const old = label.material.map;
|
|
779
|
+
label.material.map = PlotScene.texture(THREE, PlotScene.textOf(label), color);
|
|
780
|
+
old?.dispose();
|
|
781
|
+
}
|
|
782
|
+
for (const { mesh } of this.surfaces) {
|
|
783
|
+
mesh.children[0].material.color.setHex(ink);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
draw(elapsed) {
|
|
787
|
+
const { plot } = this;
|
|
788
|
+
const values = PlotSpec.values(plot, elapsed, this.input.sliders);
|
|
789
|
+
const rise = PlotScene.eased(elapsed / DRAW_IN);
|
|
790
|
+
const point = new this.THREE.Vector3;
|
|
791
|
+
const [x0, x1] = plot.x;
|
|
792
|
+
const [y0, y1] = plot.y;
|
|
793
|
+
const [z0, z1] = this.z;
|
|
794
|
+
const middle = (z0 + z1) / 2;
|
|
795
|
+
plot.surfaces.forEach((surface, index) => {
|
|
796
|
+
const { geometry, colored } = this.surfaces[index];
|
|
797
|
+
const positions = geometry.getAttribute("position");
|
|
798
|
+
const colors = geometry.getAttribute("color");
|
|
799
|
+
const scope = PlotSpec.scope(0, 0, 0, elapsed, values);
|
|
800
|
+
const color = new this.THREE.Color;
|
|
801
|
+
for (let i = 0;i < GRID; i++) {
|
|
802
|
+
for (let j = 0;j < GRID; j++) {
|
|
803
|
+
const x = x0 + (x1 - x0) * j / (GRID - 1);
|
|
804
|
+
const y = y0 + (y1 - y0) * i / (GRID - 1);
|
|
805
|
+
scope[0] = x;
|
|
806
|
+
scope[1] = y;
|
|
807
|
+
const value = surface.z.evaluate(scope);
|
|
808
|
+
const z = Number.isFinite(value) ? Math.max(Math.min(value, z1 + (z1 - z0) * 4), z0 - (z1 - z0) * 4) : z1 + (z1 - z0) * 4;
|
|
809
|
+
this.at(x, y, middle + (z - middle) * rise, point);
|
|
810
|
+
positions.setXYZ(i * GRID + j, point.x, point.y, point.z);
|
|
811
|
+
if (colored) {
|
|
812
|
+
PlotScene.ramp((z - z0) / (z1 - z0), color);
|
|
813
|
+
colors.setXYZ(i * GRID + j, color.r, color.g, color.b);
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
positions.needsUpdate = true;
|
|
818
|
+
colors.needsUpdate = colored;
|
|
819
|
+
geometry.computeVertexNormals();
|
|
820
|
+
geometry.computeBoundingSphere();
|
|
821
|
+
});
|
|
822
|
+
plot.curves.forEach((curve, index) => {
|
|
823
|
+
const shown = PlotScene.eased((elapsed - index * STAGGER) / DRAW_IN);
|
|
824
|
+
const { geometry, line } = this.curves[index];
|
|
825
|
+
const positions = geometry.getAttribute("position");
|
|
826
|
+
const [s0, s1] = curve.range;
|
|
827
|
+
const scope = PlotSpec.scope(0, 0, 0, elapsed, values);
|
|
828
|
+
let count = 0;
|
|
829
|
+
let last;
|
|
830
|
+
for (let i = 0;i < SAMPLES2; i++) {
|
|
831
|
+
const s = s0 + (s1 - s0) * shown * i / (SAMPLES2 - 1);
|
|
832
|
+
const next = this.curvePoint(curve, scope, s, new this.THREE.Vector3);
|
|
833
|
+
if (next && last && last.distanceTo(next) < 1) {
|
|
834
|
+
positions.setXYZ(count++, last.x, last.y, last.z);
|
|
835
|
+
positions.setXYZ(count++, next.x, next.y, next.z);
|
|
836
|
+
}
|
|
837
|
+
last = next;
|
|
838
|
+
}
|
|
839
|
+
geometry.setDrawRange(0, count);
|
|
840
|
+
positions.needsUpdate = true;
|
|
841
|
+
geometry.computeBoundingSphere();
|
|
842
|
+
if (curve.dash) {
|
|
843
|
+
line.computeLineDistances();
|
|
844
|
+
}
|
|
845
|
+
});
|
|
846
|
+
let dot = 0;
|
|
847
|
+
plot.curves.forEach((curve, index) => {
|
|
848
|
+
if (!plot.dot || curve.dash) {
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
const mesh = this.dots[dot++];
|
|
852
|
+
const [s0, s1] = curve.range;
|
|
853
|
+
const s = s0 + (s1 - s0) * (0.5 + 0.5 * Math.sin(elapsed * 0.9 + index * 1.3));
|
|
854
|
+
const at = this.curvePoint(curve, PlotSpec.scope(0, 0, 0, elapsed, values), s, mesh.position);
|
|
855
|
+
mesh.visible = !!at && elapsed > plot.curves.length * STAGGER + DRAW_IN;
|
|
856
|
+
});
|
|
857
|
+
const settled = elapsed > plot.curves.length * STAGGER + DRAW_IN * 0.6;
|
|
858
|
+
plot.points.forEach((point2, index) => {
|
|
859
|
+
const scope = PlotSpec.scope(0, 0, 0, elapsed, values);
|
|
860
|
+
const x = point2.x.evaluate(scope);
|
|
861
|
+
const y = point2.y.evaluate(scope);
|
|
862
|
+
const z = point2.z ? point2.z.evaluate(scope) : this.z[0];
|
|
863
|
+
const mesh = this.points[index];
|
|
864
|
+
mesh.visible = settled && [x, y, z].every(Number.isFinite);
|
|
865
|
+
this.at(x, y, z, mesh.position);
|
|
866
|
+
});
|
|
867
|
+
return values;
|
|
868
|
+
}
|
|
869
|
+
curvePoint(curve, scope, s, target) {
|
|
870
|
+
scope[0] = s;
|
|
871
|
+
scope[2] = s;
|
|
872
|
+
const x = curve.x ? curve.x.evaluate(scope) : s;
|
|
873
|
+
const y = curve.y.evaluate(scope);
|
|
874
|
+
const z = curve.z ? curve.z.evaluate(scope) : this.z[0];
|
|
875
|
+
if (![x, y, z].every(Number.isFinite)) {
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
878
|
+
return this.at(x, y, z, target);
|
|
879
|
+
}
|
|
880
|
+
update(_seconds, elapsed) {
|
|
881
|
+
const { plot, input } = this;
|
|
882
|
+
const intro = elapsed < plot.curves.length * STAGGER + DRAW_IN + 0.6;
|
|
883
|
+
if (intro || plot.moving || plot.dot || input.dirty || this.drawn < 0) {
|
|
884
|
+
input.dirty = false;
|
|
885
|
+
const values = this.draw(elapsed);
|
|
886
|
+
this.drawn = elapsed;
|
|
887
|
+
if (values.length && elapsed - this.shownAt > 0.1) {
|
|
888
|
+
this.shownAt = elapsed;
|
|
889
|
+
input.shown(values);
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
this.controls.update();
|
|
893
|
+
}
|
|
894
|
+
page(page) {
|
|
895
|
+
this.controls.autoRotate = !page.reduceMotion;
|
|
896
|
+
this.drawFrame(page);
|
|
897
|
+
}
|
|
898
|
+
dispose() {
|
|
899
|
+
this.context.element.removeEventListener("wheel", this.wheel);
|
|
900
|
+
this.controls.dispose();
|
|
901
|
+
}
|
|
902
|
+
wheel = (event) => {
|
|
903
|
+
if (!event.ctrlKey) {
|
|
904
|
+
return;
|
|
905
|
+
}
|
|
906
|
+
event.preventDefault();
|
|
907
|
+
const camera = this.context.camera;
|
|
908
|
+
const distance = Math.min(Math.max(camera.position.length() * Math.exp(event.deltaY * 0.01), this.controls.minDistance), this.controls.maxDistance);
|
|
909
|
+
camera.position.setLength(distance);
|
|
910
|
+
};
|
|
911
|
+
static reach(plot, sliders) {
|
|
912
|
+
const values = PlotSpec.values(plot, 0, sliders);
|
|
913
|
+
const found = [];
|
|
914
|
+
const [x0, x1] = plot.x;
|
|
915
|
+
const [y0, y1] = plot.y;
|
|
916
|
+
const scope = PlotSpec.scope(0, 0, 0, 0, values);
|
|
917
|
+
for (const surface of plot.surfaces) {
|
|
918
|
+
for (let i = 0;i <= 24; i++) {
|
|
919
|
+
for (let j = 0;j <= 24; j++) {
|
|
920
|
+
scope[0] = x0 + (x1 - x0) * j / 24;
|
|
921
|
+
scope[1] = y0 + (y1 - y0) * i / 24;
|
|
922
|
+
found.push(surface.z.evaluate(scope));
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
for (const curve of plot.curves) {
|
|
927
|
+
const [s0, s1] = curve.range;
|
|
928
|
+
for (let i = 0;i <= 100 && curve.z; i++) {
|
|
929
|
+
scope[0] = scope[2] = s0 + (s1 - s0) * i / 100;
|
|
930
|
+
found.push(curve.z.evaluate(scope));
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
for (const point of plot.points) {
|
|
934
|
+
found.push(point.z?.evaluate(scope) ?? Number.NaN);
|
|
935
|
+
}
|
|
936
|
+
const finite = found.filter(Number.isFinite).sort((a, b) => a - b);
|
|
937
|
+
if (!finite.length) {
|
|
938
|
+
return [-1, 1];
|
|
939
|
+
}
|
|
940
|
+
let low = finite[Math.floor(finite.length * 0.02)];
|
|
941
|
+
let high = finite[Math.ceil(finite.length * 0.98) - 1];
|
|
942
|
+
if (high - low < 0.000001) {
|
|
943
|
+
low -= 1;
|
|
944
|
+
high += 1;
|
|
945
|
+
}
|
|
946
|
+
const pad = (high - low) * 0.08;
|
|
947
|
+
return [low - pad, high + pad];
|
|
948
|
+
}
|
|
949
|
+
static ramp(height, target) {
|
|
950
|
+
const at = Math.min(Math.max(height, 0), 1) * (RAMP.length - 1);
|
|
951
|
+
const index = Math.min(Math.floor(at), RAMP.length - 2);
|
|
952
|
+
const low = target.set(RAMP[index]);
|
|
953
|
+
const r = low.r;
|
|
954
|
+
const g = low.g;
|
|
955
|
+
const b = low.b;
|
|
956
|
+
target.set(RAMP[index + 1]);
|
|
957
|
+
const k = at - index;
|
|
958
|
+
return target.setRGB(r + (target.r - r) * k, g + (target.g - g) * k, b + (target.b - b) * k);
|
|
959
|
+
}
|
|
960
|
+
static eased(shown) {
|
|
961
|
+
const clamped = Math.min(Math.max(shown, 0), 1);
|
|
962
|
+
return 1 - (1 - clamped) ** 3;
|
|
963
|
+
}
|
|
964
|
+
static label(THREE, text2, color = "#888888", scale = 1) {
|
|
965
|
+
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({ map: PlotScene.texture(THREE, text2, color), depthTest: false, transparent: true }));
|
|
966
|
+
sprite.userData.text = text2;
|
|
967
|
+
const height = 0.13 * scale;
|
|
968
|
+
const image = sprite.material.map?.image;
|
|
969
|
+
sprite.scale.set(height * image.width / image.height, height, 1);
|
|
970
|
+
sprite.renderOrder = 10;
|
|
971
|
+
return sprite;
|
|
972
|
+
}
|
|
973
|
+
static textOf(sprite) {
|
|
974
|
+
return String(sprite.userData.text ?? "");
|
|
975
|
+
}
|
|
976
|
+
static texture(THREE, text2, color) {
|
|
977
|
+
const canvas = document.createElement("canvas");
|
|
978
|
+
const context = canvas.getContext("2d");
|
|
979
|
+
const font = "600 40px system-ui, sans-serif";
|
|
980
|
+
if (context) {
|
|
981
|
+
context.font = font;
|
|
982
|
+
}
|
|
983
|
+
canvas.width = Math.ceil((context?.measureText(text2).width ?? 40) + 16);
|
|
984
|
+
canvas.height = 56;
|
|
985
|
+
if (context) {
|
|
986
|
+
context.font = font;
|
|
987
|
+
context.fillStyle = color;
|
|
988
|
+
context.textBaseline = "middle";
|
|
989
|
+
context.fillText(text2, 8, 28);
|
|
990
|
+
}
|
|
991
|
+
const texture = new THREE.CanvasTexture(canvas);
|
|
992
|
+
texture.colorSpace = THREE.SRGBColorSpace;
|
|
993
|
+
return texture;
|
|
994
|
+
}
|
|
995
|
+
static dispose(object2) {
|
|
996
|
+
object2.traverse((child) => {
|
|
997
|
+
const mesh = child;
|
|
998
|
+
mesh.geometry?.dispose();
|
|
999
|
+
for (const material of [].concat(mesh.material ?? [])) {
|
|
1000
|
+
material.map?.dispose();
|
|
1001
|
+
material.dispose();
|
|
1002
|
+
}
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
// web-src/react.ts
|
|
1008
|
+
var React;
|
|
1009
|
+
function bind(react) {
|
|
1010
|
+
React = react;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
// web-src/plot-view.tsx
|
|
1014
|
+
var OWN_GESTURES = { "data-own-gestures": "" };
|
|
1015
|
+
function plotView(pi) {
|
|
1016
|
+
const Markdown = pi.ui.Markdown;
|
|
1017
|
+
function Canvas({
|
|
1018
|
+
plot,
|
|
1019
|
+
input,
|
|
1020
|
+
setSlider,
|
|
1021
|
+
redraw
|
|
1022
|
+
}) {
|
|
1023
|
+
const canvas = React.useRef(null);
|
|
1024
|
+
const dragged = plot.params.findIndex((param) => param.slider);
|
|
1025
|
+
const sliders = {
|
|
1026
|
+
get current() {
|
|
1027
|
+
return input.sliders;
|
|
1028
|
+
}
|
|
1029
|
+
};
|
|
1030
|
+
React.useEffect(() => {
|
|
1031
|
+
const element = canvas.current;
|
|
1032
|
+
const context = element?.getContext("2d");
|
|
1033
|
+
const frame = element?.parentElement;
|
|
1034
|
+
if (!element || !context || !frame) {
|
|
1035
|
+
return;
|
|
1036
|
+
}
|
|
1037
|
+
const painter = new PlotPainter(context, plot);
|
|
1038
|
+
const resize = () => {
|
|
1039
|
+
const width = element.clientWidth;
|
|
1040
|
+
const height = element.clientHeight;
|
|
1041
|
+
const scale = window.devicePixelRatio || 1;
|
|
1042
|
+
element.width = Math.round(width * scale);
|
|
1043
|
+
element.height = Math.round(height * scale);
|
|
1044
|
+
context.setTransform(scale, 0, 0, scale, 0, 0);
|
|
1045
|
+
painter.width = width;
|
|
1046
|
+
painter.height = height;
|
|
1047
|
+
const [r, g, b] = getComputedStyle(frame).color.match(/\d+(\.\d+)?/g) ?? ["128", "128", "128"];
|
|
1048
|
+
painter.ink = (alpha) => `rgba(${r},${g},${b},${alpha})`;
|
|
1049
|
+
};
|
|
1050
|
+
resize();
|
|
1051
|
+
let visible = true;
|
|
1052
|
+
let request = 0;
|
|
1053
|
+
let start;
|
|
1054
|
+
let elapsed = 0;
|
|
1055
|
+
let lastShown = 0;
|
|
1056
|
+
const tick = (now) => {
|
|
1057
|
+
request = 0;
|
|
1058
|
+
start ??= now - elapsed * 1000;
|
|
1059
|
+
elapsed = (now - start) / 1000;
|
|
1060
|
+
const current = painter.paint(elapsed, sliders.current);
|
|
1061
|
+
if (current.length && now - lastShown > 100) {
|
|
1062
|
+
lastShown = now;
|
|
1063
|
+
input.shown(current);
|
|
1064
|
+
}
|
|
1065
|
+
if (visible && (plot.moving || plot.dot || elapsed < painter.intro)) {
|
|
1066
|
+
request = requestAnimationFrame(tick);
|
|
1067
|
+
}
|
|
1068
|
+
};
|
|
1069
|
+
const play = () => {
|
|
1070
|
+
if (!request) {
|
|
1071
|
+
start = undefined;
|
|
1072
|
+
request = requestAnimationFrame(tick);
|
|
1073
|
+
}
|
|
1074
|
+
};
|
|
1075
|
+
redraw.current = () => {
|
|
1076
|
+
if (!request) {
|
|
1077
|
+
input.shown(painter.paint(elapsed, sliders.current));
|
|
1078
|
+
}
|
|
1079
|
+
};
|
|
1080
|
+
const resized = new ResizeObserver(() => {
|
|
1081
|
+
resize();
|
|
1082
|
+
painter.paint(elapsed, sliders.current);
|
|
1083
|
+
});
|
|
1084
|
+
resized.observe(frame);
|
|
1085
|
+
const seen = new IntersectionObserver(([entry]) => {
|
|
1086
|
+
visible = entry.isIntersecting;
|
|
1087
|
+
if (visible) {
|
|
1088
|
+
play();
|
|
1089
|
+
} else if (request) {
|
|
1090
|
+
cancelAnimationFrame(request);
|
|
1091
|
+
request = 0;
|
|
1092
|
+
}
|
|
1093
|
+
});
|
|
1094
|
+
seen.observe(frame);
|
|
1095
|
+
play();
|
|
1096
|
+
return () => {
|
|
1097
|
+
cancelAnimationFrame(request);
|
|
1098
|
+
resized.disconnect();
|
|
1099
|
+
seen.disconnect();
|
|
1100
|
+
};
|
|
1101
|
+
}, [plot, input, redraw]);
|
|
1102
|
+
const drag = (event) => {
|
|
1103
|
+
if (event.button !== 0) {
|
|
1104
|
+
return;
|
|
1105
|
+
}
|
|
1106
|
+
const slider = plot.params[dragged]?.slider;
|
|
1107
|
+
if (!slider) {
|
|
1108
|
+
return;
|
|
1109
|
+
}
|
|
1110
|
+
const width = canvas.current?.clientWidth || 1;
|
|
1111
|
+
const from = event.clientX;
|
|
1112
|
+
const at = sliders.current[dragged] ?? slider.value;
|
|
1113
|
+
const move = (moved) => setSlider(dragged, at + (moved.clientX - from) / width * (slider.max - slider.min));
|
|
1114
|
+
const end = () => {
|
|
1115
|
+
window.removeEventListener("pointermove", move);
|
|
1116
|
+
window.removeEventListener("pointerup", end);
|
|
1117
|
+
window.removeEventListener("pointercancel", end);
|
|
1118
|
+
};
|
|
1119
|
+
window.addEventListener("pointermove", move);
|
|
1120
|
+
window.addEventListener("pointerup", end);
|
|
1121
|
+
window.addEventListener("pointercancel", end);
|
|
1122
|
+
};
|
|
1123
|
+
const ratio = (plot.y[1] - plot.y[0]) / (plot.x[1] - plot.x[0]);
|
|
1124
|
+
const shape = { aspectRatio: String(1 / Math.min(Math.max(ratio, 0.5), 1)) };
|
|
1125
|
+
return dragged < 0 ? /* @__PURE__ */ React.createElement("canvas", {
|
|
1126
|
+
ref: canvas,
|
|
1127
|
+
className: "tpl-canvas",
|
|
1128
|
+
style: shape
|
|
1129
|
+
}) : /* @__PURE__ */ React.createElement("canvas", {
|
|
1130
|
+
ref: canvas,
|
|
1131
|
+
className: "tpl-canvas",
|
|
1132
|
+
style: shape,
|
|
1133
|
+
"data-drag": "",
|
|
1134
|
+
onPointerDown: drag,
|
|
1135
|
+
...OWN_GESTURES
|
|
1136
|
+
});
|
|
1137
|
+
}
|
|
1138
|
+
function Space({ plot, input }) {
|
|
1139
|
+
const Game3D = pi.ui.Game3D;
|
|
1140
|
+
const setup = React.useCallback((context) => new PlotScene(context, plot, input), [plot, input]);
|
|
1141
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
1142
|
+
className: "tpl-space"
|
|
1143
|
+
}, Game3D ? /* @__PURE__ */ React.createElement(Game3D, {
|
|
1144
|
+
setup
|
|
1145
|
+
}) : null);
|
|
1146
|
+
}
|
|
1147
|
+
function Card({ plot }) {
|
|
1148
|
+
const [values, setValues] = React.useState(() => PlotSpec.values(plot, 0));
|
|
1149
|
+
const redraw = React.useRef(() => {
|
|
1150
|
+
return;
|
|
1151
|
+
});
|
|
1152
|
+
const input = React.useMemo(() => ({ sliders: plot.params.map((param) => param.slider?.value), dirty: false, shown: setValues }), [plot]);
|
|
1153
|
+
const setSlider = (index, value) => {
|
|
1154
|
+
const slider = plot.params[index]?.slider;
|
|
1155
|
+
if (!slider) {
|
|
1156
|
+
return;
|
|
1157
|
+
}
|
|
1158
|
+
const next = input.sliders.slice();
|
|
1159
|
+
next[index] = Math.min(Math.max(Math.round(value / slider.step) * slider.step, slider.min), slider.max);
|
|
1160
|
+
input.sliders = next;
|
|
1161
|
+
input.dirty = true;
|
|
1162
|
+
redraw.current();
|
|
1163
|
+
};
|
|
1164
|
+
const labeled = plot.curves.filter((curve) => curve.label);
|
|
1165
|
+
const surfaces = plot.surfaces.filter((surface) => surface.label);
|
|
1166
|
+
const live = plot.params.map((param, index) => ({ param, index })).filter(({ param }) => !param.slider);
|
|
1167
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
1168
|
+
className: "tpl-card",
|
|
1169
|
+
"data-space": plot.space || undefined
|
|
1170
|
+
}, plot.title ? /* @__PURE__ */ React.createElement("div", {
|
|
1171
|
+
className: "tpl-title"
|
|
1172
|
+
}, plot.title) : null, plot.space ? /* @__PURE__ */ React.createElement(Space, {
|
|
1173
|
+
plot,
|
|
1174
|
+
input
|
|
1175
|
+
}) : /* @__PURE__ */ React.createElement(Canvas, {
|
|
1176
|
+
plot,
|
|
1177
|
+
input,
|
|
1178
|
+
setSlider,
|
|
1179
|
+
redraw
|
|
1180
|
+
}), plot.params.map((param, index) => param.slider ? /* @__PURE__ */ React.createElement(Slider, {
|
|
1181
|
+
key: param.name,
|
|
1182
|
+
name: param.name,
|
|
1183
|
+
slider: param.slider,
|
|
1184
|
+
value: values[index],
|
|
1185
|
+
onChange: (value) => setSlider(index, value)
|
|
1186
|
+
}) : null), labeled.length || surfaces.length || live.length ? /* @__PURE__ */ React.createElement("div", {
|
|
1187
|
+
className: "tpl-legend"
|
|
1188
|
+
}, surfaces.map((surface, index) => /* @__PURE__ */ React.createElement("span", {
|
|
1189
|
+
key: `s${index}`,
|
|
1190
|
+
className: "tpl-key"
|
|
1191
|
+
}, /* @__PURE__ */ React.createElement("span", {
|
|
1192
|
+
className: "tpl-patch",
|
|
1193
|
+
style: surface.color ? { background: surface.color } : undefined
|
|
1194
|
+
}), surface.label)), labeled.map((curve, index) => /* @__PURE__ */ React.createElement("span", {
|
|
1195
|
+
key: `c${index}`,
|
|
1196
|
+
className: "tpl-key"
|
|
1197
|
+
}, /* @__PURE__ */ React.createElement("span", {
|
|
1198
|
+
className: "tpl-line",
|
|
1199
|
+
"data-dash": curve.dash || undefined,
|
|
1200
|
+
style: { borderTopColor: curve.color }
|
|
1201
|
+
}), curve.label)), live.map(({ param, index }) => /* @__PURE__ */ React.createElement("span", {
|
|
1202
|
+
key: param.name,
|
|
1203
|
+
className: "tpl-live"
|
|
1204
|
+
}, param.name, " = ", PlotSpec.format(values[index] ?? 0)))) : null);
|
|
1205
|
+
}
|
|
1206
|
+
function Slider({ name, slider, value, onChange }) {
|
|
1207
|
+
const digits = Math.max(0, Math.min(4, -Math.floor(Math.log10(slider.step) + 0.000000001)));
|
|
1208
|
+
const shown = Number.isFinite(value) ? value : slider.value;
|
|
1209
|
+
return /* @__PURE__ */ React.createElement("label", {
|
|
1210
|
+
className: "tpl-slider"
|
|
1211
|
+
}, /* @__PURE__ */ React.createElement("span", {
|
|
1212
|
+
className: "tpl-slider-name",
|
|
1213
|
+
"data-label": slider.label ? "" : undefined
|
|
1214
|
+
}, slider.label ?? name), /* @__PURE__ */ React.createElement("input", {
|
|
1215
|
+
type: "range",
|
|
1216
|
+
min: slider.min,
|
|
1217
|
+
max: slider.max,
|
|
1218
|
+
step: slider.step,
|
|
1219
|
+
value: shown,
|
|
1220
|
+
onChange: (event) => onChange(Number(event.target.value)),
|
|
1221
|
+
...OWN_GESTURES
|
|
1222
|
+
}), /* @__PURE__ */ React.createElement("span", {
|
|
1223
|
+
className: "tpl-slider-value"
|
|
1224
|
+
}, shown.toFixed(digits)));
|
|
1225
|
+
}
|
|
1226
|
+
return function PlotBlock({ code, closed }) {
|
|
1227
|
+
const words = PlotWords.of(pi.ui.language);
|
|
1228
|
+
const read = React.useMemo(() => {
|
|
1229
|
+
if (!closed) {
|
|
1230
|
+
return;
|
|
1231
|
+
}
|
|
1232
|
+
try {
|
|
1233
|
+
return { plot: PlotSpec.read(code) };
|
|
1234
|
+
} catch (error) {
|
|
1235
|
+
return { error: error instanceof Error ? error.message : String(error) };
|
|
1236
|
+
}
|
|
1237
|
+
}, [code, closed]);
|
|
1238
|
+
if (!read) {
|
|
1239
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
1240
|
+
className: "tpl-card tpl-writing"
|
|
1241
|
+
}, /* @__PURE__ */ React.createElement("span", {
|
|
1242
|
+
className: "ta-spinner"
|
|
1243
|
+
}), /* @__PURE__ */ React.createElement("span", null, words.drawing));
|
|
1244
|
+
}
|
|
1245
|
+
if (!read.plot) {
|
|
1246
|
+
return /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement("div", {
|
|
1247
|
+
className: "tpl-failed"
|
|
1248
|
+
}, words.failed.replace("{reason}", read.error)), /* @__PURE__ */ React.createElement(Markdown, {
|
|
1249
|
+
text: `\`\`\`json
|
|
1250
|
+
${code}
|
|
1251
|
+
\`\`\``
|
|
1252
|
+
}));
|
|
1253
|
+
}
|
|
1254
|
+
return /* @__PURE__ */ React.createElement(Card, {
|
|
1255
|
+
plot: read.plot
|
|
1256
|
+
});
|
|
1257
|
+
};
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
// web-src/style.ts
|
|
1261
|
+
var STYLE = `
|
|
1262
|
+
/* A plot of a reply (\`\`\`ta-plot): a title, the canvas, the sliders of its params and a legend under it. */
|
|
1263
|
+
.tpl-card {
|
|
1264
|
+
--tpl-s: var(--ta-font-scale, 1);
|
|
1265
|
+
margin: 2px 0 12px;
|
|
1266
|
+
padding: 12px;
|
|
1267
|
+
max-width: 560px;
|
|
1268
|
+
border: 1px solid var(--ta-border);
|
|
1269
|
+
border-radius: 12px;
|
|
1270
|
+
background: var(--ta-card);
|
|
1271
|
+
color: var(--ta-text);
|
|
1272
|
+
font-size: calc(13px * var(--tpl-s));
|
|
1273
|
+
line-height: 1.5;
|
|
1274
|
+
}
|
|
1275
|
+
.tpl-title { font-weight: 600; margin-bottom: 8px; }
|
|
1276
|
+
.tpl-canvas { display: block; width: 100%; }
|
|
1277
|
+
.tpl-canvas[data-drag] { cursor: ew-resize; touch-action: pan-y; }
|
|
1278
|
+
.tpl-slider { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
|
|
1279
|
+
.tpl-slider-name { min-width: 16px; font-weight: 600; font-style: italic; }
|
|
1280
|
+
.tpl-slider-name[data-label] { font-style: normal; }
|
|
1281
|
+
.tpl-slider input { flex: 1; min-width: 0; accent-color: var(--ta-accent); }
|
|
1282
|
+
.tpl-slider-value { min-width: 44px; text-align: right; font-variant-numeric: tabular-nums; color: var(--ta-muted); }
|
|
1283
|
+
.tpl-space { position: relative; width: 100%; aspect-ratio: 4 / 3; border-radius: 8px; overflow: hidden; cursor: grab; }
|
|
1284
|
+
.tpl-space:active { cursor: grabbing; }
|
|
1285
|
+
.tpl-space > * { position: absolute; inset: 0; }
|
|
1286
|
+
.tpl-patch { width: 12px; height: 12px; border-radius: 3px; background: linear-gradient(135deg, #3b4cc0, #2bb6a3, #f5b400, #ff7a59); }
|
|
1287
|
+
.tpl-legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 8px; font-size: calc(12px * var(--tpl-s)); }
|
|
1288
|
+
.tpl-key { display: inline-flex; align-items: center; gap: 6px; }
|
|
1289
|
+
.tpl-line { width: 14px; height: 0; border-top: 3px solid; border-radius: 2px; }
|
|
1290
|
+
.tpl-line[data-dash] { border-top-width: 2px; border-top-style: dashed; }
|
|
1291
|
+
.tpl-live { font-variant-numeric: tabular-nums; color: var(--ta-muted); }
|
|
1292
|
+
.tpl-writing { display: flex; align-items: center; gap: 8px; color: var(--ta-muted); }
|
|
1293
|
+
.tpl-failed { margin-bottom: 4px; color: var(--ta-muted); font-size: calc(12px * var(--ta-font-scale, 1)); }
|
|
1294
|
+
`;
|
|
1295
|
+
function injectStyle() {
|
|
1296
|
+
const element = document.createElement("style");
|
|
1297
|
+
element.dataset.taPackage = "ta-plot";
|
|
1298
|
+
element.textContent = STYLE;
|
|
1299
|
+
document.head.appendChild(element);
|
|
1300
|
+
return () => element.remove();
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
// web-src/index.tsx
|
|
1304
|
+
function activate(pi) {
|
|
1305
|
+
bind(pi.React);
|
|
1306
|
+
if (!pi.registerCodeRenderer) {
|
|
1307
|
+
return () => {
|
|
1308
|
+
return;
|
|
1309
|
+
};
|
|
1310
|
+
}
|
|
1311
|
+
const removeStyle = injectStyle();
|
|
1312
|
+
const view = plotView(pi);
|
|
1313
|
+
for (const language of [LANGUAGE, ...ALIASES]) {
|
|
1314
|
+
pi.registerCodeRenderer(language, view);
|
|
1315
|
+
}
|
|
1316
|
+
return removeStyle;
|
|
1317
|
+
}
|
|
1318
|
+
export {
|
|
1319
|
+
activate as default
|
|
1320
|
+
};
|