Dynamicx is the ESM version of dynamics.js (a JavaScript library to create physics-based animations) for better coding experience. Here are some examples.
Download:
- GitHub releases
- npm:
npm install dynamicx
import dynamicx into your module:
import dynamicx from 'dynamicx'You can animate CSS properties of DOM element. Here in dynamicx, length properties, transform functions are most used. For length property values, we can simply use number, the default unit is px.
var el = document.getElementById('logo')
dynamicx.animate(el, {
translateX: 350,
scale: 2,
opacity: 0.5
}, {
type: dynamicx.spring,
frequency: 200,
friction: 200,
duration: 1500
})You also can animate SVG attribute.
var path = document.querySelector('path')
dynamicx.animate(path, {
d: 'M0,0 L0,100 L100,50 L0,0 Z',
fill: 'red',
rotateZ: 45,
// rotateCX and rotateCY are the center of the rotation
rotateCX: 100,
rotateCY: 100
}, {
friction: 800
})And any JavaScript object.
var o = {
number: 10,
color: '#FFFFFF',
string: '10deg',
array: [ 1, 10 ]
}
dynamicx.animate(o, {
number: 20,
color: '#000000',
string: '90deg',
array: [-9, 99 ]
})Animates an element to the properties with the animation options.
elis a DOM element, a JavaScript object or an Array of elementspropertiesis an object of the properties/values you want to animateoptionsis an object representing the animationtypeis the animation type:dynamicx.spring,dynamicx.easeInOut,... (default:dynamicx.easeInOut)frequency,friction,bounciness,... are specific to the animation type you are usingdurationis in milliseconds (default:1000)delayis in milliseconds (default:0)complete(optional) is the completion callbackchange(optional) is called at every change. Two arguments are passed to the function.function(el, progress)elis the element it's animatingprogressis the progress of the animation between 0 and 1
Stops the animation applied on the element
This is applying the CSS properties to your element with the correct browser prefixes.
elis a DOM elementpropertiesis an object of the CSS properties
Dynamicx has its own setTimeout. The reason is that requestAnimationFrame and setTimeout have different behaviors. In most browsers, requestAnimationFrame will not run in a background tab while setTimeout will. This can cause a lot of problems while using setTimeout along your animations. I suggest you use Dynamicx's setTimeout and clearTimeout to handle these scenarios.
fnis the callbackdelayis in milliseconds
Returns a unique id
Clears a timeout that was defined earlier
idis the timeout id
Toggle a debug mode to slow down every animations and timeouts.
This is useful for development mode to tweak your animation.
This can be activated using Shift + D in the browser.
frequencydefault is 300frictiondefault is 200anticipationSize(optional)anticipationStrength(optional)
frequencydefault is 300frictiondefault is 200
bouncinessdefault is 400elasticitydefault is 200
frictiondefault is 500
No properties
pointsarray of points and control points
The easiest way to output this kind of array is to use the curve creator. Here is an example:
[{x:0, y:0, cp:[{x:0.2, y:0}]},
{x:0.5, y:-0.4, cp:[{x:0.4, y:-0.4},{x:0.8, y:-0.4}]},
{x:1, y:1, cp:[{x:0.8,y:1}]}]Compile: npm run build
Run tests: npm test