@sarmal/core 0.10.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auto-init.cjs +189 -94
- package/dist/auto-init.cjs.map +1 -1
- package/dist/auto-init.d.cts +1 -2
- package/dist/auto-init.d.ts +1 -2
- package/dist/auto-init.js +188 -93
- package/dist/auto-init.js.map +1 -1
- package/dist/curves/artemis2.cjs +10 -7
- package/dist/curves/artemis2.d.cts +1 -1
- package/dist/curves/artemis2.d.ts +1 -1
- package/dist/curves/artemis2.js +9 -6
- package/dist/curves/astroid.cjs +4 -4
- package/dist/curves/astroid.d.cts +1 -1
- package/dist/curves/astroid.d.ts +1 -1
- package/dist/curves/astroid.js +3 -3
- package/dist/curves/deltoid.cjs +4 -4
- package/dist/curves/deltoid.d.cts +1 -1
- package/dist/curves/deltoid.d.ts +1 -1
- package/dist/curves/deltoid.js +3 -3
- package/dist/curves/epicycloid3.cjs +4 -4
- package/dist/curves/epicycloid3.d.cts +1 -1
- package/dist/curves/epicycloid3.d.ts +1 -1
- package/dist/curves/epicycloid3.js +3 -3
- package/dist/curves/epitrochoid7.cjs +5 -5
- package/dist/curves/epitrochoid7.d.cts +1 -1
- package/dist/curves/epitrochoid7.d.ts +1 -1
- package/dist/curves/epitrochoid7.js +4 -4
- package/dist/curves/index.cjs +32 -28
- package/dist/curves/index.cjs.map +1 -1
- package/dist/curves/index.d.cts +25 -13
- package/dist/curves/index.d.ts +25 -13
- package/dist/curves/index.js +44 -28
- package/dist/curves/index.js.map +1 -1
- package/dist/curves/lame.cjs +6 -5
- package/dist/curves/lame.d.cts +1 -1
- package/dist/curves/lame.d.ts +1 -1
- package/dist/curves/lame.js +5 -4
- package/dist/curves/lissajous32.cjs +4 -4
- package/dist/curves/lissajous32.d.cts +1 -1
- package/dist/curves/lissajous32.d.ts +1 -1
- package/dist/curves/lissajous32.js +3 -3
- package/dist/curves/lissajous43.cjs +4 -4
- package/dist/curves/lissajous43.d.cts +1 -1
- package/dist/curves/lissajous43.d.ts +1 -1
- package/dist/curves/lissajous43.js +3 -3
- package/dist/curves/rose3.cjs +4 -4
- package/dist/curves/rose3.d.cts +1 -1
- package/dist/curves/rose3.d.ts +1 -1
- package/dist/curves/rose3.js +3 -3
- package/dist/curves/rose5.cjs +4 -4
- package/dist/curves/rose5.d.cts +1 -1
- package/dist/curves/rose5.d.ts +1 -1
- package/dist/curves/rose5.js +3 -3
- package/dist/index.cjs +216 -108
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +69 -34
- package/dist/index.d.ts +69 -34
- package/dist/index.js +233 -108
- package/dist/index.js.map +1 -1
- package/dist/types-BW0bpL1Z.d.cts +290 -0
- package/dist/types-BW0bpL1Z.d.ts +290 -0
- package/package.json +1 -1
- package/dist/types-DcyISvnH.d.cts +0 -230
- package/dist/types-DcyISvnH.d.ts +0 -230
package/dist/auto-init.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
// src/engine.ts
|
|
4
4
|
var TWO_PI = Math.PI * 2;
|
|
@@ -63,22 +63,24 @@ function resolveCurve(curveDef) {
|
|
|
63
63
|
period,
|
|
64
64
|
speed,
|
|
65
65
|
skeleton: curveDef.skeleton,
|
|
66
|
-
skeletonFn: curveDef.skeletonFn
|
|
66
|
+
skeletonFn: curveDef.skeletonFn,
|
|
67
67
|
};
|
|
68
68
|
}
|
|
69
69
|
function createEngine(curveDef, trailLength = 120) {
|
|
70
70
|
if (!Number.isFinite(trailLength) || trailLength <= 0) {
|
|
71
71
|
throw new RangeError(
|
|
72
|
-
`[sarmal] trailLength must be a positive finite number, got ${trailLength}
|
|
72
|
+
`[sarmal] trailLength must be a positive finite number, got ${trailLength}`,
|
|
73
73
|
);
|
|
74
74
|
}
|
|
75
75
|
let curve = resolveCurve(curveDef);
|
|
76
76
|
const trail = new CircularBuffer(trailLength);
|
|
77
77
|
let t = 0;
|
|
78
78
|
let actualTime = 0;
|
|
79
|
+
let userSpeedOverride = null;
|
|
79
80
|
let morphCurveB = null;
|
|
80
81
|
let _morphAlpha = null;
|
|
81
82
|
let _morphStrategy = "normalized";
|
|
83
|
+
let _speedTransition = null;
|
|
82
84
|
function sampleSkeleton(c, sampleT) {
|
|
83
85
|
if (c.skeletonFn) {
|
|
84
86
|
return c.skeletonFn(sampleT);
|
|
@@ -90,15 +92,25 @@ function createEngine(curveDef, trailLength = 120) {
|
|
|
90
92
|
}
|
|
91
93
|
return {
|
|
92
94
|
tick(deltaTime) {
|
|
93
|
-
|
|
95
|
+
if (_speedTransition !== null) {
|
|
96
|
+
_speedTransition.elapsed += deltaTime * 1e3;
|
|
97
|
+
const alpha = Math.min(_speedTransition.elapsed / _speedTransition.duration, 1);
|
|
98
|
+
userSpeedOverride = lerp(_speedTransition.from, _speedTransition.to, alpha);
|
|
99
|
+
if (alpha >= 1) {
|
|
100
|
+
userSpeedOverride = _speedTransition.to;
|
|
101
|
+
_speedTransition.resolve();
|
|
102
|
+
_speedTransition = null;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
let effectiveSpeed = userSpeedOverride ?? curve.speed;
|
|
94
106
|
if (morphCurveB !== null && _morphAlpha !== null) {
|
|
95
|
-
effectiveSpeed = lerp(
|
|
107
|
+
effectiveSpeed = lerp(effectiveSpeed, morphCurveB.speed, _morphAlpha);
|
|
96
108
|
}
|
|
97
109
|
t = (t + effectiveSpeed * deltaTime) % curve.period;
|
|
98
110
|
actualTime += deltaTime;
|
|
99
111
|
if (morphCurveB !== null && _morphAlpha !== null) {
|
|
100
112
|
const a = curve.fn(t, actualTime, EMPTY_PARAMS);
|
|
101
|
-
const tB = _morphStrategy === "normalized" ? t / curve.period * morphCurveB.period : t;
|
|
113
|
+
const tB = _morphStrategy === "normalized" ? (t / curve.period) * morphCurveB.period : t;
|
|
102
114
|
const b = morphCurveB.fn(tB, actualTime, EMPTY_PARAMS);
|
|
103
115
|
trail.push(a.x + (b.x - a.x) * _morphAlpha, a.y + (b.y - a.y) * _morphAlpha);
|
|
104
116
|
} else {
|
|
@@ -122,14 +134,14 @@ function createEngine(curveDef, trailLength = 120) {
|
|
|
122
134
|
trail.clear();
|
|
123
135
|
},
|
|
124
136
|
jump(newT, { clearTrail = false } = {}) {
|
|
125
|
-
t = (newT % curve.period + curve.period) % curve.period;
|
|
137
|
+
t = ((newT % curve.period) + curve.period) % curve.period;
|
|
126
138
|
if (clearTrail) {
|
|
127
139
|
trail.clear();
|
|
128
140
|
}
|
|
129
141
|
},
|
|
130
142
|
seek(targetT, { wrap = false, step = curve.period / trailLength } = {}) {
|
|
131
143
|
const advance = curve.speed * step;
|
|
132
|
-
const target = (targetT % curve.period + curve.period) % curve.period;
|
|
144
|
+
const target = ((targetT % curve.period) + curve.period) % curve.period;
|
|
133
145
|
const targetTime = target / curve.speed;
|
|
134
146
|
t = target;
|
|
135
147
|
actualTime = targetTime;
|
|
@@ -138,7 +150,7 @@ function createEngine(curveDef, trailLength = 120) {
|
|
|
138
150
|
const count = wrap ? trailLength : Math.min(trailLength, pointsFromStart);
|
|
139
151
|
for (let i = count - 1; i >= 0; i--) {
|
|
140
152
|
const sampleT = target - i * advance;
|
|
141
|
-
const wrappedT = (sampleT % curve.period + curve.period) % curve.period;
|
|
153
|
+
const wrappedT = ((sampleT % curve.period) + curve.period) % curve.period;
|
|
142
154
|
const time = targetTime - i * step;
|
|
143
155
|
const point = curve.fn(wrappedT, time, EMPTY_PARAMS);
|
|
144
156
|
trail.push(point.x, point.y);
|
|
@@ -155,13 +167,16 @@ function createEngine(curveDef, trailLength = 120) {
|
|
|
155
167
|
...frozenB,
|
|
156
168
|
fn: (sampleT, time, params) => {
|
|
157
169
|
const a = frozenA.fn(sampleT, time, params);
|
|
158
|
-
const tB =
|
|
170
|
+
const tB =
|
|
171
|
+
frozenStrategy === "normalized"
|
|
172
|
+
? (sampleT / frozenA.period) * frozenB.period
|
|
173
|
+
: sampleT;
|
|
159
174
|
const b = frozenB.fn(tB, time, params);
|
|
160
175
|
return {
|
|
161
176
|
x: a.x + (b.x - a.x) * frozenAlpha,
|
|
162
|
-
y: a.y + (b.y - a.y) * frozenAlpha
|
|
177
|
+
y: a.y + (b.y - a.y) * frozenAlpha,
|
|
163
178
|
};
|
|
164
|
-
}
|
|
179
|
+
},
|
|
165
180
|
};
|
|
166
181
|
}
|
|
167
182
|
_morphStrategy = strategy;
|
|
@@ -174,7 +189,7 @@ function createEngine(curveDef, trailLength = 120) {
|
|
|
174
189
|
completeMorph() {
|
|
175
190
|
if (morphCurveB !== null) {
|
|
176
191
|
if (_morphStrategy === "normalized" && curve.period !== morphCurveB.period) {
|
|
177
|
-
t = t / curve.period * morphCurveB.period;
|
|
192
|
+
t = (t / curve.period) * morphCurveB.period;
|
|
178
193
|
}
|
|
179
194
|
curve = morphCurveB;
|
|
180
195
|
}
|
|
@@ -186,23 +201,64 @@ function createEngine(curveDef, trailLength = 120) {
|
|
|
186
201
|
const points = new Array(steps);
|
|
187
202
|
if (morphCurveB !== null && _morphAlpha !== null) {
|
|
188
203
|
for (let i = 0; i < steps; i++) {
|
|
189
|
-
const sampleT = i / (steps - 1) * curve.period;
|
|
204
|
+
const sampleT = (i / (steps - 1)) * curve.period;
|
|
190
205
|
const a = sampleSkeleton(curve, sampleT);
|
|
191
|
-
const tB =
|
|
206
|
+
const tB =
|
|
207
|
+
_morphStrategy === "normalized"
|
|
208
|
+
? (sampleT / curve.period) * morphCurveB.period
|
|
209
|
+
: sampleT;
|
|
192
210
|
const b = sampleSkeleton(morphCurveB, tB);
|
|
193
211
|
points[i] = {
|
|
194
212
|
x: a.x + (b.x - a.x) * _morphAlpha,
|
|
195
|
-
y: a.y + (b.y - a.y) * _morphAlpha
|
|
213
|
+
y: a.y + (b.y - a.y) * _morphAlpha,
|
|
196
214
|
};
|
|
197
215
|
}
|
|
198
216
|
return points;
|
|
199
217
|
}
|
|
200
218
|
for (let i = 0; i < steps; i++) {
|
|
201
|
-
const sampleT = i / (steps - 1) * curve.period;
|
|
219
|
+
const sampleT = (i / (steps - 1)) * curve.period;
|
|
202
220
|
points[i] = sampleSkeleton(curve, sampleT);
|
|
203
221
|
}
|
|
204
222
|
return points;
|
|
205
|
-
}
|
|
223
|
+
},
|
|
224
|
+
setSpeed(speed) {
|
|
225
|
+
if (!Number.isFinite(speed)) {
|
|
226
|
+
throw new Error("speed must be a finite number");
|
|
227
|
+
}
|
|
228
|
+
if (_speedTransition !== null) {
|
|
229
|
+
_speedTransition.reject(new Error("Speed transition cancelled"));
|
|
230
|
+
_speedTransition = null;
|
|
231
|
+
}
|
|
232
|
+
userSpeedOverride = speed;
|
|
233
|
+
},
|
|
234
|
+
getSpeed() {
|
|
235
|
+
return userSpeedOverride ?? curve.speed;
|
|
236
|
+
},
|
|
237
|
+
resetSpeed() {
|
|
238
|
+
userSpeedOverride = null;
|
|
239
|
+
},
|
|
240
|
+
setSpeedOver(speed, duration) {
|
|
241
|
+
if (!Number.isFinite(speed)) {
|
|
242
|
+
throw new Error("speed must be a finite number");
|
|
243
|
+
}
|
|
244
|
+
if (!Number.isFinite(duration) || duration <= 0) {
|
|
245
|
+
throw new Error("duration must be a finite number greater than 0");
|
|
246
|
+
}
|
|
247
|
+
if (_speedTransition !== null) {
|
|
248
|
+
_speedTransition.reject(new Error("Speed transition cancelled"));
|
|
249
|
+
_speedTransition = null;
|
|
250
|
+
}
|
|
251
|
+
const from = userSpeedOverride ?? curve.speed;
|
|
252
|
+
return new Promise((resolve, reject) => {
|
|
253
|
+
_speedTransition = { from, to: speed, elapsed: 0, duration, resolve, reject };
|
|
254
|
+
});
|
|
255
|
+
},
|
|
256
|
+
cancelSpeedTransition() {
|
|
257
|
+
if (_speedTransition !== null) {
|
|
258
|
+
_speedTransition.reject(new Error("Speed transition cancelled"));
|
|
259
|
+
_speedTransition = null;
|
|
260
|
+
}
|
|
261
|
+
},
|
|
206
262
|
};
|
|
207
263
|
}
|
|
208
264
|
|
|
@@ -210,6 +266,7 @@ function createEngine(curveDef, trailLength = 120) {
|
|
|
210
266
|
var DEFAULT_MORPH_DURATION_MS = 300;
|
|
211
267
|
var DEFAULT_SKELETON_OPACITY = 0.15;
|
|
212
268
|
var FIT_PADDING = 0.1;
|
|
269
|
+
var FIT_PADDING_MIN = 4;
|
|
213
270
|
var TRAIL_FADE_CURVE = 1.5;
|
|
214
271
|
var TRAIL_MAX_OPACITY = 0.88;
|
|
215
272
|
var TRAIL_MIN_WIDTH = 0.5;
|
|
@@ -264,13 +321,16 @@ function computeTrailQuad(trail, i, trailCount, toX, toY) {
|
|
|
264
321
|
r1x: nx - n1.x * w1,
|
|
265
322
|
r1y: ny - n1.y * w1,
|
|
266
323
|
opacity,
|
|
267
|
-
progress
|
|
324
|
+
progress,
|
|
268
325
|
};
|
|
269
326
|
}
|
|
270
327
|
function computeBoundaries(pts, logicalWidth, logicalHeight) {
|
|
271
328
|
if (pts.length === 0) return null;
|
|
272
329
|
const first = pts[0];
|
|
273
|
-
let minX = first.x,
|
|
330
|
+
let minX = first.x,
|
|
331
|
+
maxX = first.x,
|
|
332
|
+
minY = first.y,
|
|
333
|
+
maxY = first.y;
|
|
274
334
|
for (const p of pts) {
|
|
275
335
|
if (p.x < minX) {
|
|
276
336
|
minX = p.x;
|
|
@@ -289,21 +349,37 @@ function computeBoundaries(pts, logicalWidth, logicalHeight) {
|
|
|
289
349
|
const h = maxY - minY;
|
|
290
350
|
if (w === 0 && h === 0) {
|
|
291
351
|
throw new Error(
|
|
292
|
-
"[sarmal] Degenerate curve: all skeleton points are identical. Check that your curve fn returns distinct points for different values of t."
|
|
352
|
+
"[sarmal] Degenerate curve: all skeleton points are identical. Check that your curve fn returns distinct points for different values of t.",
|
|
293
353
|
);
|
|
294
354
|
}
|
|
295
|
-
const
|
|
296
|
-
const
|
|
297
|
-
const
|
|
355
|
+
const scaleXProportional = logicalWidth / (w * (1 + FIT_PADDING * 2));
|
|
356
|
+
const scaleYProportional = logicalHeight / (h * (1 + FIT_PADDING * 2));
|
|
357
|
+
const scaleXMinPadding = (logicalWidth - FIT_PADDING_MIN * 2) / w;
|
|
358
|
+
const scaleYMinPadding = (logicalHeight - FIT_PADDING_MIN * 2) / h;
|
|
359
|
+
const scale = Math.min(
|
|
360
|
+
scaleXProportional,
|
|
361
|
+
scaleYProportional,
|
|
362
|
+
scaleXMinPadding,
|
|
363
|
+
scaleYMinPadding,
|
|
364
|
+
);
|
|
298
365
|
return {
|
|
299
366
|
scale,
|
|
300
367
|
offsetX: (logicalWidth - w * scale) / 2 - minX * scale,
|
|
301
|
-
offsetY: (logicalHeight - h * scale) / 2 - minY * scale
|
|
368
|
+
offsetY: (logicalHeight - h * scale) / 2 - minY * scale,
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
function enginePassthroughs(engine) {
|
|
372
|
+
return {
|
|
373
|
+
jump: engine.jump,
|
|
374
|
+
seek: engine.seek,
|
|
375
|
+
setSpeed: engine.setSpeed,
|
|
376
|
+
getSpeed: engine.getSpeed,
|
|
377
|
+
resetSpeed: engine.resetSpeed,
|
|
378
|
+
setSpeedOver: engine.setSpeedOver,
|
|
302
379
|
};
|
|
303
380
|
}
|
|
304
381
|
|
|
305
382
|
// src/renderer.ts
|
|
306
|
-
var DEFAULT_HEAD_RADIUS = 4;
|
|
307
383
|
var DEFAULT_SKELETON_COLOR = "#ffffff";
|
|
308
384
|
var GRADIENT = {
|
|
309
385
|
bard: ["#a855f7", "#3b82f6", "#14b8a6", "#ec4899"],
|
|
@@ -311,7 +387,7 @@ var GRADIENT = {
|
|
|
311
387
|
ocean: ["#1e3a8a", "#06b6d4", "#22d3ee", "#e0f2fe"],
|
|
312
388
|
ice: ["#1e3a8a", "#67e8f9"],
|
|
313
389
|
fire: ["#7f1d1d", "#fbbf24"],
|
|
314
|
-
forest: ["#14532d", "#86efac"]
|
|
390
|
+
forest: ["#14532d", "#86efac"],
|
|
315
391
|
};
|
|
316
392
|
var PRESETS = {
|
|
317
393
|
bard: GRADIENT.bard,
|
|
@@ -319,16 +395,16 @@ var PRESETS = {
|
|
|
319
395
|
ocean: GRADIENT.ocean,
|
|
320
396
|
ice: GRADIENT.ice,
|
|
321
397
|
fire: GRADIENT.fire,
|
|
322
|
-
forest: GRADIENT.forest
|
|
398
|
+
forest: GRADIENT.forest,
|
|
323
399
|
};
|
|
324
400
|
function hexToRgb(hex) {
|
|
325
401
|
const n = parseInt(hex.slice(1), 16);
|
|
326
|
-
return { r: n >> 16, g: n >> 8 & 255, b: n & 255 };
|
|
402
|
+
return { r: n >> 16, g: (n >> 8) & 255, b: n & 255 };
|
|
327
403
|
}
|
|
328
404
|
var lerpRgb = (a, b, t) => ({
|
|
329
405
|
r: Math.round(a.r + (b.r - a.r) * t),
|
|
330
406
|
g: Math.round(a.g + (b.g - a.g) * t),
|
|
331
|
-
b: Math.round(a.b + (b.b - a.b) * t)
|
|
407
|
+
b: Math.round(a.b + (b.b - a.b) * t),
|
|
332
408
|
});
|
|
333
409
|
function getPaletteColor(palette, position, timeOffset = 0) {
|
|
334
410
|
if (palette.length === 0) return { r: 255, g: 255, b: 255 };
|
|
@@ -348,7 +424,7 @@ function resolvePalette(palette, trailStyle) {
|
|
|
348
424
|
}
|
|
349
425
|
function hexToRgbComponents(hex) {
|
|
350
426
|
const n = parseInt(hex.slice(1), 16);
|
|
351
|
-
return `${n >> 16},${n >> 8 & 255},${n & 255}`;
|
|
427
|
+
return `${n >> 16},${(n >> 8) & 255},${n & 255}`;
|
|
352
428
|
}
|
|
353
429
|
function applyDprSizing(target, logicalWidth, logicalHeight, dpr) {
|
|
354
430
|
target.style.width = `${logicalWidth}px`;
|
|
@@ -363,14 +439,21 @@ function createRenderer(options) {
|
|
|
363
439
|
}
|
|
364
440
|
const ctx = canvas.getContext("2d");
|
|
365
441
|
const engine = options.engine;
|
|
442
|
+
const trailStyle = options.trailStyle ?? "default";
|
|
443
|
+
const trailColor = options.trailColor ?? "#ffffff";
|
|
444
|
+
const palette = resolvePalette(options.palette, trailStyle);
|
|
445
|
+
function defaultHeadColor() {
|
|
446
|
+
if (trailStyle !== "default") {
|
|
447
|
+
const { r, g, b } = getPaletteColor(palette, 1);
|
|
448
|
+
return `rgb(${r},${g},${b})`;
|
|
449
|
+
}
|
|
450
|
+
return trailColor;
|
|
451
|
+
}
|
|
366
452
|
const opts = {
|
|
367
453
|
skeletonColor: options.skeletonColor ?? DEFAULT_SKELETON_COLOR,
|
|
368
|
-
trailColor
|
|
369
|
-
headColor: options.headColor ??
|
|
370
|
-
headRadius: options.headRadius ?? DEFAULT_HEAD_RADIUS
|
|
454
|
+
trailColor,
|
|
455
|
+
headColor: options.headColor ?? defaultHeadColor(),
|
|
371
456
|
};
|
|
372
|
-
const trailStyle = options.trailStyle ?? "default";
|
|
373
|
-
const palette = resolvePalette(options.palette, trailStyle);
|
|
374
457
|
const trailRgb = hexToRgbComponents(opts.trailColor);
|
|
375
458
|
const dpr = typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1;
|
|
376
459
|
function setupCanvas() {
|
|
@@ -470,7 +553,7 @@ function createRenderer(options) {
|
|
|
470
553
|
i,
|
|
471
554
|
trailCount,
|
|
472
555
|
toX,
|
|
473
|
-
toY
|
|
556
|
+
toY,
|
|
474
557
|
);
|
|
475
558
|
if (trailStyle === "default") {
|
|
476
559
|
ctx.fillStyle = `rgba(${trailRgb},${opacity})`;
|
|
@@ -494,15 +577,14 @@ function createRenderer(options) {
|
|
|
494
577
|
}
|
|
495
578
|
const x = head.x * scale + offsetX;
|
|
496
579
|
const y = head.y * scale + offsetY;
|
|
580
|
+
const r =
|
|
581
|
+
options.headRadius ?? Math.max(2, 3 * Math.sqrt(Math.min(logicalWidth, logicalHeight) / 160));
|
|
497
582
|
ctx.fillStyle = opts.headColor;
|
|
498
583
|
ctx.beginPath();
|
|
499
|
-
ctx.arc(x, y,
|
|
584
|
+
ctx.arc(x, y, r, 0, Math.PI * 2);
|
|
500
585
|
ctx.fill();
|
|
501
586
|
}
|
|
502
|
-
function
|
|
503
|
-
const now = performance.now();
|
|
504
|
-
const deltaTime = Math.min((now - lastTime) / 1e3, 1 / 30);
|
|
505
|
-
lastTime = now;
|
|
587
|
+
function renderFrame(deltaTime) {
|
|
506
588
|
if (trailStyle === "gradient-animated") {
|
|
507
589
|
gradientAnimTime += deltaTime * 1e3;
|
|
508
590
|
}
|
|
@@ -538,27 +620,39 @@ function createRenderer(options) {
|
|
|
538
620
|
drawSkeleton();
|
|
539
621
|
drawTrail();
|
|
540
622
|
drawHead();
|
|
541
|
-
|
|
623
|
+
}
|
|
624
|
+
function loop() {
|
|
625
|
+
const now = performance.now();
|
|
626
|
+
const deltaTime = Math.min((now - lastTime) / 1e3, 1 / 30);
|
|
627
|
+
lastTime = now;
|
|
628
|
+
renderFrame(deltaTime);
|
|
629
|
+
animationId = requestAnimationFrame(loop);
|
|
542
630
|
}
|
|
543
631
|
skeleton = engine.getSarmalSkeleton();
|
|
544
632
|
calculateBoundaries();
|
|
545
633
|
if (!engine.isLiveSkeleton) {
|
|
546
634
|
buildSkeletonCanvas();
|
|
547
635
|
}
|
|
548
|
-
|
|
549
|
-
|
|
636
|
+
if (options.initialT !== void 0) {
|
|
637
|
+
engine.seek(options.initialT);
|
|
638
|
+
}
|
|
639
|
+
renderFrame(0);
|
|
640
|
+
const shouldAutoStart = options.autoStart !== false;
|
|
641
|
+
const instance = {
|
|
642
|
+
play() {
|
|
550
643
|
if (animationId !== null) {
|
|
551
644
|
return;
|
|
552
645
|
}
|
|
553
646
|
lastTime = performance.now();
|
|
554
|
-
|
|
647
|
+
loop();
|
|
555
648
|
},
|
|
556
|
-
|
|
649
|
+
pause() {
|
|
557
650
|
if (animationId === null) {
|
|
558
651
|
return;
|
|
559
652
|
}
|
|
560
653
|
cancelAnimationFrame(animationId);
|
|
561
654
|
animationId = null;
|
|
655
|
+
engine.cancelSpeedTransition();
|
|
562
656
|
},
|
|
563
657
|
reset() {
|
|
564
658
|
engine.reset();
|
|
@@ -571,12 +665,7 @@ function createRenderer(options) {
|
|
|
571
665
|
animationId = null;
|
|
572
666
|
}
|
|
573
667
|
},
|
|
574
|
-
|
|
575
|
-
engine.jump(t, options2);
|
|
576
|
-
},
|
|
577
|
-
seek(t, options2) {
|
|
578
|
-
engine.seek(t, options2);
|
|
579
|
-
},
|
|
668
|
+
...enginePassthroughs(engine),
|
|
580
669
|
morphTo(target, options2) {
|
|
581
670
|
if (morphResolve !== null) {
|
|
582
671
|
engine.completeMorph();
|
|
@@ -590,26 +679,33 @@ function createRenderer(options) {
|
|
|
590
679
|
return new Promise((resolve) => {
|
|
591
680
|
morphResolve = resolve;
|
|
592
681
|
});
|
|
593
|
-
}
|
|
682
|
+
},
|
|
594
683
|
};
|
|
684
|
+
if (shouldAutoStart) {
|
|
685
|
+
instance.play();
|
|
686
|
+
}
|
|
687
|
+
return instance;
|
|
595
688
|
}
|
|
596
689
|
|
|
597
690
|
// src/curves/artemis2.ts
|
|
598
691
|
var TWO_PI2 = Math.PI * 2;
|
|
599
692
|
function artemis2Fn(t, _time, _params) {
|
|
600
|
-
const a = 0.35,
|
|
601
|
-
|
|
693
|
+
const a = 0.35,
|
|
694
|
+
b = 0.15,
|
|
695
|
+
ox = 0.175;
|
|
696
|
+
const s = Math.sin(t),
|
|
697
|
+
c = Math.cos(t);
|
|
602
698
|
const denom = 1 + s * s;
|
|
603
699
|
return {
|
|
604
|
-
x: c * (1 + a * c) / denom - ox,
|
|
605
|
-
y: s * c * (1 + b * c) / denom
|
|
700
|
+
x: (c * (1 + a * c)) / denom - ox,
|
|
701
|
+
y: (s * c * (1 + b * c)) / denom,
|
|
606
702
|
};
|
|
607
703
|
}
|
|
608
704
|
var artemis2 = {
|
|
609
705
|
name: "Artemis II",
|
|
610
706
|
fn: artemis2Fn,
|
|
611
707
|
period: TWO_PI2,
|
|
612
|
-
speed: 0.7
|
|
708
|
+
speed: 0.7,
|
|
613
709
|
};
|
|
614
710
|
|
|
615
711
|
// src/curves/astroid.ts
|
|
@@ -619,14 +715,14 @@ function astroidFn(t, _time, _params) {
|
|
|
619
715
|
const s = Math.sin(t);
|
|
620
716
|
return {
|
|
621
717
|
x: c * c * c,
|
|
622
|
-
y: s * s * s
|
|
718
|
+
y: s * s * s,
|
|
623
719
|
};
|
|
624
720
|
}
|
|
625
721
|
var astroid = {
|
|
626
722
|
name: "Astroid",
|
|
627
723
|
fn: astroidFn,
|
|
628
724
|
period: TWO_PI3,
|
|
629
|
-
speed: 1.1
|
|
725
|
+
speed: 1.1,
|
|
630
726
|
};
|
|
631
727
|
|
|
632
728
|
// src/curves/deltoid.ts
|
|
@@ -634,14 +730,14 @@ var TWO_PI4 = Math.PI * 2;
|
|
|
634
730
|
function deltoidFn(t, _time, _params) {
|
|
635
731
|
return {
|
|
636
732
|
x: 2 * Math.cos(t) + Math.cos(2 * t),
|
|
637
|
-
y: 2 * Math.sin(t) - Math.sin(2 * t)
|
|
733
|
+
y: 2 * Math.sin(t) - Math.sin(2 * t),
|
|
638
734
|
};
|
|
639
735
|
}
|
|
640
736
|
var deltoid = {
|
|
641
737
|
name: "Deltoid",
|
|
642
738
|
fn: deltoidFn,
|
|
643
739
|
period: TWO_PI4,
|
|
644
|
-
speed: 0.9
|
|
740
|
+
speed: 0.9,
|
|
645
741
|
};
|
|
646
742
|
|
|
647
743
|
// src/curves/epicycloid3.ts
|
|
@@ -649,14 +745,14 @@ var TWO_PI5 = Math.PI * 2;
|
|
|
649
745
|
function epicycloid3Fn(t, _time, _params) {
|
|
650
746
|
return {
|
|
651
747
|
x: 4 * Math.cos(t) - Math.cos(4 * t),
|
|
652
|
-
y: 4 * Math.sin(t) - Math.sin(4 * t)
|
|
748
|
+
y: 4 * Math.sin(t) - Math.sin(4 * t),
|
|
653
749
|
};
|
|
654
750
|
}
|
|
655
751
|
var epicycloid3 = {
|
|
656
752
|
name: "Epicycloid (n=3)",
|
|
657
753
|
fn: epicycloid3Fn,
|
|
658
754
|
period: TWO_PI5,
|
|
659
|
-
speed: 0.75
|
|
755
|
+
speed: 0.75,
|
|
660
756
|
};
|
|
661
757
|
|
|
662
758
|
// src/curves/epitrochoid7.ts
|
|
@@ -665,14 +761,14 @@ function epitrochoid7Fn(t, _time, _params) {
|
|
|
665
761
|
const d = 1 + 0.55 * Math.sin(t * 0.5);
|
|
666
762
|
return {
|
|
667
763
|
x: 7 * Math.cos(t) - d * Math.cos(7 * t),
|
|
668
|
-
y: 7 * Math.sin(t) - d * Math.sin(7 * t)
|
|
764
|
+
y: 7 * Math.sin(t) - d * Math.sin(7 * t),
|
|
669
765
|
};
|
|
670
766
|
}
|
|
671
767
|
function epitrochoid7SkeletonFn(t) {
|
|
672
768
|
const d = 1.275;
|
|
673
769
|
return {
|
|
674
770
|
x: 7 * Math.cos(t) - d * Math.cos(7 * t),
|
|
675
|
-
y: 7 * Math.sin(t) - d * Math.sin(7 * t)
|
|
771
|
+
y: 7 * Math.sin(t) - d * Math.sin(7 * t),
|
|
676
772
|
};
|
|
677
773
|
}
|
|
678
774
|
var epitrochoid7 = {
|
|
@@ -680,7 +776,7 @@ var epitrochoid7 = {
|
|
|
680
776
|
fn: epitrochoid7Fn,
|
|
681
777
|
period: TWO_PI6,
|
|
682
778
|
speed: 1.4,
|
|
683
|
-
skeletonFn: epitrochoid7SkeletonFn
|
|
779
|
+
skeletonFn: epitrochoid7SkeletonFn,
|
|
684
780
|
};
|
|
685
781
|
|
|
686
782
|
// src/curves/lissajous32.ts
|
|
@@ -689,7 +785,7 @@ function lissajous32Fn(t, time, _params) {
|
|
|
689
785
|
const phi = time * 0.45;
|
|
690
786
|
return {
|
|
691
787
|
x: Math.sin(3 * t + phi),
|
|
692
|
-
y: Math.sin(2 * t)
|
|
788
|
+
y: Math.sin(2 * t),
|
|
693
789
|
};
|
|
694
790
|
}
|
|
695
791
|
var lissajous32 = {
|
|
@@ -697,7 +793,7 @@ var lissajous32 = {
|
|
|
697
793
|
fn: lissajous32Fn,
|
|
698
794
|
period: TWO_PI7,
|
|
699
795
|
speed: 2,
|
|
700
|
-
skeleton: "live"
|
|
796
|
+
skeleton: "live",
|
|
701
797
|
};
|
|
702
798
|
|
|
703
799
|
// src/curves/lissajous43.ts
|
|
@@ -706,7 +802,7 @@ function lissajous43Fn(t, time, _params) {
|
|
|
706
802
|
const phi = time * 0.38;
|
|
707
803
|
return {
|
|
708
804
|
x: Math.sin(4 * t + phi),
|
|
709
|
-
y: Math.sin(3 * t)
|
|
805
|
+
y: Math.sin(3 * t),
|
|
710
806
|
};
|
|
711
807
|
}
|
|
712
808
|
var lissajous43 = {
|
|
@@ -714,17 +810,18 @@ var lissajous43 = {
|
|
|
714
810
|
fn: lissajous43Fn,
|
|
715
811
|
period: TWO_PI8,
|
|
716
812
|
speed: 1.8,
|
|
717
|
-
skeleton: "live"
|
|
813
|
+
skeleton: "live",
|
|
718
814
|
};
|
|
719
815
|
|
|
720
816
|
// src/curves/lame.ts
|
|
721
817
|
var TWO_PI9 = Math.PI * 2;
|
|
722
818
|
function lameFn(t, time, _params) {
|
|
723
819
|
const p = 1.75 + 1.25 * Math.sin(time * 0.48);
|
|
724
|
-
const c = Math.cos(t),
|
|
820
|
+
const c = Math.cos(t),
|
|
821
|
+
s = Math.sin(t);
|
|
725
822
|
return {
|
|
726
823
|
x: Math.sign(c) * Math.pow(Math.abs(c), p),
|
|
727
|
-
y: Math.sign(s) * Math.pow(Math.abs(s), p)
|
|
824
|
+
y: Math.sign(s) * Math.pow(Math.abs(s), p),
|
|
728
825
|
};
|
|
729
826
|
}
|
|
730
827
|
var lame = {
|
|
@@ -732,7 +829,7 @@ var lame = {
|
|
|
732
829
|
fn: lameFn,
|
|
733
830
|
period: TWO_PI9,
|
|
734
831
|
speed: 1,
|
|
735
|
-
skeleton: "live"
|
|
832
|
+
skeleton: "live",
|
|
736
833
|
};
|
|
737
834
|
|
|
738
835
|
// src/curves/rose3.ts
|
|
@@ -741,14 +838,14 @@ function rose3Fn(t, _time, _params) {
|
|
|
741
838
|
const r = Math.cos(3 * t);
|
|
742
839
|
return {
|
|
743
840
|
x: r * Math.cos(t),
|
|
744
|
-
y: r * Math.sin(t)
|
|
841
|
+
y: r * Math.sin(t),
|
|
745
842
|
};
|
|
746
843
|
}
|
|
747
844
|
var rose3 = {
|
|
748
845
|
name: "Rose (n=3)",
|
|
749
846
|
fn: rose3Fn,
|
|
750
847
|
period: TWO_PI10,
|
|
751
|
-
speed: 1.15
|
|
848
|
+
speed: 1.15,
|
|
752
849
|
};
|
|
753
850
|
|
|
754
851
|
// src/curves/rose5.ts
|
|
@@ -757,14 +854,14 @@ function rose5Fn(t, _time, _params) {
|
|
|
757
854
|
const r = Math.cos(5 * t);
|
|
758
855
|
return {
|
|
759
856
|
x: r * Math.cos(t),
|
|
760
|
-
y: r * Math.sin(t)
|
|
857
|
+
y: r * Math.sin(t),
|
|
761
858
|
};
|
|
762
859
|
}
|
|
763
860
|
var rose5 = {
|
|
764
861
|
name: "Rose (n=5)",
|
|
765
862
|
fn: rose5Fn,
|
|
766
863
|
period: TWO_PI11,
|
|
767
|
-
speed: 1
|
|
864
|
+
speed: 1,
|
|
768
865
|
};
|
|
769
866
|
|
|
770
867
|
// src/curves/index.ts
|
|
@@ -778,7 +875,7 @@ var curves = {
|
|
|
778
875
|
lissajous32,
|
|
779
876
|
lissajous43,
|
|
780
877
|
epicycloid3,
|
|
781
|
-
lame
|
|
878
|
+
lame,
|
|
782
879
|
};
|
|
783
880
|
|
|
784
881
|
// src/index.ts
|
|
@@ -795,8 +892,7 @@ function parsePalette(value) {
|
|
|
795
892
|
if (Array.isArray(parsed)) {
|
|
796
893
|
return parsed;
|
|
797
894
|
}
|
|
798
|
-
} catch {
|
|
799
|
-
}
|
|
895
|
+
} catch {}
|
|
800
896
|
return value;
|
|
801
897
|
}
|
|
802
898
|
function init() {
|
|
@@ -810,18 +906,17 @@ function init() {
|
|
|
810
906
|
if (!curveDef) {
|
|
811
907
|
return console.error(`[sarmal] "${curveName}" is not a valid curve name`);
|
|
812
908
|
}
|
|
813
|
-
|
|
814
|
-
...canvas.dataset.trailColor && { trailColor: canvas.dataset.trailColor },
|
|
815
|
-
...canvas.dataset.skeletonColor && { skeletonColor: canvas.dataset.skeletonColor },
|
|
816
|
-
...canvas.dataset.headColor && { headColor: canvas.dataset.headColor },
|
|
817
|
-
...canvas.dataset.headRadius && { headRadius: parseFloat(canvas.dataset.headRadius) },
|
|
818
|
-
...canvas.dataset.trailLength && { trailLength: parseInt(canvas.dataset.trailLength, 10) },
|
|
819
|
-
...canvas.dataset.trailStyle && {
|
|
820
|
-
trailStyle: canvas.dataset.trailStyle
|
|
821
|
-
},
|
|
822
|
-
...canvas.dataset.palette && { palette: parsePalette(canvas.dataset.palette) }
|
|
909
|
+
createSarmal(canvas, curveDef, {
|
|
910
|
+
...(canvas.dataset.trailColor && { trailColor: canvas.dataset.trailColor }),
|
|
911
|
+
...(canvas.dataset.skeletonColor && { skeletonColor: canvas.dataset.skeletonColor }),
|
|
912
|
+
...(canvas.dataset.headColor && { headColor: canvas.dataset.headColor }),
|
|
913
|
+
...(canvas.dataset.headRadius && { headRadius: parseFloat(canvas.dataset.headRadius) }),
|
|
914
|
+
...(canvas.dataset.trailLength && { trailLength: parseInt(canvas.dataset.trailLength, 10) }),
|
|
915
|
+
...(canvas.dataset.trailStyle && {
|
|
916
|
+
trailStyle: canvas.dataset.trailStyle,
|
|
917
|
+
}),
|
|
918
|
+
...(canvas.dataset.palette && { palette: parsePalette(canvas.dataset.palette) }),
|
|
823
919
|
});
|
|
824
|
-
sarmal.start();
|
|
825
920
|
});
|
|
826
921
|
}
|
|
827
922
|
if (document.readyState === "loading") {
|
|
@@ -832,4 +927,4 @@ if (document.readyState === "loading") {
|
|
|
832
927
|
requestAnimationFrame(init);
|
|
833
928
|
}
|
|
834
929
|
//# sourceMappingURL=auto-init.cjs.map
|
|
835
|
-
//# sourceMappingURL=auto-init.cjs.map
|
|
930
|
+
//# sourceMappingURL=auto-init.cjs.map
|