@sarmal/core 0.7.1 → 0.9.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/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright 2026 Sarmal
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,7 +1,93 @@
1
- # Sarmal Core
1
+ # @sarmal/core
2
2
 
3
- ## Publishing
3
+ <p align="center">
4
+ <strong>Parametric curve animations for loading/thinking indicators</strong>
5
+ </p>
6
+
7
+ <p align="center">
8
+ <a href="https://sarmal.art" target="_blank">Live Demo at sarmal.art</a>
9
+ </p>
10
+
11
+ ---
12
+
13
+ **@sarmal/core** is a lightweight library for rendering elegant parametric curve animations.
14
+
15
+ The animations can be used anywhere you want. Use it for loading spinners, progress indicators, or to indicate that your very special AI model is _thinking_, up to you.
16
+
17
+ In web applications and perhaps also in the terminal maybe if possible!
18
+
19
+ - **Canvas & SVG renderers**: choose one or the other, but why not both?
20
+ - **standard curves**: default cliche curves any LLM can generate in seconds, from classic spirals to custom parametric paths
21
+ - **TIME CONTROL**: programmatic time stepping, seeking, and trail effects
22
+ - **Zero dependencies**: tiny bundle, quick to get started
23
+ - **TypeScript-first**: because who would build anyhing complex in pure JS?!
24
+ - full type safety, but no assurance it will work in runtime!
25
+
26
+ ## Install
4
27
 
5
28
  ```bash
6
- npm publish --access public
29
+ npm install @sarmal/core
30
+ ```
31
+
32
+ Or use directly from CDN:
33
+
34
+ ```html
35
+ <script type="module">
36
+ import { createSarmal, curves } from "https://cdn.jsdelivr.net/npm/@sarmal/core/+esm";
37
+ // your code here
38
+ </script>
39
+ ```
40
+
41
+ ## Quick Start
42
+
43
+ ```javascript
44
+ import { createSarmal, curves } from "@sarmal/core";
45
+
46
+ const canvas = document.getElementById("my-canvas");
47
+ const sarmal = createSarmal(canvas, curves.artemis2, {
48
+ trailLength: 30,
49
+ strokeStyle: "#00ffaa",
50
+ lineWidth: 2,
51
+ });
52
+
53
+ sarmal.start();
54
+ ```
55
+
56
+ Or with **auto-init** without having to write any JS:
57
+
58
+ ```html
59
+ <script src="https://cdn.jsdelivr.net/npm/@sarmal/core/dist/auto-init.js"></script>
60
+ <canvas data-sarmal="artemis2" width="200" height="200"></canvas>
7
61
  ```
