vevet 2.0.1-dev.8 → 2.0.6

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.
Files changed (134) hide show
  1. package/build/cdn/index.js +1 -1
  2. package/build/cjs/app/Application.js +12 -8
  3. package/build/cjs/app/events/PageLoad.js +3 -3
  4. package/build/cjs/app/events/Viewport.js +5 -5
  5. package/build/cjs/base/Callbacks.js +3 -3
  6. package/build/cjs/base/Module.js +16 -7
  7. package/build/cjs/base/MutableProp.js +10 -10
  8. package/build/cjs/components/animation-frame/AnimationFrame.js +1 -1
  9. package/build/cjs/components/canvas/Ctx2D.js +7 -3
  10. package/build/cjs/components/canvas/Ctx2DPrerender.js +21 -3
  11. package/build/cjs/components/cursor/CustomCursor.js +12 -12
  12. package/build/cjs/components/dragger/Dragger.js +8 -8
  13. package/build/cjs/components/dragger/DraggerMove.js +2 -2
  14. package/build/cjs/components/loading/Preloader.js +5 -5
  15. package/build/cjs/components/loading/ProgressPreloader.js +17 -5
  16. package/build/cjs/components/page/Page.js +37 -9
  17. package/build/cjs/components/scroll/plugins/SmoothScrollKeyboardPlugin.js +2 -2
  18. package/build/cjs/components/scroll/scrollable/ScrollEventsBase.js +1 -1
  19. package/build/cjs/components/scroll/scrollable/ScrollView.js +133 -73
  20. package/build/cjs/components/scroll/scrollbar/Bar.js +18 -15
  21. package/build/cjs/components/scroll/scrollbar/ScrollBar.js +9 -7
  22. package/build/cjs/components/scroll/smooth-scroll/SmoothScroll.js +26 -22
  23. package/build/cjs/components/text/SplitText.js +47 -12
  24. package/build/cjs/components/timeline/StaticTimeline.js +4 -4
  25. package/build/cjs/components/timeline/Timeline.js +2 -2
  26. package/build/cjs/handlers/wheel/WheelHandler.js +4 -4
  27. package/build/cjs/utils/common/mergeWithoutArrays.js +1 -1
  28. package/build/cjs/utils/common/randID.js +2 -2
  29. package/build/cjs/utils/listeners/onScroll.js +66 -28
  30. package/build/cjs/utils/math/{boundVal.js → clamp.js} +3 -3
  31. package/build/cjs/utils/math/clampScope.js +16 -0
  32. package/build/cjs/utils/math/inScope.js +10 -0
  33. package/build/cjs/utils/math/index.js +13 -7
  34. package/build/cjs/utils/math/scoped.js +18 -0
  35. package/build/cjs/utils/math/{spreadScopeProgress.js → spreadScope.js} +2 -2
  36. package/build/cjs/utils/math/wrap.js +13 -0
  37. package/build/es/app/Application.js +4 -0
  38. package/build/es/base/Module.js +2 -4
  39. package/build/es/components/animation-frame/AnimationFrame.js +2 -2
  40. package/build/es/components/canvas/Ctx2D.js +6 -2
  41. package/build/es/components/canvas/Ctx2DPrerender.js +20 -2
  42. package/build/es/components/loading/ProgressPreloader.js +14 -2
  43. package/build/es/components/page/Page.js +32 -8
  44. package/build/es/components/scroll/scrollable/ScrollView.js +112 -62
  45. package/build/es/components/scroll/scrollbar/Bar.js +9 -6
  46. package/build/es/components/scroll/scrollbar/ScrollBar.js +3 -1
  47. package/build/es/components/scroll/smooth-scroll/SmoothScroll.js +13 -9
  48. package/build/es/components/text/SplitText.js +38 -3
  49. package/build/es/components/timeline/StaticTimeline.js +3 -3
  50. package/build/es/components/timeline/Timeline.js +2 -2
  51. package/build/es/utils/listeners/onScroll.js +65 -28
  52. package/build/es/utils/math/{boundVal.js → clamp.js} +2 -2
  53. package/build/es/utils/math/clampScope.js +8 -0
  54. package/build/es/utils/math/inScope.js +6 -0
  55. package/build/es/utils/math/index.js +7 -4
  56. package/build/es/utils/math/scoped.js +14 -0
  57. package/build/es/utils/math/{spreadScopeProgress.js → spreadScope.js} +1 -1
  58. package/build/es/utils/math/wrap.js +10 -0
  59. package/build/types/app/Application.d.ts +1 -0
  60. package/build/types/app/Application.d.ts.map +1 -1
  61. package/build/types/base/Module.d.ts.map +1 -1
  62. package/build/types/components/canvas/Ctx2D.d.ts +9 -0
  63. package/build/types/components/canvas/Ctx2D.d.ts.map +1 -1
  64. package/build/types/components/canvas/Ctx2DPrerender.d.ts +1 -1
  65. package/build/types/components/canvas/Ctx2DPrerender.d.ts.map +1 -1
  66. package/build/types/components/loading/ProgressPreloader.d.ts +7 -0
  67. package/build/types/components/loading/ProgressPreloader.d.ts.map +1 -1
  68. package/build/types/components/page/Page.d.ts +2 -0
  69. package/build/types/components/page/Page.d.ts.map +1 -1
  70. package/build/types/components/scroll/scrollable/ScrollView.d.ts +40 -14
  71. package/build/types/components/scroll/scrollable/ScrollView.d.ts.map +1 -1
  72. package/build/types/components/scroll/scrollbar/Bar.d.ts.map +1 -1
  73. package/build/types/components/scroll/scrollbar/ScrollBar.d.ts +4 -0
  74. package/build/types/components/scroll/scrollbar/ScrollBar.d.ts.map +1 -1
  75. package/build/types/components/scroll/smooth-scroll/SmoothScroll.d.ts +4 -0
  76. package/build/types/components/scroll/smooth-scroll/SmoothScroll.d.ts.map +1 -1
  77. package/build/types/components/text/SplitText.d.ts +10 -0
  78. package/build/types/components/text/SplitText.d.ts.map +1 -1
  79. package/build/types/utils/listeners/onScroll.d.ts +10 -6
  80. package/build/types/utils/listeners/onScroll.d.ts.map +1 -1
  81. package/build/types/utils/math/clamp.d.ts +5 -0
  82. package/build/types/utils/math/clamp.d.ts.map +1 -0
  83. package/build/types/utils/math/clampScope.d.ts +5 -0
  84. package/build/types/utils/math/clampScope.d.ts.map +1 -0
  85. package/build/types/utils/math/inScope.d.ts +5 -0
  86. package/build/types/utils/math/inScope.d.ts.map +1 -0
  87. package/build/types/utils/math/index.d.ts +7 -4
  88. package/build/types/utils/math/index.d.ts.map +1 -1
  89. package/build/types/utils/math/scoped.d.ts +12 -0
  90. package/build/types/utils/math/scoped.d.ts.map +1 -0
  91. package/build/types/utils/math/spreadScope.d.ts +5 -0
  92. package/build/types/utils/math/spreadScope.d.ts.map +1 -0
  93. package/build/types/utils/math/wrap.d.ts +5 -0
  94. package/build/types/utils/math/wrap.d.ts.map +1 -0
  95. package/package.json +3 -3
  96. package/src/ts/app/Application.ts +6 -0
  97. package/src/ts/base/Module.ts +5 -4
  98. package/src/ts/components/animation-frame/AnimationFrame.ts +2 -2
  99. package/src/ts/components/canvas/Ctx2D.ts +16 -1
  100. package/src/ts/components/canvas/Ctx2DPrerender.ts +23 -6
  101. package/src/ts/components/loading/ProgressPreloader.ts +21 -2
  102. package/src/ts/components/page/Page.ts +34 -8
  103. package/src/ts/components/scroll/scrollable/ScrollView.ts +155 -86
  104. package/src/ts/components/scroll/scrollbar/Bar.ts +9 -6
  105. package/src/ts/components/scroll/scrollbar/ScrollBar.ts +7 -0
  106. package/src/ts/components/scroll/smooth-scroll/SmoothScroll.ts +17 -8
  107. package/src/ts/components/text/SplitText.ts +52 -2
  108. package/src/ts/components/timeline/StaticTimeline.ts +4 -4
  109. package/src/ts/components/timeline/Timeline.ts +2 -2
  110. package/src/ts/utils/listeners/onScroll.ts +101 -38
  111. package/src/ts/utils/math/{boundVal.ts → clamp.ts} +3 -3
  112. package/src/ts/utils/math/clampScope.ts +16 -0
  113. package/src/ts/utils/math/inScope.ts +9 -0
  114. package/src/ts/utils/math/index.ts +12 -6
  115. package/src/ts/utils/math/scoped.ts +17 -0
  116. package/src/ts/utils/math/{spreadScopeProgress.ts → spreadScope.ts} +2 -2
  117. package/src/ts/utils/math/wrap.ts +18 -0
  118. package/build/cjs/components/scroll/custom-scroll/CustomScroll.js +0 -517
  119. package/build/cjs/components/split-text/SplitText.js +0 -233
  120. package/build/cjs/utils/math/scopeProgress.js +0 -25
  121. package/build/es/components/scroll/custom-scroll/CustomScroll.js +0 -486
  122. package/build/es/components/split-text/SplitText.js +0 -199
  123. package/build/es/utils/math/scopeProgress.js +0 -22
  124. package/build/types/components/scroll/custom-scroll/CustomScroll.d.ts +0 -322
  125. package/build/types/components/scroll/custom-scroll/CustomScroll.d.ts.map +0 -1
  126. package/build/types/components/split-text/SplitText.d.ts +0 -118
  127. package/build/types/components/split-text/SplitText.d.ts.map +0 -1
  128. package/build/types/utils/math/boundVal.d.ts +0 -5
  129. package/build/types/utils/math/boundVal.d.ts.map +0 -1
  130. package/build/types/utils/math/scopeProgress.d.ts +0 -20
  131. package/build/types/utils/math/scopeProgress.d.ts.map +0 -1
  132. package/build/types/utils/math/spreadScopeProgress.d.ts +0 -5
  133. package/build/types/utils/math/spreadScopeProgress.d.ts.map +0 -1
  134. package/src/ts/utils/math/scopeProgress.ts +0 -23
