@prtcl/plonk 1.0.1-beta.0 → 1.0.1-beta.1

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 ADDED
@@ -0,0 +1,680 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
20
+
21
+ // src/index.ts
22
+ var src_exports = {};
23
+ __export(src_exports, {
24
+ Drunk: () => Drunk,
25
+ Env: () => Env,
26
+ Frames: () => Frames,
27
+ MS_IN_HOUR: () => MS_IN_HOUR,
28
+ MS_IN_MINUTE: () => MS_IN_MINUTE,
29
+ MS_IN_SECOND: () => MS_IN_SECOND,
30
+ Metro: () => Metro,
31
+ Rand: () => Rand,
32
+ SINE_PERIOD: () => SINE_PERIOD,
33
+ SIXTY_FPS: () => SIXTY_FPS,
34
+ Scale: () => Scale,
35
+ Sine: () => Sine,
36
+ TimeFormat: () => TimeFormat,
37
+ clamp: () => clamp,
38
+ expo: () => expo,
39
+ flip: () => flip,
40
+ ms: () => ms,
41
+ now: () => now
42
+ });
43
+ module.exports = __toCommonJS(src_exports);
44
+
45
+ // src/utils/clamp.ts
46
+ function clamp(n, min, max) {
47
+ let a = 0;
48
+ let b = 1;
49
+ if (typeof min === "number") {
50
+ if (typeof max === "number") {
51
+ a = min;
52
+ b = max;
53
+ } else {
54
+ a = 0;
55
+ b = min;
56
+ }
57
+ }
58
+ return Math.min(Math.max(n, a), b);
59
+ }
60
+
61
+ // src/math/Rand.ts
62
+ var parseOptions = (opts) => {
63
+ return {
64
+ min: 0,
65
+ max: 1,
66
+ ...opts
67
+ };
68
+ };
69
+ var Rand = class _Rand {
70
+ constructor(opts) {
71
+ __publicField(this, "state");
72
+ const { min, max } = parseOptions(opts);
73
+ this.state = { max, min, value: void 0 };
74
+ this.next();
75
+ }
76
+ static rand(opts) {
77
+ return new _Rand(opts).value();
78
+ }
79
+ setRange(partialOpts) {
80
+ const { min, max } = {
81
+ ...this.state,
82
+ ...partialOpts
83
+ };
84
+ this.state = {
85
+ ...this.state,
86
+ max,
87
+ min,
88
+ value: clamp(this.state.value, min, max)
89
+ };
90
+ }
91
+ value() {
92
+ return this.state.value;
93
+ }
94
+ next() {
95
+ const { min, max } = this.state;
96
+ const updates = Math.random() * (max - min) + min;
97
+ this.state.value = updates;
98
+ return updates;
99
+ }
100
+ };
101
+
102
+ // src/math/Drunk.ts
103
+ var DEFAULT_DRUNK_STEP = 0.1;
104
+ var parseStepSize = (step) => typeof step !== "undefined" ? clamp(step, 0, 1) : DEFAULT_DRUNK_STEP;
105
+ var parseOptions2 = (opts) => {
106
+ const { step, ...restOpts } = opts || {};
107
+ const parsedStepSize = parseStepSize(step);
108
+ return {
109
+ max: 1,
110
+ min: 0,
111
+ startsAt: void 0,
112
+ step: parsedStepSize,
113
+ ...restOpts
114
+ };
115
+ };
116
+ var Drunk = class {
117
+ constructor(opts) {
118
+ __publicField(this, "state");
119
+ __publicField(this, "_initialValue");
120
+ __publicField(this, "_step");
121
+ const { min, max, step, startsAt } = parseOptions2(opts);
122
+ this._initialValue = new Rand({ min, max });
123
+ this._step = new Rand({ min: -1, max: 1 });
124
+ const initialValue = typeof startsAt !== "undefined" ? startsAt : this._initialValue.value();
125
+ this.state = {
126
+ initialValue,
127
+ max,
128
+ min,
129
+ step,
130
+ value: initialValue
131
+ };
132
+ }
133
+ setRange(partialOpts) {
134
+ const { max, min } = {
135
+ min: this.state.min,
136
+ max: this.state.max,
137
+ ...partialOpts
138
+ };
139
+ this._initialValue.setRange({ min, max });
140
+ this.state = {
141
+ ...this.state,
142
+ ...min !== this.state.min || max !== this.state.max ? {
143
+ initialValue: clamp(this._initialValue.value(), min, max),
144
+ max,
145
+ min,
146
+ value: clamp(this.state.value, min, max)
147
+ } : {
148
+ max,
149
+ min
150
+ }
151
+ };
152
+ }
153
+ setStepSize(partialOpts) {
154
+ const step = parseStepSize(partialOpts.step);
155
+ this.state = {
156
+ ...this.state,
157
+ step
158
+ };
159
+ }
160
+ reset(opts) {
161
+ const { min, max, startsAt, step } = parseOptions2(opts);
162
+ this.setRange({ min, max });
163
+ this.setStepSize({ step });
164
+ const initialValue = typeof startsAt !== "undefined" ? startsAt : this._initialValue.next();
165
+ this.state = {
166
+ ...this.state,
167
+ initialValue,
168
+ value: initialValue
169
+ };
170
+ }
171
+ value() {
172
+ return this.state.value;
173
+ }
174
+ next() {
175
+ const { min, max, step, value } = this.state;
176
+ const updates = clamp(value + max * this._step.next() * step, min, max);
177
+ this.state.value = updates;
178
+ return updates;
179
+ }
180
+ };
181
+
182
+ // src/utils/now.ts
183
+ var internal;
184
+ if (typeof performance !== "undefined" && "now" in performance) {
185
+ internal = () => {
186
+ return performance.now();
187
+ };
188
+ } else if (typeof process === "object" && // eslint-disable-next-line @typescript-eslint/no-base-to-string
189
+ process.toString() === "[object process]") {
190
+ const timestamp = () => {
191
+ const hr = process.hrtime();
192
+ return hr[0] * 1e9 + hr[1];
193
+ };
194
+ const initial = timestamp();
195
+ internal = () => {
196
+ return (timestamp() - initial) / 1e6;
197
+ };
198
+ } else {
199
+ const initial = Date.now();
200
+ internal = () => {
201
+ return Date.now() - initial;
202
+ };
203
+ }
204
+ function now() {
205
+ return internal();
206
+ }
207
+
208
+ // src/math/Scale.ts
209
+ var parseOptions3 = (opts) => {
210
+ const { from, to } = {
211
+ ...opts,
212
+ from: {
213
+ min: 0,
214
+ max: 1,
215
+ ...opts?.from
216
+ },
217
+ to: {
218
+ min: 0,
219
+ max: 1,
220
+ ...opts?.to
221
+ }
222
+ };
223
+ return {
224
+ from,
225
+ to
226
+ };
227
+ };
228
+ var updateStateFromOptions = (opts, prevState) => {
229
+ const { from, to } = opts;
230
+ const updatedTo = {
231
+ ...prevState.to,
232
+ ...to
233
+ };
234
+ return {
235
+ ...prevState,
236
+ from: {
237
+ ...prevState.from,
238
+ ...from
239
+ },
240
+ to: updatedTo,
241
+ value: clamp(prevState.value, updatedTo.min, updatedTo.max)
242
+ };
243
+ };
244
+ var Scale = class _Scale {
245
+ constructor(opts) {
246
+ __publicField(this, "state");
247
+ const { from, to } = parseOptions3(opts);
248
+ this.state = { from, to, value: void 0 };
249
+ }
250
+ static scale(n, opts) {
251
+ return new _Scale(opts).scale(n);
252
+ }
253
+ setRanges(opts) {
254
+ this.state = updateStateFromOptions(opts, this.state);
255
+ }
256
+ reset(opts) {
257
+ const { from, to } = parseOptions3(opts);
258
+ this.state = { from, to, value: void 0 };
259
+ }
260
+ value() {
261
+ return this.state.value;
262
+ }
263
+ scale(n) {
264
+ const { from, to } = this.state;
265
+ const updates = to.min + (clamp(n, from.min, from.max) - from.min) * (to.max - to.min) / (from.max - from.min);
266
+ this.state.value = updates;
267
+ return updates;
268
+ }
269
+ };
270
+
271
+ // src/math/Env.ts
272
+ var parseOptions4 = (opts) => {
273
+ return {
274
+ from: 0,
275
+ to: 1,
276
+ ...opts
277
+ };
278
+ };
279
+ var getInitialState = ({ from, to, duration }) => {
280
+ return {
281
+ duration,
282
+ from,
283
+ prev: now(),
284
+ to,
285
+ totalElapsed: 0,
286
+ value: from
287
+ };
288
+ };
289
+ var updateStateFromOptions2 = (opts, prevState) => {
290
+ const { from, to, duration } = {
291
+ ...prevState,
292
+ ...opts
293
+ };
294
+ return {
295
+ ...prevState,
296
+ duration,
297
+ from,
298
+ to,
299
+ totalElapsed: 0
300
+ };
301
+ };
302
+ var Env = class {
303
+ constructor(opts) {
304
+ __publicField(this, "state");
305
+ __publicField(this, "_interpolator");
306
+ const { from, to, duration } = parseOptions4(opts);
307
+ this.state = getInitialState({ from, to, duration });
308
+ this._interpolator = new Scale({
309
+ from: {
310
+ min: 0,
311
+ max: duration
312
+ },
313
+ to: {
314
+ min: from,
315
+ max: to
316
+ }
317
+ });
318
+ }
319
+ setDuration(duration) {
320
+ const { to, totalElapsed } = this.state;
321
+ this.state = {
322
+ ...this.state,
323
+ ...duration <= totalElapsed ? {
324
+ duration,
325
+ value: to
326
+ } : { duration }
327
+ };
328
+ }
329
+ reset(opts) {
330
+ const updates = updateStateFromOptions2(opts, this.state);
331
+ this.state = {
332
+ ...updates,
333
+ prev: now(),
334
+ value: updates.from
335
+ };
336
+ this._interpolator.setRanges({
337
+ from: {
338
+ min: 0,
339
+ max: updates.duration
340
+ },
341
+ to: {
342
+ min: updates.from,
343
+ max: updates.to
344
+ }
345
+ });
346
+ }
347
+ done() {
348
+ return this.state.duration <= this.state.totalElapsed;
349
+ }
350
+ value() {
351
+ const { to, value } = this.state;
352
+ if (this.done()) {
353
+ return to;
354
+ }
355
+ return value;
356
+ }
357
+ next() {
358
+ if (this.done()) {
359
+ return this.value();
360
+ }
361
+ const { prev, totalElapsed: prevTotalElapsed } = this.state;
362
+ const curr = now();
363
+ const tickInterval = curr - prev;
364
+ const totalElapsed = prevTotalElapsed + tickInterval;
365
+ const updates = this._interpolator.scale(totalElapsed);
366
+ this.state.prev = curr;
367
+ this.state.totalElapsed = totalElapsed;
368
+ this.state.value = updates;
369
+ return updates;
370
+ }
371
+ };
372
+
373
+ // src/math/Sine.ts
374
+ var SINE_PERIOD = Math.PI * 2 - 1e-4;
375
+ var getInitialState2 = (duration) => ({
376
+ cycle: 0,
377
+ duration,
378
+ prev: now(),
379
+ totalElapsed: 0,
380
+ value: 0
381
+ });
382
+ var Sine = class {
383
+ constructor(opts) {
384
+ __publicField(this, "state");
385
+ __publicField(this, "_interpolator");
386
+ const { duration } = opts;
387
+ this._interpolator = new Scale({
388
+ from: {
389
+ min: 0,
390
+ max: duration
391
+ },
392
+ to: {
393
+ min: 0,
394
+ max: SINE_PERIOD
395
+ }
396
+ });
397
+ this.state = getInitialState2(duration);
398
+ }
399
+ setDuration(duration) {
400
+ this.state = {
401
+ ...this.state,
402
+ duration
403
+ };
404
+ }
405
+ reset(opts) {
406
+ const { duration } = {
407
+ ...this.state,
408
+ ...opts
409
+ };
410
+ this.state = getInitialState2(duration);
411
+ }
412
+ value() {
413
+ return this.state.value;
414
+ }
415
+ next() {
416
+ const {
417
+ cycle,
418
+ duration,
419
+ prev,
420
+ totalElapsed: prevTotalElapsed
421
+ } = this.state;
422
+ const curr = now();
423
+ const tickInterval = curr - prev;
424
+ const totalElapsed = prevTotalElapsed + tickInterval;
425
+ const updates = clamp(
426
+ Math.sin(this._interpolator.scale(totalElapsed)),
427
+ -1,
428
+ 1
429
+ );
430
+ if (cycle >= duration) {
431
+ this.state.cycle = 0;
432
+ } else {
433
+ this.state.cycle = cycle + tickInterval;
434
+ }
435
+ this.state.prev = curr;
436
+ this.state.totalElapsed = totalElapsed;
437
+ this.state.value = updates;
438
+ return updates;
439
+ }
440
+ };
441
+
442
+ // src/constants.ts
443
+ var SIXTY_FPS = 1e3 / 60;
444
+ var MS_IN_SECOND = 1e3;
445
+ var MS_IN_MINUTE = 60 * MS_IN_SECOND;
446
+ var MS_IN_HOUR = MS_IN_MINUTE * 60;
447
+
448
+ // src/utils/ms.ts
449
+ var TimeFormat = /* @__PURE__ */ ((TimeFormat2) => {
450
+ TimeFormat2["FPS"] = "fps";
451
+ TimeFormat2["HOURS"] = "h";
452
+ TimeFormat2["HZ"] = "hz";
453
+ TimeFormat2["MILLISECONDS"] = "ms";
454
+ TimeFormat2["MINUTES"] = "m";
455
+ TimeFormat2["SECONDS"] = "s";
456
+ return TimeFormat2;
457
+ })(TimeFormat || {});
458
+ var FORMAT_IDENTIFIERS = [
459
+ "fps" /* FPS */,
460
+ "h" /* HOURS */,
461
+ "hz" /* HZ */,
462
+ "ms" /* MILLISECONDS */,
463
+ "m" /* MINUTES */,
464
+ "s" /* SECONDS */
465
+ ].sort((a, b) => b.length - a.length);
466
+ var FORMATTERS = /* @__PURE__ */ new Map([
467
+ ["fps" /* FPS */, (val) => MS_IN_SECOND / val],
468
+ ["h" /* HOURS */, (val) => val * MS_IN_HOUR],
469
+ ["hz" /* HZ */, (val) => 1 / val * MS_IN_SECOND],
470
+ ["ms" /* MILLISECONDS */, (val) => val],
471
+ ["m" /* MINUTES */, (val) => val * MS_IN_MINUTE],
472
+ ["s" /* SECONDS */, (val) => val * MS_IN_SECOND]
473
+ ]);
474
+ var sanitizeStringVal = (val) => val.toLocaleLowerCase().trim();
475
+ var parseStringValAndFormat = (val) => {
476
+ for (let i = 0; i < FORMAT_IDENTIFIERS.length; i += 1) {
477
+ const format = FORMAT_IDENTIFIERS[i];
478
+ if (val.includes(format)) {
479
+ const value = Number(val.replace(" ", "").replace(format, ""));
480
+ return {
481
+ format,
482
+ value
483
+ };
484
+ }
485
+ }
486
+ return {
487
+ format: void 0,
488
+ value: void 0
489
+ };
490
+ };
491
+ function ms(val, format = "ms" /* MILLISECONDS */) {
492
+ let parsedValue;
493
+ let parsedFormat = format;
494
+ if (val === null || typeof val === "undefined") {
495
+ return void 0;
496
+ }
497
+ if (typeof val === "string") {
498
+ const parsed = parseStringValAndFormat(sanitizeStringVal(val));
499
+ if (typeof parsed.value !== "undefined") {
500
+ parsedValue = parsed.value;
501
+ }
502
+ if (parsed.format) {
503
+ parsedFormat = parsed.format;
504
+ }
505
+ } else {
506
+ parsedValue = val;
507
+ }
508
+ if (Number.isNaN(val)) {
509
+ return void 0;
510
+ }
511
+ const formatter = FORMATTERS.get(parsedFormat);
512
+ if (!formatter) {
513
+ return void 0;
514
+ }
515
+ return formatter(parsedValue);
516
+ }
517
+
518
+ // src/timers/Metro.ts
519
+ var getInitialState3 = (initialTime) => {
520
+ return {
521
+ initialTime,
522
+ isRunning: false,
523
+ iterations: -1,
524
+ prev: 0,
525
+ tickInterval: 0,
526
+ time: initialTime,
527
+ totalElapsed: 0
528
+ };
529
+ };
530
+ var processTimerState = (state) => {
531
+ const { time, prev, totalElapsed, iterations } = state;
532
+ const curr = now();
533
+ if (iterations === -1) {
534
+ return {
535
+ ...state,
536
+ prev: curr,
537
+ iterations: 0
538
+ };
539
+ }
540
+ const tickInterval = curr - prev;
541
+ if (tickInterval <= time) {
542
+ return null;
543
+ }
544
+ return {
545
+ ...state,
546
+ iterations: iterations + 1,
547
+ prev: curr,
548
+ tickInterval,
549
+ totalElapsed: totalElapsed + tickInterval
550
+ };
551
+ };
552
+ var parseOptions5 = (opts) => {
553
+ return {
554
+ time: SIXTY_FPS,
555
+ ...opts
556
+ };
557
+ };
558
+ var Metro = class {
559
+ constructor(callback, opts) {
560
+ __publicField(this, "state");
561
+ __publicField(this, "_listeners");
562
+ __publicField(this, "_timerId");
563
+ __publicField(this, "stop", () => {
564
+ const { totalElapsed } = this.state;
565
+ this.reset();
566
+ this.clearAsyncHandler();
567
+ return totalElapsed;
568
+ });
569
+ __publicField(this, "reset", () => {
570
+ const { initialTime } = this.state;
571
+ this.state = getInitialState3(initialTime);
572
+ });
573
+ __publicField(this, "setTime", (updatedTime = this.state.time) => {
574
+ const time = Math.max(updatedTime, 0);
575
+ this.state = {
576
+ ...this.state,
577
+ time,
578
+ initialTime: time
579
+ };
580
+ });
581
+ __publicField(this, "run", () => {
582
+ if (this.state.isRunning) {
583
+ this.stop();
584
+ }
585
+ this.state = {
586
+ ...this.state,
587
+ isRunning: true,
588
+ prev: now()
589
+ };
590
+ const tick = () => {
591
+ const updates = processTimerState(this.state);
592
+ if (updates) {
593
+ this.state = updates;
594
+ this._listeners.forEach((listener) => {
595
+ listener(this);
596
+ });
597
+ }
598
+ if (this.state.isRunning) {
599
+ this.asyncHandler(tick);
600
+ }
601
+ };
602
+ tick();
603
+ });
604
+ const { time } = parseOptions5(opts);
605
+ this.state = getInitialState3(time);
606
+ this._listeners = [callback];
607
+ }
608
+ asyncHandler(callback) {
609
+ this._timerId = setTimeout(callback, SIXTY_FPS);
610
+ }
611
+ clearAsyncHandler() {
612
+ clearTimeout(this._timerId);
613
+ }
614
+ };
615
+
616
+ // src/timers/Frames.ts
617
+ var DEFAULT_FPS = 60;
618
+ var parseOptions6 = (opts) => {
619
+ const { fps } = {
620
+ fps: DEFAULT_FPS,
621
+ ...opts
622
+ };
623
+ return {
624
+ time: ms(fps, "fps" /* FPS */)
625
+ };
626
+ };
627
+ var Frames = class extends Metro {
628
+ constructor(callback, opts) {
629
+ super(callback, parseOptions6(opts));
630
+ __publicField(this, "setFPS", (fps = DEFAULT_FPS) => {
631
+ this.setTime(ms(fps, "fps" /* FPS */));
632
+ });
633
+ }
634
+ asyncHandler(callback) {
635
+ if (typeof window === "undefined") {
636
+ super.asyncHandler(callback);
637
+ } else {
638
+ this._timerId = requestAnimationFrame(callback);
639
+ }
640
+ }
641
+ clearAsyncHandler() {
642
+ if (typeof window === "undefined") {
643
+ super.clearAsyncHandler();
644
+ } else {
645
+ cancelAnimationFrame(this._timerId);
646
+ }
647
+ }
648
+ };
649
+
650
+ // src/utils/expo.ts
651
+ function expo(n) {
652
+ return Math.pow(clamp(n, 0, 1), Math.E);
653
+ }
654
+
655
+ // src/utils/flip.ts
656
+ function flip(n) {
657
+ return n * -1;
658
+ }
659
+ // Annotate the CommonJS export names for ESM import in node:
660
+ 0 && (module.exports = {
661
+ Drunk,
662
+ Env,
663
+ Frames,
664
+ MS_IN_HOUR,
665
+ MS_IN_MINUTE,
666
+ MS_IN_SECOND,
667
+ Metro,
668
+ Rand,
669
+ SINE_PERIOD,
670
+ SIXTY_FPS,
671
+ Scale,
672
+ Sine,
673
+ TimeFormat,
674
+ clamp,
675
+ expo,
676
+ flip,
677
+ ms,
678
+ now
679
+ });
680
+ //# sourceMappingURL=index.cjs.map