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