@@ -6,7 +6,7 @@ import { Component, NComponent } from '../../../base/Component';
6
6
  import { RequiredModuleProp } from '../../../utils/types/utility';
7
7
  import { AnimationFrame } from '../../animation-frame/AnimationFrame';
8
8
  import { ScrollableElement } from '../types';
9
- import boundVal from '../../../utils/math/boundVal';
9
+ import clamp from '../../../utils/math/clamp';
10
10
  import { NCallbacks } from '../../../base/Callbacks';
11
11
  import { lerp } from '../../../utils/math';
12
12
 
@@ -41,6 +41,10 @@ export namespace NSmoothScroll {
41
41
  * @default false
42
42
  */
43
43
  animationFrame?: false | AnimationFrame;
44
+ /**
45
+ * @default 0
46
+ */
47
+ resizeTimeout?: number;
44
48
  }
45
49
 
46
50
  /**
@@ -215,7 +219,7 @@ export class SmoothScroll <
215
219
  ? -this.prop.overscroll.max : 0;
216
220
  const max = this.maxScrollableWidth
217
221
  + (!!this.prop.overscroll && this.prop.isHorizontal ? this.prop.overscroll.max : 0);
218
- this._targetLeft = boundVal(
222
+ this._targetLeft = clamp(
219
223
  val,
220
224
  [min, max],
221
225
  );
@@ -247,7 +251,7 @@ export class SmoothScroll <
247
251
  ? -this.prop.overscroll.max : 0;
248
252
  const max = this.maxScrollableHeight
249
253
  + (!!this.prop.overscroll && !this.prop.isHorizontal ? this.prop.overscroll.max : 0);
250
- this._targetTop = boundVal(
254
+ this._targetTop = clamp(
251
255
  val,
252
256
  [min, max],
253
257
  );
@@ -401,9 +405,11 @@ export class SmoothScroll <
401
405
  }
402
406
  this._elementsLength = this._elements.length;
403
407
  // add will-change
404
- this._elements.forEach((el) => {
405
- el.style.willChange = 'transform';
406
- });
408
+ if (this.prop.useWillChange) {
409
+ this._elements.forEach((el) => {
410
+ el.style.willChange = 'transform';
411
+ });
412
+ }
407
413
 
408
414
  // initialize the class
409
415
  if (init) {
@@ -423,6 +429,7 @@ export class SmoothScroll <
423
429
  enabled: true,
424
430
  animationFrame: false,
425
431
  recalculateSizes: true,
432
+ resizeTimeout: 0,
426
433
  useWheel: true,
427
434
  autoStop: true,
428
435
  isHorizontal: false,
@@ -453,6 +460,8 @@ export class SmoothScroll <
453
460
  this.resize();
454
461
  this.addViewportCallback('', () => {
455
462
  this.resize(true);
463
+ }, {
464
+ timeout: this.prop.resizeTimeout,
456
465
  });
457
466
 
458
467
  // wheel
@@ -490,8 +499,8 @@ export class SmoothScroll <
490
499
  // get sizes
491
500
  this._clientWidth = outer.clientWidth;
492
501
  this._clientHeight = outer.clientHeight;
493
- this._scrollWidth = boundVal(container.clientWidth, [this.clientWidth, Infinity]);
494
- this._scrollHeight = boundVal(container.clientHeight, [this.clientHeight, Infinity]);
502
+ this._scrollWidth = clamp(container.clientWidth, [this.clientWidth, Infinity]);
503
+ this._scrollHeight = clamp(container.clientHeight, [this.clientHeight, Infinity]);
495
504
 
496
505
  // force instant change
497
506
  // it means that after resizing, scrolling will be instantaneous for a while
@@ -1,4 +1,5 @@
1
1
  import { createElement, selectOne } from 'vevet-dom';
2
+ import { NViewport } from '../../app/events/Viewport';
2
3
  import { Component, NComponent } from '../../base/Component';
3
4
  import { RequiredModuleProp } from '../../utils/types/utility';
4
5
 
@@ -25,6 +26,15 @@ export namespace NSplitText {
25
26
  * @default false
26
27
  */
