pacer-js 1.0.0 → 1.0.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/Pacer.js +2 -3
- package/README.md +36 -23
- package/package.json +1 -1
package/Pacer.js
CHANGED
|
@@ -66,7 +66,6 @@ class Pacer {
|
|
|
66
66
|
|
|
67
67
|
this.values = {}
|
|
68
68
|
this.n = 0
|
|
69
|
-
this._label = ''
|
|
70
69
|
|
|
71
70
|
this.instanceIndex = Pacer.all.length
|
|
72
71
|
this.isEnabled = true
|
|
@@ -138,7 +137,7 @@ class Pacer {
|
|
|
138
137
|
this.keys
|
|
139
138
|
.sort( function( a, b ){
|
|
140
139
|
|
|
141
|
-
return a.timeAbsolute
|
|
140
|
+
return a.timeAbsolute - b.timeAbsolute
|
|
142
141
|
})
|
|
143
142
|
.forEach( function( key, i, keys ){
|
|
144
143
|
|
|
@@ -432,7 +431,7 @@ class Pacer {
|
|
|
432
431
|
if( keyA instanceof Key !== true ||
|
|
433
432
|
keyB instanceof Key !== true ){
|
|
434
433
|
|
|
435
|
-
console.log( 'one of the keyframes was
|
|
434
|
+
// console.log( 'one of the keyframes was unresolved.', keyA, keyB )
|
|
436
435
|
return this
|
|
437
436
|
}
|
|
438
437
|
|
package/README.md
CHANGED
|
@@ -21,9 +21,16 @@ Getting you from A to B since 2025.
|
|
|
21
21
|
|
|
22
22
|
## TL;DR
|
|
23
23
|
|
|
24
|
-
__Pacer__ is a light-weight keyframing toolkit inspired by [Soledad Penadés](https://soledadpenades.com/)’ original [tween.js](https://soledadpenades.com/projects/tween-js/) masterpiece. List your keyframes as time / value pairs, and __Pacer__ will ✨ tween your numbers and 📞 call your callbacks. __It’s minimal__. Only does what it needs to. __It’s reliable__. We use this in our own professional projects. We found the bumps and sanded them down so you won’t have to ✔️
|
|
24
|
+
__Pacer__ is a light-weight keyframing toolkit inspired by [Soledad Penadés](https://soledadpenades.com/)’ original [tween.js](https://soledadpenades.com/projects/tween-js/) masterpiece. List your keyframes as time / value pairs, and __Pacer__ will ✨ tween your numbers and 📞 call your callbacks. __It’s minimal__. Only does what it needs to. __It’s reliable__. We use this in our own professional projects. We found the bumps and sanded them down so you won’t have to ✔️ Either include the `Pacer.js` ES6 module in your codebase, or install the Node package:
|
|
25
|
+
|
|
26
|
+
```shell
|
|
27
|
+
npm install pacer-js
|
|
28
|
+
```
|
|
29
|
+
Now you’re cooking.
|
|
25
30
|
|
|
26
31
|
```javascript
|
|
32
|
+
import Pacer from 'pacer-js'
|
|
33
|
+
|
|
27
34
|
var p = new Pacer()
|
|
28
35
|
|
|
29
36
|
.key( Date.now(), { n: 0 })
|
|
@@ -31,7 +38,8 @@ var p = new Pacer()
|
|
|
31
38
|
|
|
32
39
|
.key( 2000, { n: 1 })
|
|
33
40
|
.onKey(( e )=> console.log( '2 seconds later', e.n ))
|
|
34
|
-
.
|
|
41
|
+
.tween( Pacer.cubic.inOut )
|
|
42
|
+
.onTween(( e )=> console.log( 'Tweened values!', e.n ))
|
|
35
43
|
|
|
36
44
|
.key( 2000, { n: 2 })
|
|
37
45
|
.onKey(( e )=> console.log( '2 more later', e.n ))
|
|
@@ -58,18 +66,19 @@ With all the tweening and keyframing libraries already out there, why build a ne
|
|
|
58
66
|
2. [Function chaining](#function-chaining)
|
|
59
67
|
3. [Relative _and_ absolute timestamps](#relative-and-absolute-timestamps)
|
|
60
68
|
4. [Tweening](#tweening)
|
|
61
|
-
5. [Every key
|
|
69
|
+
5. [Every key, every tween](#every-key-every-tween)
|
|
62
70
|
6. [Access within callbacks](#access-within-callbacks)
|
|
63
71
|
7. [Update all instances at once](#update-all-instances-at-once)
|
|
64
72
|
8. [Updating time](#updating-time)
|
|
65
73
|
9. [Forward _and_ backward](#forward-and-backward)
|
|
66
74
|
10. [Reduce, reuse, recycle](#reduce-reuse-recycle)
|
|
67
75
|
11. [Burn it to the ground](#burn-it-to-the-ground)
|
|
68
|
-
12. [
|
|
69
|
-
13. [
|
|
70
|
-
14. [
|
|
76
|
+
12. [__Pacer’s__ Keyframe Guarantee™](#guaranteed-keyframe-callbacks)
|
|
77
|
+
13. [Tweening outside the box](#outside-the-box)
|
|
78
|
+
14. [A verbose example](#verbose-example)
|
|
71
79
|
|
|
72
80
|
|
|
81
|
+
<br>
|
|
73
82
|
|
|
74
83
|
|
|
75
84
|
### Legible code
|
|
@@ -80,6 +89,7 @@ We did shorten some words, like “keyframe” → `key` and “between” → `
|
|
|
80
89
|
|
|
81
90
|
|
|
82
91
|
|
|
92
|
+
|
|
83
93
|
### Function chaining
|
|
84
94
|
|
|
85
95
|
Expanding on the above, a code block should read like a normal paragraph of text—one idea following another in a logical sequence. With __Pacer__ you declare a keyframe, and [chain](https://en.wikipedia.org/wiki/Method_chaining) another right onto it. Perhaps you add an `onTween` callback _between_ those keyframes. Just about every __Pacer__ method returns its own instance, so you can chain from one method to another, to another—like writing the sentences of a short story.
|
|
@@ -180,7 +190,7 @@ Each easing equation includes its `in`, `out`, and `inOut` variants, eg. `Pacer.
|
|
|
180
190
|
|
|
181
191
|
|
|
182
192
|
|
|
183
|
-
### Every key
|
|
193
|
+
### Every key, every tween
|
|
184
194
|
|
|
185
195
|
If you find you’re running the same callback over and over, perhaps you’d prefer to declare that just once? We’ve got you covered. Use `onEveryKey` to declare a callback that will fire on _every_ keyframe, and `onEveryTween` to do the same for all tweens. [Something borrowed, something blue. Every tween callback for you](https://youtu.be/4YR_Mft7yIM).
|
|
186
196
|
|
|
@@ -233,23 +243,12 @@ new Pacer()
|
|
|
233
243
|
))
|
|
234
244
|
```
|
|
235
245
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
### Update all instances at once
|
|
240
|
-
|
|
241
|
-
But how do you update an _unnamed_ instance? Under the hood, __Pacer__ keeps a reference to all created instances in its `Pacer.all` array. You can update every single instance at once by sticking this in your animation loop:
|
|
242
|
-
|
|
243
|
-
```javascript
|
|
244
|
-
Pacer.update()
|
|
245
|
-
```
|
|
246
|
-
|
|
247
246
|
And because `onKey` and `onTween` provide the same callback arguments, it’s trivial to use the same callback for both.
|
|
248
247
|
|
|
249
248
|
```javascript
|
|
250
249
|
var myCallback = ( e, p )=> console.log(
|
|
251
250
|
|
|
252
|
-
'Step
|
|
251
|
+
'Step #', p.keyIndex + 1,
|
|
253
252
|
'value:', e.n
|
|
254
253
|
)
|
|
255
254
|
new Pacer()
|
|
@@ -263,6 +262,17 @@ new Pacer()
|
|
|
263
262
|
|
|
264
263
|
|
|
265
264
|
|
|
265
|
+
### Update all instances at once
|
|
266
|
+
|
|
267
|
+
But how do you update an _unnamed_ instance? Under the hood, __Pacer__ keeps a reference to all created instances in its `Pacer.all` array. You can update every single instance at once by sticking this in your animation loop:
|
|
268
|
+
|
|
269
|
+
```javascript
|
|
270
|
+
Pacer.update()
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
|
|
266
276
|
### Updating time
|
|
267
277
|
|
|
268
278
|
You’ve seen that you can update your instance with `p.update()`, or all instances at once with `Pacer.update()`. But now you’re interested in finer control of your timing. When either the class or instance `update` method is called without arguments, __Pacer__ defaults to `Date.now()`, but you are free to use any numeric progression you choose. Perhaps you want to key off of `window.performance.now()` for finer accuraccy. Or maybe you’re building a scroll-based animation and you’re substituting `scrollY` (pixels) for time. Just pass your value via update:
|
|
@@ -270,9 +280,9 @@ You’ve seen that you can update your instance with `p.update()`, or all instan
|
|
|
270
280
|
```javascript
|
|
271
281
|
p.update( numericValue )
|
|
272
282
|
```
|
|
273
|
-
Be sure you’re consistent with your units. __Pacer__ isn’t going to magically understand that you’ve used seconds to declare keyframes, but milliseconds in your `update` call. That’s on you.
|
|
283
|
+
Be sure you’re consistent with your units. __Pacer__ isn’t going to magically understand that you’ve used seconds to declare keyframes, but milliseconds in your `update` call. That’s on you. And don’t use one instance for timed animations, another for scrolling animations, and then expect the global `Pacer.update()` to cater to both.
|
|
274
284
|
|
|
275
|
-
Another thing to note is that `update` expects an _absolute_ number, rather than a _relative_ one. Repeatedly calling `p.update( 1000 )` will _not_ advance your animation by one second with each call. Instead it will lock your animation at its one second mark. Relative units are enormously useful for crafting (and recrafting) keyframes, but slightly less useful within the context of synchronization. It’s taken years of building projects like this to be able to feel confident in asserting this subtlety.
|
|
285
|
+
Another thing to note is that `update` expects an _absolute_ number, rather than a _relative_ one. (That’s “absolute” as in each number represents a distance from zero, not “absolute value” as in a non-negative number. __Pacer__’s `update` is perfectly happy to accept negative values for time.) Repeatedly calling `p.update( 1000 )` will _not_ advance your animation by one second with each call. Instead it will lock your animation at its absolute one second mark. Relative units are enormously useful for crafting (and recrafting) keyframes, but slightly less useful within the context of synchronization. It’s taken years of building projects like this to be able to feel confident in asserting this subtlety.
|
|
276
286
|
|
|
277
287
|
|
|
278
288
|
|
|
@@ -309,6 +319,7 @@ p.reset( Date.now() + 2000 )
|
|
|
309
319
|
```
|
|
310
320
|
|
|
311
321
|
|
|
322
|
+
<br>
|
|
312
323
|
|
|
313
324
|
|
|
314
325
|
### Burn it to the ground
|
|
@@ -332,9 +343,9 @@ Pacer.removeAll()
|
|
|
332
343
|
|
|
333
344
|
|
|
334
345
|
|
|
335
|
-
### Guaranteed
|
|
346
|
+
### Guaranteed keyframe callbacks
|
|
336
347
|
|
|
337
|
-
We pledge to deliver all of your
|
|
348
|
+
We pledge to deliver all of your `onKey` callbacks with a money-back guarantee. (Reminder: You have paid zero dollars for this toolkit.And donations don’t count.) By default, each keyframe has a `guarantee` Boolean set to `true` that assures `onKey` will be called when calculating the gulf between “now” and our animation loop’s prior execution. Let’s say you have keyframes spaced very close together in time—tighter than your animation loop is able to execute. In this example, our last `update` call determined that we were between Key Frame __A__ and Key Frame __B__:
|
|
338
349
|
|
|
339
350
|
```
|
|
340
351
|
KEY KEY KEY KEY
|
|
@@ -407,7 +418,9 @@ Note that for both of these, `p.keyIndex` will be _out of range_ of `p.keys` (`-
|
|
|
407
418
|
|
|
408
419
|
```
|
|
409
420
|
if keyIndex === -1 → onBeforeAll()
|
|
421
|
+
|
|
410
422
|
if keyIndex 0..keys.length-1 → onEveryTween()
|
|
423
|
+
|
|
411
424
|
if keyIndex === keys.length → onAfterAll()
|
|
412
425
|
```
|
|
413
426
|
|