Web Animations in WebKit
Over the last 8 months we have been working on adding support for Web Animations, a W3C standard offering Web developers a JavaScript API to create, query and controls animations. While there is work left to do to ship this experimental feature to the Web at large, we feel our implementation has matured enough that, with the release of Safari Technology Preview 59, we can turn Web Animations on by default for our Web developer audience.
An Animation API for the Web
Browser engines have supported various animation features for many years, CSS Transitions and CSS Animations being two widely-supported approaches to authoring efficient animations on the Web. While these features have proven popular, they become limited when developers try to integrate browser-implemented animations via JavaScript:
- Creating a CSS Transition dynamically requires forcing or waiting for a style invalidation so start and end values can be specified
- Creating CSS Animations dynamically requires
@keyframe
rules to be generated and inserted in a global stylesheet - Animations created via CSS are not represented via JavaScript and cannot be queried or controlled
For instance, developers would have to resort to code such as this to slide an element 100 pixels to the right:
const element = document.getElementById("my-element");
// Set the start value and transition properties.
element.style.transform = "translateX(0)";
element.style.transitionProperty = "transform";
element.style.transitionDuration = "1s";
// Force a style invalidation.
window.getComputedStyle(element);
// Set the end value.
element.style.transform = "translateX(100px)";
This approach is not elegant as it forces a style invalidation that causes extra work rather than just letting the browser invalidate styles at the most appropriate time. And this is just one single transition, but what if another library in your webpage also needed to create a transition? This would multiply forced style invalidation for no good reason.
The value of Web Animations lies in having a JavaScript API that preserves the ability to let the browser engine do the heavy lifting of running animations efficiently while enabling more advanced control of your animations. Using Web Animations, we can rewrite the code above with a single method call:¬…
element.animate({ transform: ["translateX(0)", "translateX(100px)"] }, 1000);
A single method call and you’re done! But that’s not all, now you can harness the power of Web Animations with a full-featured API to control this animation:
// Obtain a reference to the animation we created above.
const animation = element.getAnimations()[0];
// Seek the animation to 500ms.
animation.currentTime = 500;
// Pause the animation.
animation.pause();
The Web Animations API is very powerful, for instance letting you get a list of all running animations on the document or an individual element, use promises to run code when an animation is ready to start or has completed, reverse an animation, etc.
Integration with CSS
The Web Animations specification goes further than specifying a JavaScript API. It provides a timing and animation model for web browsers to implement features such as CSS Transitions and CSS Animations. In fact, in WebKit, we’ve updated our implementation of these existing CSS technologies so that the same CSS Transitions and Animations you’ve been authoring for years are now represented as Web Animations objects in the browser. Using the getAnimations()
method, you can obtain a reference to a CSSTransition
or CSSAnimation
object which are Animation
subclasses. Then you can seek or pause a CSS transition running on an element just like we did above with an animation created using element.animate()
. As a developer, you can think of CSS Transitions and Animations as a declarative layer above Web Animations.
Help Wanted
We’re very excited to be bringing the power of Web Animations to WebKit and enabling the technology in Safari Technology Preview 59. But there is still a fair bit of work ahead and we need your help to ensure we have a quality implementation before enabling the feature in Safari. We encourage you to try the new API and report issues that you find, bearing in mind that our current implementation is a bit behind the current state of the specification, and you can track all API changes with bug #186518.
It’s also important to check your existing content using CSS Transitions and Animations for possible regressions. One way to see if a regression you might be seeing is caused by the new Web Animations implementation, try toggling “Web Animations and CSS Integration” under the Develop → Experimental Features menu and see if your page’s behavior differs.