62
+
63
+ ## Standard Curves
64
+
65
+ | Name | Description |
66
+ | -------------- | ------------------------------------------ |
67
+ | `artemis2` | Artemis II free-return lunar trajectory |
68
+ | `epitrochoid7` | 7-lobed epitrochoid with dynamic variation |
69
+ | `astroid` | 4-cusped astroid |
70
+ | `deltoid` | 3-cusped deltoid |
71
+ | `rose5` | 5-petal rose curve |
72
+ | `rose3` | 3-petal rose curve |
73
+ | `lissajous32` | Lissajous 3:2 with live skeleton |
74
+ | `lissajous43` | Lissajous 4:3 with live skeleton |
75
+ | `epicycloid3` | 3-cusped epicycloid |
76
+ | `lame` | Lamé curve with live skeleton |
77
+
78
+ ## Documentation
79
+
80
+ Full API reference, examples, SVG renderer usage, engine time control (`seek`, `seekWithTrail`), custom curve definitions, and framework guides are available at [sarmal.art/docs](https://sarmal.art/docs)
81
+
82
+ ## Inspiration
83
+
84
+ Inspired by [@bbssppllvv's tweet](https://x.com/bbssppllvv/status/2038718410318659763)
85
+
86
+ ## License
87
+
88
+ MIT © [Alper Halil](https://aktasalper.com)
89
+
90
+ ## Links
91
+
92
+ - [Homepage](https://sarmal.art): See all curves in action
93
+ - [npm](https://www.npmjs.com/package/@sarmal/core): Package registry
@@ -188,21 +188,90 @@ function createEngine(curveDef, trailLength = 120) {
188
188
  // src/renderer.ts
189
189
  var DEFAULT_MORPH_DURATION_MS = 300;
190
190
  var DEFAULT_HEAD_RADIUS = 4;
191
- var DEFAULT_GLOW_SIZE = 20;
192
191
  var DEFAULT_SKELETON_COLOR = "#ffffff";
193
192
  var DEFAULT_SKELETON_OPACITY = 0.15;
194
193
  var FIT_PADDING = 0.1;
195
- var TRAIL_BATCH_SIZE = 20;
196
194
  var TRAIL_FADE_CURVE = 1.5;
197
195
  var TRAIL_MAX_OPACITY = 0.88;
198
196
  var TRAIL_MIN_WIDTH = 0.5;
199
197
  var TRAIL_MAX_WIDTH = 2.5;
200
- var GLOW_INNER_EDGE = 0.4;
201
- var GLOW_FALLOFF_OPACITY = 0.53;
198
+ var GRADIENT = {
199
+ bard: ["#a855f7", "#3b82f6", "#14b8a6", "#ec4899"],
200
+ sunset: ["#f97316", "#dc2626", "#9333ea", "#f472b6"],
201
+ ocean: ["#1e3a8a", "#06b6d4", "#22d3ee", "#e0f2fe"],
202
+ ice: ["#1e3a8a", "#67e8f9"],
203
+ fire: ["#7f1d1d", "#fbbf24"],
204
+ forest: ["#14532d", "#86efac"]
205
+ };
206
+ var PRESETS = {
207
+ bard: GRADIENT.bard,
208
+ sunset: GRADIENT.sunset,
209
+ ocean: GRADIENT.ocean,
210
+ ice: GRADIENT.ice,
211
+ fire: GRADIENT.fire,
212
+ forest: GRADIENT.forest
213
+ };
214
+ function hexToRgb(hex) {
215
+ const n = parseInt(hex.slice(1), 16);
216
+ return { r: n >> 16, g: n >> 8 & 255, b: n & 255 };
217
+ }
218
+ var lerpRgb = (a, b, t) => ({
219
+ r: Math.round(a.r + (b.r - a.r) * t),
220
+ g: Math.round(a.g + (b.g - a.g) * t),
221
+ b: Math.round(a.b + (b.b - a.b) * t)
222
+ });
223
+ function getPaletteColor(palette, position, timeOffset = 0) {
224
+ if (palette.length === 0) return { r: 255, g: 255, b: 255 };
225
+ if (palette.length === 1) return hexToRgb(palette[0]);
226
+ const cyclePos = (position + timeOffset) % 1;
227
+ const scaled = cyclePos * palette.length;
228
+ const idx = Math.floor(scaled);
229
+ const t = scaled - idx;
230
+ const c1 = hexToRgb(palette[idx % palette.length]);
231
+ const c2 = hexToRgb(palette[(idx + 1) % palette.length]);
232
+ return lerpRgb(c1, c2, t);
233
+ }
234
+ function resolvePalette(palette, trailStyle) {
235
+ if (Array.isArray(palette)) return palette;
236
+ if (palette && palette in PRESETS) return PRESETS[palette];
237
+ return trailStyle === "gradient-animated" ? GRADIENT.bard : GRADIENT.ice;
238
+ }
202
239
  function hexToRgbComponents(hex) {
203
240
  const n = parseInt(hex.slice(1), 16);
204
241
  return `${n >> 16},${n >> 8 & 255},${n & 255}`;
205
242
  }
243
+ function computeTangent(trail, i) {
244
+ const count = trail.length;
245
+ if (count < 2) {
246
+ return { x: 1, y: 0 };
247
+ }
248
+ if (i === 0) {
249
+ const dx2 = trail[1].x - trail[0].x;
250
+ const dy2 = trail[1].y - trail[0].y;
251
+ const len2 = Math.sqrt(dx2 * dx2 + dy2 * dy2) || 1;
252
+ return { x: dx2 / len2, y: dy2 / len2 };
253
+ }
254
+ if (i === count - 1) {
255
+ const dx2 = trail[count - 1].x - trail[count - 2].x;
256
+ const dy2 = trail[count - 1].y - trail[count - 2].y;
257
+ const len2 = Math.sqrt(dx2 * dx2 + dy2 * dy2) || 1;
258
+ return { x: dx2 / len2, y: dy2 / len2 };
259
+ }
260
+ const dx = trail[i + 1].x - trail[i - 1].x;
261
+ const dy = trail[i + 1].y - trail[i - 1].y;
262
+ const len = Math.sqrt(dx * dx + dy * dy) || 1;
263
+ return { x: dx / len, y: dy / len };
264
+ }
265
+ function computeNormal(trail, i) {
266
+ const tangent = computeTangent(trail, i);
267
+ return { x: -tangent.y, y: tangent.x };
268
+ }
269
+ function applyDprSizing(target, logicalWidth, logicalHeight, dpr) {
270
+ target.style.width = `${logicalWidth}px`;
271
+ target.style.height = `${logicalHeight}px`;
272
+ target.width = logicalWidth * dpr;
273
+ target.height = logicalHeight * dpr;
274
+ }
206
275
  function createRenderer(options) {
207
276
  const canvas = options.canvas;
208
277
  if (!canvas.getContext("2d")) {
@@ -214,11 +283,22 @@ function createRenderer(options) {
214
283
  skeletonColor: options.skeletonColor ?? DEFAULT_SKELETON_COLOR,
215
284
  trailColor: options.trailColor ?? "#ffffff",
216
285
  headColor: options.headColor ?? "#ffffff",
217
- headRadius: options.headRadius ?? DEFAULT_HEAD_RADIUS,
218
- glowSize: options.glowSize ?? DEFAULT_GLOW_SIZE
286
+ headRadius: options.headRadius ?? DEFAULT_HEAD_RADIUS
219
287
  };
288
+ const trailStyle = options.trailStyle ?? "default";
289
+ const palette = resolvePalette(options.palette, trailStyle);
220
290
  const trailRgb = hexToRgbComponents(opts.trailColor);
221
- const headRgbFalloff = `rgba(${hexToRgbComponents(opts.headColor)},${GLOW_FALLOFF_OPACITY})`;
291
+ const dpr = typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1;
292
+ function setupCanvas() {
293
+ const rect = canvas.getBoundingClientRect();
294
+ const lw = rect.width || 200;
295
+ const lh = rect.height || 200;
296
+ applyDprSizing(canvas, lw, lh, dpr);
297
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
298
+ }
299
+ setupCanvas();
300
+ let logicalWidth = canvas.width / dpr;
301
+ let logicalHeight = canvas.height / dpr;
222
302
  let skeleton = [];
223
303
  let skeletonCanvas = null;
224
304
  let trail = [];
@@ -232,6 +312,7 @@ function createRenderer(options) {
232
312
  let morphResolve = null;
233
313
  let morphDurationMs = DEFAULT_MORPH_DURATION_MS;
234
314
  let morphAlpha = 0;
315
+ let gradientAnimTime = 0;
235
316
  function computeBoundaries(pts) {
236
317
  if (pts.length === 0) return null;
237
318
  const first = pts[0];
@@ -244,17 +325,15 @@ function createRenderer(options) {
244
325
  }
245
326
  const width = maxX - minX;
246
327
  const height = maxY - minY;
247
- const canvasWidth = canvas.width;
248
- const canvasHeight = canvas.height;
249
- const scaleX = canvasWidth / (width * (1 + FIT_PADDING * 2));
250
- const scaleY = canvasHeight / (height * (1 + FIT_PADDING * 2));
328
+ const scaleX = logicalWidth / (width * (1 + FIT_PADDING * 2));
329
+ const scaleY = logicalHeight / (height * (1 + FIT_PADDING * 2));
251
330
  const s = Math.min(scaleX, scaleY);
252
331
  const boundsWidth = width * s;
253
332
  const boundsHeight = height * s;
254
333
  return {
255
334
  scale: s,
256
- offsetX: (canvasWidth - boundsWidth) / 2 - minX * s,
257
- offsetY: (canvasHeight - boundsHeight) / 2 - minY * s
335
+ offsetX: (logicalWidth - boundsWidth) / 2 - minX * s,
336
+ offsetY: (logicalHeight - boundsHeight) / 2 - minY * s
258
337
  };
259
338
  }
260
339
  function calculateBoundaries() {
@@ -269,6 +348,7 @@ function createRenderer(options) {
269
348
  if (skeleton.length < 2) return;
270
349
  skeletonCanvas = new OffscreenCanvas(canvas.width, canvas.height);
271
350
  const skeletonCtx = skeletonCanvas.getContext("2d");
351
+ skeletonCtx.setTransform(dpr, 0, 0, dpr, 0, 0);
272
352
  skeletonCtx.strokeStyle = `rgba(${hexToRgbComponents(opts.skeletonColor)},${DEFAULT_SKELETON_OPACITY})`;
273
353
  skeletonCtx.lineWidth = 1.5;
274
354
  skeletonCtx.beginPath();
@@ -314,32 +394,47 @@ function createRenderer(options) {
314
394
  }
315
395
  ctx.stroke();
316
396
  } else if (skeletonCanvas) {
317
- ctx.drawImage(skeletonCanvas, 0, 0);
397
+ ctx.drawImage(skeletonCanvas, 0, 0, logicalWidth, logicalHeight);
318
398
  }
319
399
  }
320
400
  function drawTrail() {
321
401
  if (trailCount < 2) {
322
402
  return;
323
403
  }
324
- ctx.lineJoin = "round";
325
- ctx.lineCap = "round";
326
- for (let batchIndex = 0; batchIndex < trailCount - 1; batchIndex += TRAIL_BATCH_SIZE) {
327
- const bEnd = Math.min(batchIndex + TRAIL_BATCH_SIZE, trailCount - 1);
328
- const progress = (batchIndex + bEnd) / 2 / (trailCount - 1);
404
+ for (let i = 0; i < trailCount - 1; i++) {
405
+ const progress = i / (trailCount - 1);
406
+ const nextProgress = (i + 1) / (trailCount - 1);
329
407
  const alpha = Math.pow(progress, TRAIL_FADE_CURVE) * TRAIL_MAX_OPACITY;
330
- const lineWidth = TRAIL_MIN_WIDTH + progress * (TRAIL_MAX_WIDTH - TRAIL_MIN_WIDTH);
331
- ctx.beginPath();
332
- for (let i = batchIndex; i <= bEnd; i++) {
333
- const point = trail[i];
334
- if (i === batchIndex) {
335
- ctx.moveTo(point.x * scale + offsetX, point.y * scale + offsetY);
336
- } else {
337
- ctx.lineTo(point.x * scale + offsetX, point.y * scale + offsetY);
338
- }
408
+ const width = TRAIL_MIN_WIDTH + progress * (TRAIL_MAX_WIDTH - TRAIL_MIN_WIDTH);
409
+ const nextWidth = TRAIL_MIN_WIDTH + nextProgress * (TRAIL_MAX_WIDTH - TRAIL_MIN_WIDTH);
410
+ const curr = trail[i];
411
+ const next = trail[i + 1];
412
+ const n0 = computeNormal(trail, i);
413
+ const n1 = computeNormal(trail, i + 1);
414
+ const halfW0 = width / 2;
415
+ const halfW1 = nextWidth / 2;
416
+ const l0x = curr.x * scale + offsetX + n0.x * halfW0;
417
+ const l0y = curr.y * scale + offsetY + n0.y * halfW0;
418
+ const r0x = curr.x * scale + offsetX - n0.x * halfW0;
419
+ const r0y = curr.y * scale + offsetY - n0.y * halfW0;
420
+ const l1x = next.x * scale + offsetX + n1.x * halfW1;
421
+ const l1y = next.y * scale + offsetY + n1.y * halfW1;
422
+ const r1x = next.x * scale + offsetX - n1.x * halfW1;
423
+ const r1y = next.y * scale + offsetY - n1.y * halfW1;
424
+ if (trailStyle === "default") {
425
+ ctx.fillStyle = `rgba(${trailRgb},${alpha})`;
426
+ } else {
427
+ const timeOffset = trailStyle === "gradient-animated" ? gradientAnimTime * 5e-4 : 0;
428
+ const color = getPaletteColor(palette, progress, timeOffset);
429
+ ctx.fillStyle = `rgba(${color.r},${color.g},${color.b},${alpha})`;
339
430
  }
340
- ctx.strokeStyle = `rgba(${trailRgb},${alpha})`;
341
- ctx.lineWidth = lineWidth;
342
- ctx.stroke();
431
+ ctx.beginPath();
432
+ ctx.moveTo(l0x, l0y);
433
+ ctx.lineTo(l1x, l1y);
434
+ ctx.lineTo(r1x, r1y);
435
+ ctx.lineTo(r0x, r0y);
436
+ ctx.closePath();
437
+ ctx.fill();
343
438
  }
344
439
  }
345
440
  function drawHead() {
@@ -348,14 +443,6 @@ function createRenderer(options) {
348
443
  }
349
444
  const x = head.x * scale + offsetX;
350
445
  const y = head.y * scale + offsetY;
351
- const gradient = ctx.createRadialGradient(x, y, 0, x, y, opts.glowSize);
352
- gradient.addColorStop(0, opts.headColor);
353
- gradient.addColorStop(GLOW_INNER_EDGE, headRgbFalloff);
354
- gradient.addColorStop(1, "transparent");
355
- ctx.fillStyle = gradient;
356
- ctx.beginPath();
357
- ctx.arc(x, y, opts.glowSize, 0, Math.PI * 2);
358
- ctx.fill();
359
446
  ctx.fillStyle = opts.headColor;
360
447
  ctx.beginPath();
361
448
  ctx.arc(x, y, opts.headRadius, 0, Math.PI * 2);
@@ -365,6 +452,9 @@ function createRenderer(options) {
365
452
  const now = performance.now();
366
453
  const deltaTime = Math.min((now - lastTime) / 1e3, 1 / 30);
367
454
  lastTime = now;
455
+ if (trailStyle === "gradient-animated") {
456
+ gradientAnimTime += deltaTime * 1e3;
457
+ }
368
458
  if (engine.morphAlpha !== null) {
369
459
  morphAlpha = Math.min(1, morphAlpha + deltaTime / (morphDurationMs / 1e3));
370
460
  engine.setMorphAlpha(morphAlpha);
@@ -389,7 +479,7 @@ function createRenderer(options) {
389
479
  trail = engine.tick(deltaTime);
390
480
  trailCount = engine.trailCount;
391
481
  head = trailCount > 0 ? trail[trailCount - 1] : null;
392
- ctx.clearRect(0, 0, canvas.width, canvas.height);
482
+ ctx.clearRect(0, 0, logicalWidth, logicalHeight);
393
483
  if (engine.isLiveSkeleton && engine.morphAlpha === null) {
394
484
  skeleton = engine.getSarmalSkeleton();
395
485
  calculateBoundaries();
@@ -625,7 +715,6 @@ function init() {
625
715
  ...canvas.dataset.skeletonColor && { skeletonColor: canvas.dataset.skeletonColor },
626
716
  ...canvas.dataset.headColor && { headColor: canvas.dataset.headColor },
627
717
  ...canvas.dataset.headRadius && { headRadius: parseFloat(canvas.dataset.headRadius) },
628
- ...canvas.dataset.glowSize && { glowSize: parseInt(canvas.dataset.glowSize, 10) },
629
718
  ...canvas.dataset.trailLength && { trailLength: parseInt(canvas.dataset.trailLength, 10) }
630
719
  });
631
720
  sarmal.start();