Animation library build on top of web animation API (WAAPI)

Overview

unanime.js

Javascript animation library build on top of web animation API (WAAPI).

 

Getting started

Download

$ npm install unanime --save

Import

ES6 modules

import { animate } from 'unanime';

Common js

const animate = require('unanime');

 

Quick start

Example

import { animate } from 'unanime';

const myAnimation = animate(
  '.targets', 
  [
    {transform: 'translateX(100px)'}
  ],
  {
    duration: 1500,
    direction: 'alternate',
    iterations: 3, 
    autoplay: true
  }
)

 

Structure

cont var = animate(
  // Targets,
  [
    // Keyframes
  ],
  { 
    // Options
  }
)

 

Targets

String, Object or Array accepted.

// Targets
'.myTargets' // Under the hood, document.querySelectorAll is used

 

Keyframes

Array with one or multiple objects. When one object is specified, it's like 'TO' direction. Use all CSS(Js) property you want.

// Keyframes
[
  {transform: 'translate3d(0, 0, 0)', backgroundColor: 'red'},
  {transform: 'translate3d(0, 100px, 0)', backgroundColor: 'green'},
]

 

Options

Set your options, they all have a default value specified. You can use an anonymous function on every property.

// Options
{
  duration: () => Math.floor(Math.random() * 10000); // 5683
}
const anotherTarget = document.querySelector('#anotherTarget');

// Inject in var for timeline usage
const myAnimation = animate( 

  // Targets (string, object or array)
  ['.targets', anotherTarget], 

  // Keyframes (array)
  [
    {transform: 'translate3d(0, 0, 0)', backgroundColor: 'red'},
    {transform: 'translate3d(0, 100px, 0)', backgroundColor: 'green'},
  ],

  // Options (object)
  {
    duration: 1000, 
    // animation's duration in ms (number, object) | default: 1000
    direction: 'normal', 
    // 'normal', 'reverse', 'alternate' (string) | default: 'normal'
    easing: 'easeOutBack', 
    // easings.net accepted, basic css or cubic-bezier (string) | default: 'linear'
    delay: 200, 
    // delay before animation start (number, object) | default: 0
    endDelay: 0, 
    // delay after animation finished (number, object) | default: 0
    iterations: 3, 
    // times the animation is executed (positive number or Infinity) | default: 1
    iterationStart: 1.2, 
    // select at which iteration animation start (number) | default: 0
    fill: 'both', 
    // apply the style to your targets 'none', 'auto', 'backwards', 'forwards', 'both' (string) | default: 'both'
    composite: 'replace', 
    // choose how each keyframe impact the style, 'replace', 'add', 'accumulate' | default: 'replace'
    playbackRate: 1, 
    // choose the speed of your animation without changing duration (+ / - number) | default: 1
    autoplay: false, 
    // start animation when ready (boolean) | default: false
    initStyles: true,
    // option that apply css style (first keyframes) to the target | default: false
    commitStyles: false, 
    // option that apply css style to targets after animation finished | default: false
    autocancel: false, 
    // remove animation from browser at the end | default: false
    willChange: false, 
    // add and remove automatically a 'will-change: transform' to your target during animation | default: false
  }
)

myAnimation.play();

 

Running

Animation is not running by default, you have three options:

// Options
{ autoplay: true }

or

// Method
myAnimation.play();

or

// Timeline
const tl = timeline(
  [
    {animate: myAnimation}
  ] 
)
tl.play();

 

Methods

Player

.play()

Start the animation.

.reverse()

Reverse the current playbackRate and play animation.

.pause()

Pause the animation.

.reset()

Go back to first frame and pause animation.

.restart()

Go back to first frame and play animation.

.finish()

Go to the last frame animation.

.cancel()

Stop and remove animation from browser.

.seek(number)

Go to a specific moment of animation (number from 0 to 1).

 

Example:

myAnimation.play();

setTimeout(() => {
  myAnimation.pause();
}, 2500);

 

Getters

.getDuration()

Return the total duration with duration, delays, iterations ...

.getCurrentIteration()

Return the current iteration when executed.

.getPlayState()

Return the animation's status: 'idle', 'running', 'finished'.

.getPlaybackRate()

Return the current playbackRate.

.getProgress() // Soon

Return the current progression (from 0 to 1).

 

Example:

