-
Notifications
You must be signed in to change notification settings - Fork 1
3. Helper functions
All helpers are contained in helpers folder. Suddenly
Group helper accepts array of tweens and calls its methods on ALL passed tweens.
import NanoTween from 'nanotween'
import { group } from 'nanotween/helpers'
import { linear } from 'nanotween/easings'
const tweenA = new NanoTween()
.duration(1000)
.on('complete', () => console.log('A completed'))
const tweenB = new NanoTween()
.duration(2000)
.on('complete', () => console.log('B completed'))
const tweenC = new NanoTween()
.duration(3000)
.on('complete', () => console.log('C completed'))
// Group now have ALL of tween methods and control ALL tweens
const group = new Group([tweenA, tweenB, tweenC])
// Add linear easing to all tweens
group.easing(linear)
// Start all tweens
group.start()
// => A completed (1 sec)
// => B completed (2 sec)
// => C completed (3 sec)You can also set equal duration or add one handler to all tweens
group.duration(1000).on('complete', () => console.log('Tween completed'))
// => A completed
// => Tween completed
// => B completed
// => Tween completed
// => C completed
// => Tween completedNOTE: If you want to call handlers only once for all tweens, you can wrap it with
_.debounceor_.throttlemethods.
P.S. Also I'm working on nanoutils library that has these methods already Group also has.tweensproperty that contains array of passed tweens:
console.log(group.tweens) // => [tweenA, tweenB, tweenC]Chain helper also accepts array of tweens. But it calls tweens in sequence
import NanoTween from 'nanotween'
import { chain } from 'nanotween/helpers'
const el = document.getElementById('el')
const moveLeft = new NanoTween()
.duration(1000)
.on('update', progress => el.style.left = `${progress * 50}px`)
const moveBottom = new NanoTween()
.duration(1000)
.on('update', progress => el.style.bottom = `${progress * 50}px`)
const chain = new Chain([moveLeft, moveBottom])
// Start the first tween
// If some of tweens are working, it will be stopped
chain.start()
// Stop current tween and return to start
chain.stop()
// Play or pause current chain
chain.play()
chain.pause()
// Add events handler. IMPORTANT: Events are different from NanoTween instances
chain.on('start', () => console.log('Chain is started'))
// Also it contains index of current tween and array of tweens
console.log(chain.current) // => 0
console.log(chain.tweens) // => [moveLeft, moveBottom]Whenever first tween is started
none
Whenever chain.stop() is called
none
Whenever chain.play() is called
-
index- index of current tween
Whenever chain.pause() is called
-
index- index of current tween
Whenever last tween is completed
none
Whenever current tween is completed
-
index- index of current tween
Whenever current tween is updated
-
metaobject with meta info-
value- current value -
remaining- repeats count remaining -
completed- how many iterations are completed
-
-
index- index of current tween
Yo-yo helper calls .reverse() on each step (like 0 -> 0.33 -> 0.66 -> 1 -> 0.66 -> 0.33 -> 0 -> ...)
import NanoTween from 'nanotween'
import { yoyo } from 'nanotween/helpers'
const tween = new NanoTween()
.use(yoyo)
.duration(1000)
.repeat(Infinity)
.start()
// It also emits `yoyo` event on reverse with `reversed` flag
tween.on('yoyo', isReversed => {
console.log(isReversed ? "I'm going back" : "I'm going forward")
})
// => I'm going back
// => I'm going forward
// => I'm going back
// ...From -> to is shortcut for often used tween from number to number or from object to object without callbacks.
import NanoTween from 'nanotween'
import { fromTo } from 'nanotween/helpers'
const el = document.getElementById('progress')
const tween = new NanoTween()
.use(fromTo(0, 100))
.duration(1000)
.on('update', progress => { el.value = progress })In that example progress will take values from 0 to 100 rather than from 0 to 1
Also it understands objects. Are you remembered example with left/bottom transition?
const el = document.getElementById('block')
const tween = new NanoTween()
.use(fromTo({ left: 0, bottom: 0 }, { left: 100, bottom: 200 }))
.on('update', style => {
el.style.left = `${style.left}px`
el.style.bottom = `${style.bottom}px`
})If you want more customization, fromTo accepts callback in third argument that will be applied to values (and nested object values).
const step = word => (from, to, k) =>
word.slice(0, Math.round(k))
const printWord = word =>
fromTo(0, word.length, step(word))
const tween = new NanoTween()
.use(printWord('hello'))
.duration(1000)
.start()
// =>
// => h
// => he
// => hel
// => hell
// => hello