@prtcl/plonk 1.0.10 → 1.1.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
@@ -24,6 +24,7 @@ var index_exports = {};
24
24
  __export(index_exports, {
25
25
  Drunk: () => Drunk,
26
26
  Env: () => Env,
27
+ Fold: () => Fold,
27
28
  Frames: () => Frames,
28
29
  MS_IN_HOUR: () => MS_IN_HOUR,
29
30
  MS_IN_MINUTE: () => MS_IN_MINUTE,
@@ -35,11 +36,21 @@ __export(index_exports, {
35
36
  Scale: () => Scale,
36
37
  Sine: () => Sine,
37
38
  TimeFormat: () => TimeFormat,
39
+ Wrap: () => Wrap,
38
40
  clamp: () => clamp,
41
+ drunk: () => drunk,
42
+ env: () => env,
39
43
  expo: () => expo,
44
+ fold: () => fold,
45
+ frames: () => frames,
46
+ metro: () => metro,
40
47
  ms: () => ms,
41
48
  now: () => now,
42
- wait: () => wait
49
+ rand: () => rand,
50
+ scale: () => scale,
51
+ sine: () => sine,
52
+ wait: () => wait,
53
+ wrap: () => wrap
43
54
  });
44
55
  module.exports = __toCommonJS(index_exports);
45
56
 
@@ -59,7 +70,7 @@ function clamp(n, min, max) {
59
70
  return Math.min(Math.max(n, a), b);
60
71
  }
61
72
 
62
- // src/Rand.ts
73
+ // src/rand.ts
63
74
  var parseOptions = (opts) => {
64
75
  return {
65
76
  min: 0,
@@ -74,9 +85,11 @@ var Rand = class _Rand {
74
85
  this.state = { max, min, value: 0 };
75
86
  this.next();
76
87
  }
88
+ /** Returns a single random value. One-off form of `new Rand(opts).value()`. */
77
89
  static rand(opts) {
78
90
  return new _Rand(opts).value();
79
91
  }
92
+ /** Updates the range bounds, clamping the current value if needed. */
80
93
  setRange(partialOpts) {
81
94
  const { value = 0 } = this.state;
82
95
  const { min, max } = {
@@ -90,9 +103,11 @@ var Rand = class _Rand {
90
103
  value: clamp(value, min, max)
91
104
  };
92
105
  }
106
+ /** Returns the current value. */
93
107
  value() {
94
108
  return this.state.value;
95
109
  }
110
+ /** Generates a new random value within the range. */
96
111
  next() {
97
112
  const { min, max } = this.state;
98
113
  const updates = Math.random() * (max - min) + min;
@@ -100,8 +115,9 @@ var Rand = class _Rand {
100
115
  return updates;
101
116
  }
102
117
  };
118
+ var rand = Rand.rand;
103
119
 
104
- // src/Drunk.ts
120
+ // src/drunk.ts
105
121
  var DEFAULT_DRUNK_STEP = 0.1;
106
122
  var parseStepSize = (step) => typeof step !== "undefined" ? clamp(step, 0, 1) : DEFAULT_DRUNK_STEP;
107
123
  var parseOptions2 = (opts) => {
@@ -115,7 +131,7 @@ var parseOptions2 = (opts) => {
115
131
  ...restOpts
116
132
  };
117
133
  };
118
- var Drunk = class {
134
+ var Drunk = class _Drunk {
119
135
  constructor(opts) {
120
136
  __publicField(this, "state");
121
137
  __publicField(this, "_initialValue");
@@ -132,6 +148,11 @@ var Drunk = class {
132
148
  value: initialValue
133
149
  };
134
150
  }
151
+ /** Creates a new Drunk instance. Alternative form of `new Drunk(opts)`. */
152
+ static drunk(opts) {
153
+ return new _Drunk(opts);
154
+ }
155
+ /** Updates the walk bounds, clamping the current value if needed. */
135
156
  setRange(partialOpts) {
136
157
  const { max, min } = {
137
158
  min: this.state.min,
@@ -152,6 +173,7 @@ var Drunk = class {
152
173
  }
153
174
  };
154
175
  }
176
+ /** Updates the maximum step size. */
155
177
  setStepSize(partialOpts) {
156
178
  const step = parseStepSize(partialOpts?.step);
157
179
  this.state = {
@@ -159,6 +181,7 @@ var Drunk = class {
159
181
  step
160
182
  };
161
183
  }
184
+ /** Resets the walk with optional new options. */
162
185
  reset(opts) {
163
186
  const { min, max, startsAt, step } = parseOptions2(opts);
164
187
  this.setRange({ min, max });
@@ -170,9 +193,11 @@ var Drunk = class {
170
193
  value: initialValue
171
194
  };
172
195
  }
196
+ /** Returns the current value. */
173
197
  value() {
174
198
  return this.state.value;
175
199
  }
200
+ /** Advances one step and returns the new value. */
176
201
  next() {
177
202
  const { min, max, step, value } = this.state;
178
203
  const updates = clamp(value + max * this._step.next() * step, min, max);
@@ -180,8 +205,29 @@ var Drunk = class {
180
205
  return updates;
181
206
  }
182
207
  };
208
+ var drunk = Drunk.drunk;
183
209
 
184
- // src/Scale.ts
210
+ // src/now.ts
211
+ var innerNow = (() => {
212
+ if (typeof performance !== "undefined" && "now" in performance) {
213
+ return () => performance.now();
214
+ }
215
+ if (typeof process === "object" && process.toString() === "[object process]") {
216
+ const ts = () => {
217
+ const hr = process.hrtime();
218
+ return hr[0] * 1e9 + hr[1];
219
+ };
220
+ const initialNow2 = ts();
221
+ return () => (ts() - initialNow2) / 1e6;
222
+ }
223
+ const initialNow = Date.now();
224
+ return () => Date.now() - initialNow;
225
+ })();
226
+ function now() {
227
+ return innerNow();
228
+ }
229
+
230
+ // src/scale.ts
185
231
  var computeRatio = (from, to) => (to.max - to.min) / (from.max - from.min);
186
232
  var parseInitialState = (opts) => {
187
233
  const initialRange = {
@@ -225,18 +271,23 @@ var Scale = class _Scale {
225
271
  __publicField(this, "state");
226
272
  this.state = parseInitialState(opts);
227
273
  }
274
+ /** Scales a single value. One-off form of `new Scale(opts).scale(n)`. */
228
275
  static scale(n, opts) {
229
276
  return new _Scale(opts).scale(n);
230
277
  }
278
+ /** Updates input and/or output ranges, recomputing the internal ratio. */
231
279
  setRanges(opts) {
232
280
  this.state = updateStateFromOptions(opts, this.state);
233
281
  }
282
+ /** Resets with new range options. */
234
283
  reset(opts) {
235
284
  this.state = parseInitialState(opts);
236
285
  }
286
+ /** Returns the last scaled value. */
237
287
  value() {
238
288
  return this.state.value;
239
289
  }
290
+ /** Maps a value from the input range to the output range. */
240
291
  scale(n) {
241
292
  const { from, to, ratio } = this.state;
242
293
  const updates = to.min + (clamp(n, from.min, from.max) - from.min) * ratio;
@@ -244,28 +295,9 @@ var Scale = class _Scale {
244
295
  return updates;
245
296
  }
246
297
  };
298
+ var scale = Scale.scale;
247
299
 
248
- // src/now.ts
249
- var innerNow = (() => {
250
- if (typeof performance !== "undefined" && "now" in performance) {
251
- return () => performance.now();
252
- }
253
- if (typeof process === "object" && process.toString() === "[object process]") {
254
- const ts = () => {
255
- const hr = process.hrtime();
256
- return hr[0] * 1e9 + hr[1];
257
- };
258
- const initialNow2 = ts();
259
- return () => (ts() - initialNow2) / 1e6;
260
- }
261
- const initialNow = Date.now();
262
- return () => Date.now() - initialNow;
263
- })();
264
- function now() {
265
- return innerNow();
266
- }
267
-
268
- // src/Env.ts
300
+ // src/env.ts
269
301
  var parseOptions3 = (opts) => {
270
302
  return {
271
303
  duration: 0,
@@ -297,7 +329,7 @@ var updateStateFromOptions2 = (opts, prevState) => {
297
329
  totalElapsed: 0
298
330
  };
299
331
  };
300
- var Env = class {
332
+ var Env = class _Env {
301
333
  constructor(opts) {
302
334
  __publicField(this, "state");
303
335
  __publicField(this, "_interpolator");
@@ -314,6 +346,11 @@ var Env = class {
314
346
  }
315
347
  });
316
348
  }
349
+ /** Creates a new Env instance. Alternative form of `new Env(opts)`. */
350
+ static env(opts) {
351
+ return new _Env(opts);
352
+ }
353
+ /** Updates the envelope duration. */
317
354
  setDuration(duration) {
318
355
  const { to, totalElapsed } = this.state;
319
356
  this.state = {
@@ -324,6 +361,7 @@ var Env = class {
324
361
  } : { duration }
325
362
  };
326
363
  }
364
+ /** Restarts the envelope with optional new options. */
327
365
  reset(opts) {
328
366
  const updates = updateStateFromOptions2(opts, this.state);
329
367
  this.state = {
@@ -342,9 +380,11 @@ var Env = class {
342
380
  }
343
381
  });
344
382
  }
383
+ /** Returns true when the envelope has completed. */
345
384
  done() {
346
385
  return this.state.duration <= this.state.totalElapsed;
347
386
  }
387
+ /** Returns the current interpolated value. */
348
388
  value() {
349
389
  const { to, value } = this.state;
350
390
  if (this.done()) {
@@ -352,6 +392,7 @@ var Env = class {
352
392
  }
353
393
  return value;
354
394
  }
395
+ /** Advances the envelope and returns the new value. */
355
396
  next() {
356
397
  if (this.done()) {
357
398
  return this.value();
@@ -367,8 +408,73 @@ var Env = class {
367
408
  return updates;
368
409
  }
369
410
  };
411
+ var env = Env.env;
370
412
 
371
- // src/Sine.ts
413
+ // src/fold.ts
414
+ var parseOptions4 = (opts) => {
415
+ return {
416
+ min: 0,
417
+ max: 1,
418
+ ...opts
419
+ };
420
+ };
421
+ var Fold = class _Fold {
422
+ constructor(opts) {
423
+ __publicField(this, "state");
424
+ const { min, max } = parseOptions4(opts);
425
+ this.state = { min, max, value: min };
426
+ }
427
+ /** Folds a single value. One-off form of `new Fold(opts).fold(n)`. */
428
+ static fold(n, opts) {
429
+ return new _Fold(opts).fold(n);
430
+ }
431
+ /** Updates the range bounds, folding the current value into the new range. */
432
+ setRange(partialOpts) {
433
+ const { min, max } = { ...this.state, ...partialOpts };
434
+ this.state = {
435
+ ...this.state,
436
+ min,
437
+ max,
438
+ value: transform(this.state.value, min, max)
439
+ };
440
+ }
441
+ /** Returns the last folded value. */
442
+ value() {
443
+ return this.state.value;
444
+ }
445
+ /** Folds a value into the configured range and caches the result. */
446
+ fold(n) {
447
+ const { min, max } = this.state;
448
+ const updates = transform(n, min, max);
449
+ this.state.value = updates;
450
+ return updates;
451
+ }
452
+ };
453
+ var fold = Fold.fold;
454
+ function transform(n, min, max) {
455
+ let a = 0;
456
+ let b = 1;
457
+ if (typeof min === "number") {
458
+ if (typeof max === "number") {
459
+ a = min;
460
+ b = max;
461
+ } else {
462
+ a = 0;
463
+ b = min;
464
+ }
465
+ }
466
+ const lo = Math.min(a, b);
467
+ const hi = Math.max(a, b);
468
+ const range = hi - lo;
469
+ if (range === 0) return lo;
470
+ const period = range * 2;
471
+ const offset = n - lo;
472
+ const normalized = (offset % period + period) % period;
473
+ const value = normalized <= range ? lo + normalized : lo + period - normalized;
474
+ return clamp(value, lo, hi);
475
+ }
476
+
477
+ // src/sine.ts
372
478
  var SINE_PERIOD = Math.PI * 2 - 1e-4;
373
479
  var getInitialState2 = (duration) => ({
374
480
  cycle: 0,
@@ -377,7 +483,7 @@ var getInitialState2 = (duration) => ({
377
483
  totalElapsed: 0,
378
484
  value: 0
379
485
  });
380
- var Sine = class {
486
+ var Sine = class _Sine {
381
487
  constructor(opts) {
382
488
  __publicField(this, "state");
383
489
  __publicField(this, "_interpolator");
@@ -394,12 +500,18 @@ var Sine = class {
394
500
  });
395
501
  this.state = getInitialState2(duration);
396
502
  }
503
+ /** Creates a new Sine instance. Alternative form of `new Sine(opts)`. */
504
+ static sine(opts) {
505
+ return new _Sine(opts);
506
+ }
507
+ /** Updates the cycle duration. */
397
508
  setDuration(duration) {
398
509
  this.state = {
399
510
  ...this.state,
400
511
  duration
401
512
  };
402
513
  }
514
+ /** Resets the oscillator with optional new options. */
403
515
  reset(opts) {
404
516
  const { duration } = {
405
517
  ...this.state,
@@ -407,9 +519,11 @@ var Sine = class {
407
519
  };
408
520
  this.state = getInitialState2(duration);
409
521
  }
522
+ /** Returns the current oscillator value. */
410
523
  value() {
411
524
  return this.state.value;
412
525
  }
526
+ /** Advances the oscillator and returns the new value. */
413
527
  next() {
414
528
  const { cycle, duration, prev, totalElapsed: prevTotalElapsed } = this.state;
415
529
  const curr = now();
@@ -427,6 +541,70 @@ var Sine = class {
427
541
  return updates;
428
542
  }
429
543
  };
544
+ var sine = Sine.sine;
545
+
546
+ // src/wrap.ts
547
+ var parseOptions5 = (opts) => {
548
+ return {
549
+ min: 0,
550
+ max: 1,
551
+ ...opts
552
+ };
553
+ };
554
+ var Wrap = class _Wrap {
555
+ constructor(opts) {
556
+ __publicField(this, "state");
557
+ const { min, max } = parseOptions5(opts);
558
+ this.state = { min, max, value: min };
559
+ }
560
+ /** Wraps a single value. One-off form of `new Wrap(opts).wrap(n)`. */
561
+ static wrap(n, opts) {
562
+ return new _Wrap(opts).wrap(n);
563
+ }
564
+ /** Updates the range bounds, wrapping the current value into the new range. */
565
+ setRange(partialOpts) {
566
+ const { min, max } = {
567
+ ...this.state,
568
+ ...partialOpts
569
+ };
570
+ this.state = {
571
+ ...this.state,
572
+ min,
573
+ max,
574
+ value: transform2(this.state.value, min, max)
575
+ };
576
+ }
577
+ /** Returns the last wrapped value. */
578
+ value() {
579
+ return this.state.value;
580
+ }
581
+ /** Wraps a value into the configured range and caches the result. */
582
+ wrap(n) {
583
+ const { min, max } = this.state;
584
+ const updates = transform2(n, min, max);
585
+ this.state.value = updates;
586
+ return updates;
587
+ }
588
+ };
589
+ var wrap = Wrap.wrap;
590
+ function transform2(n, min, max) {
591
+ let a = 0;
592
+ let b = 1;
593
+ if (typeof min === "number") {
594
+ if (typeof max === "number") {
595
+ a = min;
596
+ b = max;
597
+ } else {
598
+ a = 0;
599
+ b = min;
600
+ }
601
+ }
602
+ const lo = Math.min(a, b);
603
+ const range = Math.max(a, b) - lo;
604
+ if (range === 0) return lo;
605
+ const offset = n - lo;
606
+ return lo + (offset % range + range) % range;
607
+ }
430
608
 
431
609
  // src/constants.ts
432
610
  var SIXTY_FPS = 1e3 / 60;
@@ -434,7 +612,7 @@ var MS_IN_SECOND = 1e3;
434
612
  var MS_IN_MINUTE = 60 * MS_IN_SECOND;
435
613
  var MS_IN_HOUR = MS_IN_MINUTE * 60;
436
614
 
437
- // src/Metro.ts
615
+ // src/metro.ts
438
616
  var getInitialState3 = (initialTime) => {
439
617
  return {
440
618
  initialTime,
@@ -468,26 +646,29 @@ var processTimerState = (state) => {
468
646
  totalElapsed: totalElapsed + tickInterval
469
647
  };
470
648
  };
471
- var parseOptions4 = (opts) => {
649
+ var parseOptions6 = (opts) => {
472
650
  return {
473
651
  time: SIXTY_FPS,
474
652
  ...opts
475
653
  };
476
654
  };
477
- var Metro = class {
655
+ var Metro = class _Metro {
478
656
  constructor(callback, opts) {
479
657
  __publicField(this, "state");
480
658
  __publicField(this, "_listeners");
659
+ /** Stops the timer and returns total elapsed time in milliseconds. */
481
660
  __publicField(this, "stop", () => {
482
661
  const { totalElapsed } = this.state;
483
662
  this.reset();
484
663
  this.clearAsyncHandler();
485
664
  return totalElapsed;
486
665
  });
666
+ /** Resets state to initial values. */
487
667
  __publicField(this, "reset", () => {
488
668
  const { initialTime } = this.state;
489
669
  this.state = getInitialState3(initialTime);
490
670
  });
671
+ /** Updates the tick interval in milliseconds. */
491
672
  __publicField(this, "setTime", (updatedTime = this.state.time) => {
492
673
  const time = Math.max(updatedTime, 0);
493
674
  this.state = {
@@ -496,6 +677,7 @@ var Metro = class {
496
677
  initialTime: time
497
678
  };
498
679
  });
680
+ /** Starts the timer loop. */
499
681
  __publicField(this, "run", () => {
500
682
  if (this.state.isRunning) {
501
683
  this.stop();
@@ -519,10 +701,14 @@ var Metro = class {
519
701
  };
520
702
  tick();
521
703
  });
522
- const { time } = parseOptions4(opts);
704
+ const { time } = parseOptions6(opts);
523
705
  this.state = getInitialState3(time);
524
706
  this._listeners = [callback];
525
707
  }
708
+ /** Creates a new Metro instance. Alternative form of `new Metro(callback, opts)`. */
709
+ static metro(callback, opts) {
710
+ return new _Metro(callback, opts);
711
+ }
526
712
  asyncHandler(callback) {
527
713
  this._timerId = setTimeout(callback, SIXTY_FPS);
528
714
  }
@@ -530,6 +716,7 @@ var Metro = class {
530
716
  clearTimeout(this._timerId);
531
717
  }
532
718
  };
719
+ var metro = Metro.metro;
533
720
 
534
721
  // src/ms.ts
535
722
  var TimeFormat = /* @__PURE__ */ ((TimeFormat2) => {
@@ -598,9 +785,9 @@ function ms(val, format) {
598
785
  return formatter(parsedValue);
599
786
  }
600
787
 
601
- // src/Frames.ts
788
+ // src/frames.ts
602
789
  var DEFAULT_FPS = 60;
603
- var parseOptions5 = (opts) => {
790
+ var parseOptions7 = (opts) => {
604
791
  const { fps } = {
605
792
  fps: DEFAULT_FPS,
606
793
  ...opts
@@ -609,13 +796,18 @@ var parseOptions5 = (opts) => {
609
796
  time: ms(fps, "fps" /* FPS */)
610
797
  };
611
798
  };
612
- var Frames = class extends Metro {
799
+ var Frames = class _Frames extends Metro {
613
800
  constructor(callback, opts) {
614
- super(() => callback(this), parseOptions5(opts));
801
+ super(() => callback(this), parseOptions7(opts));
802
+ /** Updates the target frames per second. */
615
803
  __publicField(this, "setFPS", (fps = DEFAULT_FPS) => {
616
804
  this.setTime(ms(fps, "fps" /* FPS */));
617
805
  });
618
806
  }
807
+ /** Creates a new Frames instance. Alternative form of `new Frames(callback, opts)`. */
808
+ static frames(callback, opts) {
809
+ return new _Frames(callback, opts);
810
+ }
619
811
  asyncHandler(callback) {
620
812
  if (typeof window === "undefined" || !("requestAnimationFrame" in window)) {
621
813
  super.asyncHandler(callback);
@@ -631,6 +823,7 @@ var Frames = class extends Metro {
631
823
  }
632
824
  }
633
825
  };
826
+ var frames = Frames.frames;
634
827
 
635
828
  // src/expo.ts
636
829
  function expo(n) {
@@ -645,6 +838,7 @@ function wait(time) {
645
838
  0 && (module.exports = {
646
839
  Drunk,
647
840
  Env,
841
+ Fold,
648
842
  Frames,
649
843
  MS_IN_HOUR,
650
844
  MS_IN_MINUTE,
@@ -656,10 +850,20 @@ function wait(time) {
656
850
  Scale,
657
851
  Sine,
658
852
  TimeFormat,
853
+ Wrap,
659
854
  clamp,
855
+ drunk,
856
+ env,
660
857
  expo,
858
+ fold,
859
+ frames,
860
+ metro,
661
861
  ms,
662
862
  now,
663
- wait
863
+ rand,
864
+ scale,
865
+ sine,
866
+ wait,
867
+ wrap
664
868
  });
665
869
  //# sourceMappingURL=index.cjs.map