27
28
  appendLines?: boolean;
29
+ /**
30
+ * Viewport resize event target
31
+ * @default ''
32
+ */
33
+ viewportTarget?: keyof NViewport.CallbacksTypes;
34
+ /**
35
+ * @default 0
36
+ */
37
+ resizeTimeout?: number;
28
38
  }
29
39
 
30
40
  /**
@@ -84,6 +94,8 @@ export class SplitText <
84
94
  container: `#${this.prefix}`,
85
95
  appendLetters: true,
86
96
  appendLines: false,
97
+ viewportTarget: '',
98
+ resizeTimeout: 0,
87
99
  };
88
100
  }
89
101
 
@@ -153,8 +165,12 @@ export class SplitText <
153
165
 
154
166
  // get initial text
155
167
  this._initHTML = this._container.innerHTML;
156
- this._initText = (this._container.innerText || 'no rendered text').trim();
168
+ const innerText = this._container.innerText.trim() || this._container.innerHTML.trim();
169
+ this._initText = innerText || 'no rendered text';
157
170
  this._initText = this._initText.replace(/\s+\n/gm, '\n');
171
+ this._initText = this._initText.replace(/<br>/gm, String.fromCharCode(10));
172
+ this._initText = this._initText.replace(/<br\/>/gm, String.fromCharCode(10));
173
+ this._initText = this._initText.replace(/<br \/>/gm, String.fromCharCode(10));
158
174
 
159
175
  // set default vars
160
176
  this._isPrimarySplit = false;
@@ -174,8 +190,10 @@ export class SplitText <
174
190
  // split the text
175
191
  this.splitText();
176
192
  if (this.prop.appendLines) {
177
- this.addViewportCallback('', () => {
193
+ this.addViewportCallback(this.prop.viewportTarget, () => {
178
194
  this.splitText();
195
+ }, {
196
+ timeout: this.prop.resizeTimeout,
179
197
  });
180
198
  }
181
199
  }
@@ -228,6 +246,7 @@ export class SplitText <
228
246
  // get type of the char
229
247
  const charCode = char.charCodeAt(0);
230
248
  const isWhitespace = charCode === 32 || charCode === 160;
249
+ const isSeparator = [45, 8208, 8211, 8212, 8722].includes(charCode);
231
250
  const isNewLine = charCode === 10;
232
251
 
233
252
  // add elements
@@ -251,6 +270,37 @@ export class SplitText <
251
270
  if (!this.prop.appendLetters) {
252
271
  currentWord.el.innerHTML = currentWord.content;
253
272
  }
273
+
274
+ // go to next word if needed
275
+ if (isSeparator) {
276
+ wordIndex += 1;
277
+ }
278
+ });
279
+
280
+ // only filled words
281
+ this._words = this._words.filter((word, index) => {
282
+ if (word.content.length === 0) {
283
+ if (index > 0) {
284
+ this._words[index - 1].whitespace = word.whitespace;
285
+ this._words[index - 1].br = word.br;
286
+ }
287
+ return false;
288
+ }
289
+ return true;
290
+ });
291
+
292
+ // post-processing
293
+ let prevWord: NSplitText.Word | undefined;
294
+ this._words.forEach((word) => {
295
+ if (prevWord) {
296
+ if (prevWord.whitespace) {
297
+ word.el.classList.add('pre-whitespace');
298
+ }
299
+ }
300
+ if (word.whitespace) {
301
+ word.el.classList.add('has-whitespace');
302
+ }
303
+ prevWord = word;
254
304
  });
255
305
  }
256
306
 
@@ -2,8 +2,8 @@ import easingProgress from 'easing-progress';
2
2
  import { NApplication } from '../../app/Application';
3
3
  import { Component, NComponent } from '../../base/Component';
4
4
  import { RequiredModuleProp } from '../../utils/types/utility';
5
- import scopeProgress from '../../utils/math/scopeProgress';
6
- import boundVal from '../../utils/math/boundVal';
5
+ import scoped from '../../utils/math/scoped';
6
+ import clamp from '../../utils/math/clamp';
7
7
 
8
8
 
9
9
 
@@ -174,8 +174,8 @@ export class StaticTimeline <
174
174
  for (let index = 0, l = length; index < l; index += 1) {
175
175
  const tm = this._nestedTimelines[index];
176
176
  // calculate progress of this very timeline
177
- const tmProgress = boundVal(
178
- scopeProgress(progressForNested, tm.prop.nestedScope),
177
+ const tmProgress = clamp(
178
+ scoped(progressForNested, tm.prop.nestedScope),
179
179
  [0, 1],
180
180
  );
181
181
  tm.progress = tmProgress;
@@ -1,6 +1,6 @@
1
1
  import { StaticTimeline, NStaticTimeline } from './StaticTimeline';
2
2
  import { RequiredModuleProp } from '../../utils/types/utility';
3
- import boundVal from '../../utils/math/boundVal';
3
+ import clamp from '../../utils/math/clamp';
4
4
 
5
5
 
6
6
 
@@ -205,7 +205,7 @@ export class Timeline <
205
205
 
206
206
  // calculate current progress
207
207
  const progressIterator = frameDiff / this.prop.duration / (isReversed ? -1 : 1);
208
- const progressTarget = boundVal(
208
+ const progressTarget = clamp(
209
209
  this.progress + progressIterator,
210
210
  [0, 1],
211
211
  );
@@ -1,64 +1,127 @@
1
- import { addEventListener, IAddEventListener, selectOne } from 'vevet-dom';
2
- import { SmoothScroll } from '../../components/scroll/smooth-scroll/SmoothScroll';
1
+ import { addEventListener, selectOne } from 'vevet-dom';
3
2
  import { IRemovable } from '../types/general';
3
+ import { SmoothScroll } from '../../components/scroll/smooth-scroll/SmoothScroll';
4
+ import { randID } from '../common';
5
+
6
+ type Container = string | Element | SmoothScroll | Window;
7
+
8
+ interface ArgData {
9
+ scrollTop: number;
10
+ scrollLeft: number;
11
+ }
12
+
13
+ interface Instance {
14
+ id: string;
15
+ container: Container;
16
+ callbacks: {
17
+ id: string;
18
+ callback: (
19
+ data: ArgData,
20
+ ) => void;
21
+ }[];
22
+ isPassive: boolean;
23
+ listeners: IRemovable[];
24
+ }
4
25
 
5
26
  interface Props {
6
- passive?: boolean;
27
+ container: Container;
28
+ callback: (data: ArgData) => void;
29
+ isPassive?: boolean;
7
30
  }
8
31
 
32
+ let instances: Instance[] = [];
33
+
9
34
  /**
10
35
  * Add OnScroll event
11
36
  */