myAnimation.getPlayState();
// 'running'

 

Setters

.setDirection(string)

Set a new direction ('normal', 'reverse', 'alternate').

.setPlaybackRate(number)

Set a new playbackRate ( + / - number). Change the speed of animation.

.setWillChange(boolean)

Set the option willChange to true or false.

.setInitStyles(boolean)

Set the option initStyles to true or false.

.setCommitStyles(boolean)

Set the option commitStyles to true or false.

 

Example:

myAnimation.setDirection('alternate');

 

Events

.onready()

Execute when animation is ready.

.onplay()

Execute when animation is playing.

.onpause()

Execute when animation is paused.

.onfinish()

Execute when animation is finished.

.onremove()

Execute when animation is removed (everytime the animation is paused/play or changed);

.oncancel()

Execute when animation is canceled.

 

Example:

myAnimation.onready(() => {
  console.log('Ready to play !');
});

 

Stagger

Staggering allows you to animate multiple elements with follow through and overlapping action.

Example:

// Options
{delay: {stagger: 100, from: 'center', direction: 'normal'}}

 

Structure

// Options
{ yourOption: {
    default: 0, // initial value of your option added to stagger | default: option's default
    stagger: 100, // amount that will be multiplied by array index (number) | default: 0
    from: 3, // choose the starting point of your stagger propagation (string, number, array) | default: 'start'
    direction: 'reverse' // select the way stagger in computed (string) | default: 'normal'
  }
}

 

from:

There are different ways to set 'from' option in stagger object.

'start', 'center', 'end' // select one mode
or
10 // select array index(+1)
or
[2, 7, 13] // select multiple starting points

 

direction:

Select the propagation's direction.

'normal' // default
or
'reverse'

 

Stagger can be used on every options property who takes a number. Example:

// Options
{
  delay: {stagger: 200},
  duration: {default: 2500, stagger: 100},
  iterations: {default: 4, stagger: 0.05},
  iterationStart: {default: 1, stagger: 0.02},
  playbackRate: {default: 1, stagger: 0.05},
  ...
}

Easing stagger

Easing can accept an array to set different easing on each elements of your targets. You can directly use this list of easing as string.

// Options
{
  easing: ['easeInCirc', null, 'ease-out', 'cubic-bezier(.17,.67,.83,.67)'] 
  // if null, get previous easing
}

 

Timeline

import { animate, timeline } from 'unanime';

const tl = timeline(
  // Animations list
  [
    { animate: myAnimation1},
    { animate: myAnimation2, start: '<+800'},
  ],
  // Timeline options
  {autoplay: false, iterations: Infinity, direction: 'alternate'}
)

tl.play();

Animation start

You can change the start point of each animations within the timeline by adding 'start'.

Relative position

Use STRING to change the relative position of your animation.

{animate: myAnimation1, start: '+200'}, // +200ms from the relative start
{animate: myAnimation2, start: '-600'},
{animate: myAnimation3, start: '<'}, // start with the previous animation
{animate: myAnimation4, start: '<<<'}, // previous * 3 (myAnimation1)
{animate: myAnimation5, start: '<<+800'},
{animate: myAnimation6, start: '<-1200'}

Absolute position

Use NUMBER to change the absolute position of your animation.

{animate: myAnimation2, start: 6500} // start at 6500ms after the timeline started
You might also like...

A pomodoro web app with features like always on top PIP mode!

A pomodoro web app with features like always on top PIP mode!

Tomodoro A pomodoro web app with always on top mode! Features: Clean UI(Inspired from other pomodoro apps like Pomotroid) Themes Works Offline PIP/Alw

Dec 24, 2022

A next-gen web framework made on top of bun

Eviate JS (WIP) Next-generation web framework to build powerful apps Features Simple: No more req or res. It's all ctx (context) and plain objects! Fa

Oct 30, 2022

A minimal routing library designed to sit on top of Bun's fast HTTP server.

siopao A minimal routing library designed to sit on top of Bun's fast HTTP server. Based on Radix Tree. Sio=Hot Pao=Bun Installation bun add siopao Us

Nov 8, 2022

a simple wrapper nestjs dynamic module on top of surrealdb.js driver, with a consumer app to show case library in action, nothing fancy

