@sarmal/core 0.19.0 → 0.20.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/index.cjs CHANGED
@@ -1444,6 +1444,55 @@ var curves = {
1444
1444
  lame,
1445
1445
  };
1446
1446
 
1447
+ // src/catmull-rom.ts
1448
+ var PERIOD = 2 * Math.PI;
1449
+ function catmullRom1D(p0, p1, p2, p3, u) {
1450
+ const u2 = u * u;
1451
+ const u3 = u2 * u;
1452
+ return (
1453
+ 0.5 *
1454
+ (2 * p1 +
1455
+ (-p0 + p2) * u +
1456
+ (2 * p0 - 5 * p1 + 4 * p2 - p3) * u2 +
1457
+ (-p0 + 3 * p1 - 3 * p2 + p3) * u3)
1458
+ );
1459
+ }
1460
+ function evaluateCatmullRom(points, t) {
1461
+ const N = points.length;
1462
+ if (N === 0) {
1463
+ return { x: 0, y: 0 };
1464
+ }
1465
+ if (N === 1) {
1466
+ return { x: points[0][0], y: points[0][1] };
1467
+ }
1468
+ t = ((t % PERIOD) + PERIOD) % PERIOD;
1469
+ const segmentSize = PERIOD / N;
1470
+ let i = Math.floor(t / segmentSize);
1471
+ if (i >= N) {
1472
+ i = N - 1;
1473
+ }
1474
+ let u = (t - i * segmentSize) / segmentSize;
1475
+ u = Math.max(0, Math.min(1, u));
1476
+ const p0 = points[(i - 1 + N) % N];
1477
+ const p1 = points[i];
1478
+ const p2 = points[(i + 1) % N];
1479
+ const p3 = points[(i + 2) % N];
1480
+ return {
1481
+ x: catmullRom1D(p0[0], p1[0], p2[0], p3[0], u),
1482
+ y: catmullRom1D(p0[1], p1[1], p2[1], p3[1], u),
1483
+ };
1484
+ }
1485
+ function drawCurve(points) {
1486
+ if (points.length < 3) {
1487
+ throw new Error(`drawCurve requires at least 3 points, received ${points.length}.`);
1488
+ }
1489
+ return {
1490
+ name: "custom",
1491
+ fn: (t) => evaluateCatmullRom(points, t),
1492
+ period: PERIOD,
1493
+ };
1494
+ }
1495
+
1447
1496
  // src/index.ts
1448
1497
  function createSarmal(canvas, curveDef, options) {
1449
1498
  const { trailLength, ...rendererOpts } = options ?? {};
@@ -1460,8 +1509,10 @@ exports.createSarmal = createSarmal;
1460
1509
  exports.createSarmalSVG = createSarmalSVG;
1461
1510
  exports.curves = curves;
1462
1511
  exports.deltoid = deltoid;
1512
+ exports.drawCurve = drawCurve;
1463
1513
  exports.epicycloid3 = epicycloid3;
1464
1514
  exports.epitrochoid7 = epitrochoid7;
1515
+ exports.evaluateCatmullRom = evaluateCatmullRom;
1465
1516
  exports.lame = lame;
1466
1517
  exports.lissajous32 = lissajous32;
1467
1518
  exports.lissajous43 = lissajous43;