phena
v2.1.1
Published
Petit animation engine
Downloads
9
Maintainers
Readme
phena
🔮 Petit tweening engine based on requestAnimationFrame
The name of the library comes from the phenakistiscope discs, one of the first motion artifacts used for entertainment.
Install
Add it to your application using a package manager.
# npm
npm i phena --save
# yarn
yarn add phena
You can also drop it in the browser using a script with https://unpkg.com/phena
as source.
Tween
class
To start tweening a value import the Tween
class first and pass the options.
import { Tween } from 'phena'
new Tween({ from: 1, to: 10, duration: 2000 })
This will iterate values from 1
to 10
in 2 seconds.
To react to this iteration define an onUpdate
method.
import { Tween } from 'phena'
function onUpdate(value) { console.log(value) }
new Tween({ from: 1, to: 10, duration: 2000, onUpdate })
All actions are queued using requestAnimationFrame
which makes this tweening engine perfect for actions that will trigger paint jobs or layout calculations in the browser.
start
If you passed paused: true
to options, start
will allow you to kick off the tweening.
import { Tween } from 'phena'
const tween = new Tween({ from: 1, to: 10, duration: 2000, paused: false })
tween.start()
cancel
Stop the tweening at any time.
import { Tween } from 'phena'
const tween = new Tween({ from: 1, to: 10, duration: 2000 })
tween.cancel()
The library doesn't support pause and resume actions yet.
Available options
List of properties you can pass to options
object:
from
required, initial numeric valueto
required, final numberic valueduration
required, tweening time in millisecondsdelay
, delay time in millisecondsonUpdate
, function to execute on each value updateonComplete
, function to execute when tweening has finishedpaused
, don't start tweening on instantiation,false
by defaultease
, function to alter the rate of change in the value pass toonUpdate
Contributing
To contribute Node.js and yarn are required.
Before commit make sure to follow conventional commits specification and check all tests pass by running yarn test
.
Disclaimer
phena works similar to basic time based tweening utility, but internally it relies on enqueueing callbacks in the paint thread so it's ideal for scheduling animation jobs.
This package is not an animation library and has no intentions to become one, so it won't expose a richful API like other tools out there. For now, it just provides the minimum set of options to iterate over value updates, focusing on animation of DOM elements.