README README Project Components Dynamic Module Consumer App Install SurrealDb Starts SurrealDb Init surrealDb Database Run App from Source Code Launc

Oct 3, 2022

Easiest way to build documentation for your project. No config or build required, hosted on @netlify.

Easiest way to build documentation for your project. No config or build required, hosted on @netlify.

Hyperdocs is the simplest way you can create documentation for your project. It blends the best of all the other documentation tools in one. Hyperdocs

Dec 22, 2022

In this project I have build a To Do List app that you can list, add, delete and manage your daily tasks better. Build with HTML, CSS, JS, ES6, JSON

ToDo List This project is build by javascript web packages which can add and remove daily tasks. Built With Html Css Javascript Sublime Text Author 👤

Oct 25, 2022

The leaderboard website displays scores submitted by different players. It also allows you to submit your score. All data is preserved thanks to the external Leaderboard API service. Build with Html, CSS, JS, API, and Webpack.

The leaderboard website displays scores submitted by different players. It also allows you to submit your score. All data is preserved thanks to the external Leaderboard API service. Build with Html, CSS, JS, API, and Webpack.

Mar 11, 2022

This project contains a leader board for a game which contains players name and list and store them on API build with HTML, CSS, JS and API

Leaderboard This App is a Game Leaderboard app Which is created by JavaScript and the big picture of this application is using API. Build With 👩‍🔧 .

Dec 15, 2022

This application provides the CDK project and a frontend that allows you to build a serverless chat application based on API Gateway's WebSocket-based API feature.

This application provides the CDK project and a frontend that allows you to build a serverless chat application based on API Gateway's WebSocket-based API feature.

Serverless chat application using ApiGateway Websockets This project lets you provision a ready-to-use fully serverless real-time chat application usi

Jan 3, 2023
Releases(0.0.6)
Owner
Clément Laval
Clément Laval
A lightweight, performant, and simple-to-use wrapper component to stick section headers to the top when scrolling brings them to top

A lightweight, performant, and simple-to-use wrapper component to stick section headers to the top when scrolling brings them to top

Mayank 7 Jun 27, 2022
Lightweight (zero dependencies) library for enabling cross document web messaging on top of the MessageChannel API.

Lightweight (zero dependencies) library for enabling cross document web messaging on top of the MessageChannel API.

LironH 4 Jul 15, 2022
A chat application build on top of Remix and Cloudflare.

Remix + Cloudflare Workers starter with Turborepo ?? Starter to get going with Remix and Cloudflare Workers. This template is based on the starter cre

Jacob Ebey 54 Dec 10, 2022
I made countdown birthday and fireworks animation using HTML Canvas, CSS, JS. The fireworks animation gonna come out once the countdown is finished or in other words, "Birthday Time".

Countdown-Birthday-Fireworks-Animation I made countdown birthday and fireworks animation using HTML Canvas, CSS, JS. The fireworks animation gonna com

Mahardika Bayu S.B 19 Dec 31, 2022
A JavaScript animation plugin for menus. It creates a div that moves when you mouse over an element, giving it an elastic animation.

Lava-Lamp Description: A JavaScript animation plugin for menus. It creates a div that moves when you mouse over an element, giving it an elastic anima

Richard Hung 38 Jun 4, 2022
An animation library, built on the Web Animations API for the smallest filesize and the fastest performance

motion-signals A wrapper over Motion One, An animation library, built on the Web Animations API for the smallest filesize and the fastest performance.

Tanvesh Sarve 48 Dec 27, 2022
A JavaScript library built on top of the Faker.JS library. It generates massive amounts of fake data in the browser and node.js.

Blaver - generate massive amounts of fake data in the browser and node.js Blaver is a JavaScript library built on top of the Faker.JS library. It gene

Priyansh 113 Dec 30, 2022
Clinton Mbonu 20 Jun 30, 2022
The Web 3.0 social layer built on top of Twitter

Niftycase – The Web 3.0 Chrome extension for Twitter Niftycase is a open-source Chrome extension that allows you to view anybody's NFTs directly on Tw

Matt Welter 16 Jul 14, 2022
The leaderboard web app displays the top scoring player on the fictional game of life.

Leaderboard is website that displays the scores submitted by all players. It also allows the current user to submit his/her score. All data is preserved thanks to the external Leaderboard API service.

Soufiane Boursen 13 Nov 23, 2022