12
- export default function onScroll (
13
- selector: string | Element | SmoothScroll | Window,
14
- callback: (arg: {
15
- scrollTop: number,
16
- scrollLeft: number
17
- }) => void,
18
- props?: Props,
19
- ): IRemovable {
20
- const listeners: IAddEventListener[] = [];
37
+ export default function onScroll ({
38
+ container,
39
+ callback,
40
+ isPassive = false,
41
+ }: Props): IRemovable {
42
+ // check if listeners for this element already exist
43
+ let instance = instances.find((data) => (
44
+ data.container === container && data.isPassive === isPassive
45
+ ))!;
46
+ const callbackId = randID('scroll-event');
21
47
 
22
- if (selector instanceof SmoothScroll) {
23
- selector.addCallback('scroll', () => {
24
- callback({
25
- scrollTop: selector.scrollTop,
26
- scrollLeft: selector.scrollLeft,
27
- });
48
+ // if a listener exists, we just add a new callback to its stack
49
+ if (instance) {
50
+ instance.callbacks.push({
51
+ id: callbackId,
52
+ callback,
28
53
  });
29
54
  } else {
30
- let outer: Element | Window;
31
- if (typeof selector === 'string') {
32
- outer = selectOne(selector) as Element;
55
+ // otherwise we create a new instance
56
+ instance = {
57
+ id: randID('scroll-event-instance'),
58
+ container,
59
+ callbacks: [{
60
+ id: callbackId,
61
+ callback,
62
+ }],
63
+ isPassive,
64
+ listeners: [],
65
+ };
66
+ instances.push(instance);
67
+
68
+ // vars
69
+ const { listeners } = instance;
70
+
71
+ // smooth scroll events
72
+ if (container instanceof SmoothScroll) {
73
+ listeners.push(
74
+ container.addCallback('scroll', () => {
75
+ const { scrollTop, scrollLeft } = container;
76
+ for (let index = 0; index < instance.callbacks.length; index += 1) {
77
+ instance.callbacks[index].callback({
78
+ scrollTop,
79
+ scrollLeft,
80
+ });
81
+ }
82
+ }),
83
+ );
33
84
  } else {
34
- outer = selector;
35
- }
36
- if (outer) {
85
+ // dom scroll events
86
+ const isWindow = container instanceof Window;
87
+ const domContainer = selectOne(container) as any;
37
88
  listeners.push(addEventListener(
38
- outer,
89
+ domContainer,
39
90
  'scroll',
40
91
  () => {
41
- const scrollTop = outer instanceof Window
42
- ? outer.pageYOffset : outer.scrollTop;
43
- const scrollLeft = outer instanceof Window
44
- ? outer.pageXOffset : outer.scrollLeft;
45
- callback({
46
- scrollTop,
47
- scrollLeft,
48
- });
92
+ const scrollTop = isWindow
93
+ ? domContainer.pageYOffset : domContainer.scrollTop;
94
+ const scrollLeft = isWindow
95
+ ? domContainer.pageXOffset : domContainer.scrollLeft;
96
+ for (let index = 0; index < instance.callbacks.length; index += 1) {
97
+ instance.callbacks[index].callback({
98
+ scrollTop,
99
+ scrollLeft,
100
+ });
101
+ }
102
+ },
103
+ {
104
+ passive: isPassive,
49
105
  },
50
- props ? {
51
- passive: props.passive,
52
- } : undefined,
53
106
  ));
54
107
  }
55
108
  }
56
109
 
57
110
  return {
58
111
  remove: () => {
59
- listeners.forEach((listener) => {
60
- listener.remove();
112
+ const newCallbacks = instance.callbacks.filter((item) => {
113
+ if (item.id !== callbackId) {
114
+ return true;
115
+ }
116
+ return false;
61
117
  });
118
+ instance.callbacks = newCallbacks;
119
+ if (newCallbacks.length === 0) {
120
+ instance.listeners.forEach((listener) => {
121
+ listener.remove();
122
+ });
123
+ instances = instances.filter((item) => item.id !== instance.id);
124
+ }
62
125
  },
63
126
  };
64
127
  }
@@ -1,9 +1,9 @@
1
1
  /**
2
- * Bound a value between two points
2
+ * Clamp the value between two points
3
3
  */
4
- export default function boundVal (
4
+ export default function clamp (
5
5
  val: number,
6
- scope: [number, number] = [0, 1],
6
+ scope = [0, 1],
7
7
  ) {
8
8
  if (val < scope[0]) {
9
9
  return scope[0];
@@ -0,0 +1,16 @@
1
+ import scoped from './scoped';
2
+ import clamp from './clamp';
3
+
4
+ /**
5
+ * Get progress relatively to the scope and clamp it within two points
6
+ */
7
+ export default function clampScoped (
8
+ val: number,
9
+ scope = [0, 1],
10
+ clampScope = [0, 1],
11
+ ) {
12
+ return clamp(
13
+ scoped(val, scope),
14
+ clampScope,
15
+ );
16
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Check if the value is within the scope
3
+ */
4
+ export default function inScope (
5
+ val: number,
6
+ scopeValue = [0, 1],
7
+ ) {
8
+ return val >= scopeValue[0] && val <= scopeValue[1];
9
+ }
@@ -1,11 +1,17 @@
1
- import boundVal from './boundVal';
1
+ import clamp from './clamp';
2
2
  import lerp from './lerp';
3
- import scopeProgress from './scopeProgress';
4
- import spreadScopeProgress from './spreadScopeProgress';
3
+ import scoped from './scoped';
4
+ import spreadScope from './spreadScope';
5
+ import inScope from './inScope';
6
+ import clampScope from './clampScope';
7
+ import wrap from './wrap';
5
8
 
6
9
  export {
7
- boundVal,
10
+ clamp,
8
11
  lerp,
9
- scopeProgress,
10
- spreadScopeProgress,
12
+ scoped,
13
+ spreadScope,
14
+ inScope,
15
+ clampScope,
16
+ wrap,
11
17
  };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Get progress relatively to the scope.
3
+ *
4
+ * @example
5
+ *
6
+ * scope(.35, [0, 1]);
7
+ * // => .5
8
+ * scope(.35, [.25, 1]);
9
+ * // => .133
10
+ */
11
+ export default function scoped (
12
+ val: number,
13
+ scopeValue = [0, 1],
14
+ ) {
15
+ const result = (val - scopeValue[0]) / (scopeValue[1] - scopeValue[0]);
16
+ return result;
17
+ }
@@ -1,11 +1,11 @@
1
1
  /**
2
2
  * Distribute scope progress among a certain quantity of timelines.
3
3
  */
4
- export default function spreadScopeProgress (
4
+ export default function spreadScope (
5
5
  quantity: number,
6
6
  shift: number,
7
7
  ) {
8
- const timelines: [number, number][] = [];
8
+ const timelines: number[][] = [];
9
9
  // duration for each element
10
10
  const duration = 1 / (quantity - shift * (quantity - 1));
11
11
  // calculate timelines
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Wrap values
3
+ */
4
+ export default function wrap (
5
+ min: number,
6
+ max: number,
7
+ value: number,
8
+ ) {
9
+ const range = max - min;
10
+ return conditionalReturn(value, (val) => ((range + ((val - min) % range)) % range) + min);
11
+ }
12
+
13
+ function conditionalReturn (
14
+ value: number,
15
+ func: (val: number) => number,
16
+ ) {
17
+ return value || value === 0 ? func(value) : func;
18
+ }