Convert Figma logo to code with AI

web-animations logoweb-animations-js

JavaScript implementation of the Web Animations API

3,769
410
3,769
91

Top Related Projects

19,483

GSAP (GreenSock Animation Platform), a JavaScript animation library for the modern web

49,575

JavaScript animation engine

🍿 A cross-browser library of CSS animations. As easy to use as an easy thing.

9,901

Reveal CSS animation as you scroll down a page

4,720

CSS3 backed JavaScript animation framework

JavaScript/TypeScript animation engine

Quick Overview

Web Animations JS is a JavaScript implementation of the Web Animations API, providing support for the API in browsers that don't natively support it. It allows developers to create and control animations using JavaScript, offering a standardized way to animate web content across different browsers.

Pros

  • Provides cross-browser compatibility for Web Animations API
  • Enables complex animations with fine-grained control
  • Offers a performance boost over CSS animations in some scenarios
  • Allows for dynamic, runtime-generated animations

Cons

  • Adds additional JavaScript overhead to web applications
  • May have slight performance differences compared to native implementations
  • Requires maintenance to keep up with evolving Web Animations API specifications
  • Learning curve for developers unfamiliar with the Web Animations API

Code Examples

Creating a simple animation:

const element = document.querySelector('#myElement');
const keyframes = [
  { transform: 'translateX(0px)' },
  { transform: 'translateX(300px)' }
];
const options = { duration: 1000, fill: 'forwards' };

const animation = element.animate(keyframes, options);

Controlling animation playback:

const animation = document.querySelector('#myElement').animate(
  [
    { opacity: 1 },
    { opacity: 0 }
  ],
  { duration: 2000, iterations: Infinity }
);

animation.pause();
animation.playbackRate = 2;
animation.play();

Using KeyframeEffect for reusable animations:

const keyframeEffect = new KeyframeEffect(
  document.querySelector('#myElement'),
  [
    { transform: 'scale(1)', backgroundColor: 'red' },
    { transform: 'scale(1.5)', backgroundColor: 'blue' }
  ],
  { duration: 1500, fill: 'both' }
);

const animation = new Animation(keyframeEffect, document.timeline);
animation.play();

Getting Started

  1. Include the Web Animations JS polyfill in your HTML:
<script src="https://cdnjs.cloudflare.com/ajax/libs/web-animations/2.3.2/web-animations.min.js"></script>
  1. Use the Web Animations API in your JavaScript:
const element = document.querySelector('#myElement');
element.animate(
  [
    { transform: 'rotate(0deg)' },
    { transform: 'rotate(360deg)' }
  ],
  {
    duration: 2000,
    iterations: Infinity
  }
);

This will create an infinite rotating animation on the selected element.

Competitor Comparisons

19,483

GSAP (GreenSock Animation Platform), a JavaScript animation library for the modern web

Pros of GSAP

  • More extensive feature set, including advanced easing functions and timeline controls
  • Better cross-browser compatibility, especially for older browsers
  • Higher performance, particularly for complex animations

Cons of GSAP

  • Requires a paid license for commercial use
  • Steeper learning curve due to its comprehensive API
  • Larger file size compared to Web Animations JS

Code Comparison

Web Animations JS:

element.animate([
  { transform: 'translateX(0px)' },
  { transform: 'translateX(100px)' }
], {
  duration: 1000,
  easing: 'ease-in-out'
});

GSAP:

gsap.to(element, {
  x: 100,
  duration: 1,
  ease: "power2.inOut"
});

Both libraries aim to simplify web animations, but GSAP offers more advanced features and better performance at the cost of a steeper learning curve and potential licensing fees. Web Animations JS provides a simpler, standards-based approach that may be sufficient for basic animation needs. The choice between them depends on project requirements, budget constraints, and desired animation complexity.

49,575

JavaScript animation engine

Pros of anime

  • Lightweight and fast, with a smaller file size
  • Extensive easing functions and timeline support
  • Easier to use API with chainable methods

Cons of anime

  • Less browser compatibility compared to web-animations-js
  • Lacks some advanced features present in the Web Animations API
  • May require more frequent updates to stay current with web standards

Code Comparison

anime:

anime({
  targets: '.element',
  translateX: 250,
  rotate: '1turn',
  duration: 800,
  easing: 'easeInOutQuad'
});

web-animations-js:

document.querySelector('.element').animate([
  { transform: 'translateX(0) rotate(0)' },
  { transform: 'translateX(250px) rotate(360deg)' }
], {
  duration: 800,
  easing: 'ease-in-out'
});

Both libraries offer powerful animation capabilities, but anime provides a more concise and intuitive API for creating complex animations. web-animations-js, being a polyfill for the Web Animations API, offers better compatibility with native browser implementations and adheres more closely to web standards. The choice between the two depends on project requirements, browser support needs, and developer preferences.

🍿 A cross-browser library of CSS animations. As easy to use as an easy thing.

Pros of animate.css

  • Lightweight and easy to implement with simple CSS classes
  • Extensive library of pre-built animations
  • No JavaScript required for basic usage

Cons of animate.css

  • Limited control over animation timing and sequencing
  • Less flexibility for complex, dynamic animations
  • Potential for larger CSS file size when using many animations

Code Comparison

animate.css:

<div class="animate__animated animate__bounce">
  An animated element
</div>

web-animations-js:

var element = document.querySelector('.my-element');
element.animate([
  { transform: 'translateY(0px)' },
  { transform: 'translateY(-100px)' }
], {
  duration: 1000,
  iterations: Infinity
});

Key Differences

  • animate.css relies on CSS classes for animation, while web-animations-js uses JavaScript
  • web-animations-js offers more programmatic control over animations
  • animate.css is easier to implement for simple animations, but web-animations-js is more powerful for complex scenarios

Use Cases

  • animate.css: Quick, pre-defined animations for simple website interactions
  • web-animations-js: Complex, dynamic animations that require precise timing and control
9,901

Reveal CSS animation as you scroll down a page

Pros of WOW

  • Lightweight and easy to implement
  • Focuses specifically on scroll-based animations
  • No dependencies, can be used as a standalone library

Cons of WOW

  • Limited to scroll-based animations only
  • Less flexible compared to more comprehensive animation libraries
  • Fewer animation options and customization features

Code Comparison

WOW:

new WOW().init();

<div class="wow fadeInUp" data-wow-duration="1s" data-wow-delay="0.5s">
  Animated content
</div>

web-animations-js:

var element = document.querySelector('.target');
element.animate([
  { opacity: 0, transform: 'translateY(50px)' },
  { opacity: 1, transform: 'translateY(0)' }
], {
  duration: 1000,
  easing: 'ease-out',
  fill: 'forwards'
});

WOW is simpler to implement for basic scroll-based animations, while web-animations-js offers more flexibility and control over animation properties. WOW uses HTML attributes for configuration, whereas web-animations-js relies on JavaScript for defining animations. web-animations-js provides a more comprehensive set of animation capabilities, including keyframe animations and fine-grained control over timing and easing functions.

4,720

CSS3 backed JavaScript animation framework

Pros of move.js

  • Lightweight and minimalistic, focusing on simple CSS transitions
  • Easy to use API for basic animations
  • Supports chaining of animations for more complex sequences

Cons of move.js

  • Limited feature set compared to web-animations-js
  • Less support for advanced animation techniques
  • Not actively maintained (last commit in 2016)

Code Comparison

move.js:

move('#box')
  .set('background-color', 'red')
  .duration('2s')
  .end();

web-animations-js:

document.querySelector('#box').animate([
  { backgroundColor: 'blue' },
  { backgroundColor: 'red' }
], {
  duration: 2000
});

Summary

move.js is a lightweight library for simple CSS transitions, offering an easy-to-use API for basic animations. It's suitable for projects requiring minimal animation features. However, it lacks advanced capabilities and is no longer actively maintained.

web-animations-js, on the other hand, is a more comprehensive solution that implements the Web Animations API. It provides a wider range of animation features and better browser support. While it may have a steeper learning curve, it offers more flexibility and power for complex animations.

Choose move.js for simple projects with basic animation needs, and web-animations-js for more advanced animation requirements or long-term maintainability.

JavaScript/TypeScript animation engine

Pros of tween.js

  • Lightweight and focused solely on tweening, making it more performant for simple animations
  • Easier to learn and implement for developers new to animation libraries
  • Provides more granular control over individual properties during animation

Cons of tween.js

  • Limited feature set compared to the more comprehensive Web Animations API
  • Lacks built-in support for keyframe animations and complex timing functions
  • May require additional libraries or custom code for more advanced animation scenarios

Code Comparison

Web Animations JS:

element.animate([
  { transform: 'translateX(0px)' },
  { transform: 'translateX(100px)' }
], {
  duration: 1000,
  easing: 'ease-in-out'
});

tween.js:

new TWEEN.Tween({ x: 0 })
  .to({ x: 100 }, 1000)
  .easing(TWEEN.Easing.Quadratic.InOut)
  .onUpdate(function(object) {
    element.style.transform = 'translateX(' + object.x + 'px)';
  })
  .start();

The Web Animations JS example shows a more declarative approach, while tween.js offers a more programmatic style with explicit control over property updates. Web Animations JS integrates seamlessly with the browser's animation engine, whereas tween.js requires manual updating (usually in a requestAnimationFrame loop) for smooth animations.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

What is Web Animations?

A new JavaScript API for driving animated content on the web. By unifying the animation features of SVG and CSS, Web Animations unlocks features previously only usable declaratively, and exposes powerful, high-performance animation capabilities to developers.

What is in this repository?

A JavaScript implementation of the Web Animations API that provides Web Animation features in browsers that do not support it natively. The polyfill falls back to the native implementation when one is available.

Quick start

Here's a simple example of an animation that fades and scales a <div>.
Try it as a live demo.

<!-- Include the polyfill -->
<script src="web-animations.min.js"></script>

<!-- Set up a target to animate -->
<div class="pulse" style="width: 150px;">Hello world!</div>

<!-- Animate! -->
<script>
    var elem = document.querySelector('.pulse');
    var animation = elem.animate({
        opacity: [0.5, 1],
        transform: ['scale(0.5)', 'scale(1)'],
    }, {
        direction: 'alternate',
        duration: 500,
        iterations: Infinity,
    });
</script>

Documentation

We love feedback!

Keep up-to-date

Breaking polyfill changes will be announced on this low-volume mailing list: web-animations-changes@googlegroups.com.

More info

NPM DownloadsLast 30 Days