explainers

Controlling Animation Frame Rate

Authors:

Participate

Table of Contents

tl;dr

We propose adding a frameRate property on the DocumentTimeline interface to allow timelines to update and their animations to run at a higher or lower frame rate than the default frame rate the browser uses to update the page rendering.

Introduction

Plenty of devices sport displays with refresh rates that are greater than 60Hz; mobile devices exist with 90Hz and 120Hz refresh rates, and desktop screens exist with 144Hz and 240Hz refresh rates. For years now, Apple devices have supported multiple refresh rates under the moniker ProMotion. Using this technology, displays may refresh at up to 120Hz, but can also adjust to various refresh rates along the way, instead of using a constant refresh rate of 60Hz. On Apple’s 120Hz devices, accelerated animations already run at 120Hz thanks to Core Animation’s built-in support for the ProMotion technology, whereas the rest of the Web page only updates at 60Hz. This “rest of the Web page” includes script-driven animations using requestAnimationFrame() as well as all non-accelerated Web Animations (including CSS Transitions and CSS Animations).

WebKit chose to restrict web content updates to 60Hz for two reasons: first, we measured a significant increase in power usage, and second, we found several examples of web pages that had incorrect behavior when requestAnimationFrame() callbacks were fired at a non-60Hz frequency. However, now that devices with high refresh rate displays are increasingly the norm, we believe it’s judicious to propose advancing the Web platform to take advantage of higher refresh rates and allow authors to choose a suitable refresh rate for different kinds of animations, including low-impact animations that could run at lower frame rates and allow lower power usage.

Variable frame rate

Most high refresh-rate displays actually use variable refresh rates; for example, a 120Hz display might be able to update at 120Hz, 60Hz, 30Hz, etc. This frequency adaptation is responsive to content being displayed on screen, for instance it’s optimal to refresh the display at 24Hz playing a 24fps video in fullscreen with no visible UI. But the frame rate variation may be dictated by other factors than just the content. For instance, the system may impose frame rate limits when in low power mode, or when the target frame rate has not been achieved or some period of time. Generally, it is important to account for the power cost of updating at a high frequency, and driving the display at its maximum frequency is not something that should be encouraged unless it helps fulfilling compelling use cases on the web.

Use cases

We think that it’s important that API enhancements in this area allow authors to communicate the need for both higher and lower-frequency animations. There is generally two categories we see where the authors would want to specify an intent to update at a rate different than the default.

Higher Frequency

Lower Frequency

Outside of animations that may benefit from a lower frame rate without jeopardizing their overall impact, authors may want to preserve power usage by generally taking a conservative approach towards opting into higher frame rate animations.

General proposal

We propose that authors are able specify an intent conveying the frame rate at which timelines are updated and their associated animations are run. Using the maximum attainable frame rate, advertised on document timelines, authors can specify frame rates that can realistically be achieved. Additionally, they may choose from a series of pre-defined relative values such as auto, low, high and highest.

Proposed changes to existing technologies

Web Animations

Animations created using the Web Animations API can be associated with a DocumentTimeline instance created with the desired frameRate by providing one of four values from the AnimationFrameRatePreset enum, the default value auto matching the browser’s default frame rate used to update the page rendering. Alternatively, the frameRate property may be set to a numeric value informed by the timeline’s maximumFrameRate property. Setting frame rates that are the result of dividing this value by a whole number is usually the best way to expect the provided frame rate to be met.

typedef unsigned long FramesPerSecond;

enum AnimationFrameRatePreset { "auto", "low", "high", "highest" };

partial dictionary DocumentTimelineOptions {
    attribute (FramesPerSecond or AnimationFrameRatePreset) frameRate = "auto";
};

partial interface DocumentTimeline {
    constructor(DocumentTimelineOptions options);
    readonly attribute FramesPerSecond? maximumFrameRate;
    attribute (FramesPerSecond or AnimationFrameRatePreset) frameRate;
};

Here’s an example where a user creates an animation associated with a timeline running at 24fps:

const timeline = new DocumentTimeline({ frameRate: 24 });
target.animate({ translate: '100px' }, { timeline, duration: 1000 });

Multiple animations can share a single document timeline, allowing the frame rate to be adjusted at once for all its associated animations. It is also possible to set the frame rate on the document’s default timeline:

document.timeline.frameRate = "low";

Note: it may not be desirable to allow page authors to control the default document timeline (document.timeline) such that pages can’t opt into a higher frame rate, forcing the developer to make a more conscious decision.

CSS Animations

Authors can already associate CSS Animations with a timeline using the animation-timeline property. We propose a new at-rule to allow the definition of document-wide timelines:

@document-timeline <dashed-ident> {
    <declaration-list>
}

In order to also allow CSS Transitions to support association with a document timeline updating at a non-default frame rate, we also introduce the new transition-timeline property. Given the monotonic nature of CSS Transitions, this property is restricted to referencing monotic timelines.

Here’s an example where a user creates a CSS Animation associated with a timeline running at 24fps:

@document-timeline --low-impact {
    frame-rate: 24fps;
}

@keyframes slide {
    to { translate: 100px; }
}

.target {
    animation: slide 1s;
    animation-timeline: --low-impact;
}

Authors can also use a new document() function notation within the animation-timeline property:

@keyframes slide {
    to { translate: 100px; }
}

.target {
    animation: slide 1s;
    animation-timeline: document(24fps);
}

Note: there may be some nuances here around the timing of transition and animation events; for example, if the end of an animation iteration for a 30fps animation falls close to a 4ms boundary, when is the animationiteration events fired?

JavaScript Animations

Most JavaScript-based animations on the web today use requestAnimationFrame(). However, we don’t think this is a good extension point for variable-frequency animations.

The Web Animations 2 specification introduces the notion of Custom Effects, a powerful way to run script-based animations while leveraging the Web Animations timing model. With the Web Animations extension described above, we believe this is the most appropriate way for web developers to request JavaScript callbacks for animation while communicating animation intent to the user agent.

WebKit is dedicated to push the concept of Custom Effects forward and has revived standardization discussions about that feature and experimental support behind an off-by-default flag is available.

Note: In the context of OffscreenCanvas and DedicatedWorkerGlobalScope, which lend themselves particularly well to the implementation of high performance canvas-based games or visualization, requestAnimationFrame() is currently the sole way to drive an animation. In the future, this proposal will investigate the possibility to expose the Web Animations API to DedicatedWorkerGlobalScope with a shared time origin with the main frame’s timeline. It is not clear yet if this will prove to be a workable solution.

Privacy considerations

We don’t believe this proposal exposes any new fingerprinting vector as existing Web Platform APIs could already be used to simply identify the device’s maximum refresh rate, potentially helping to help identify circumstances under which the animation frame rate is limited due to engaging low-power mode or or responding to thermal pressure.

Considered alternatives

An earlier internal draft of this proposal discussed only providing intent through the use of keywords, disallowing the use of a numeric value. This was primarily driven by privacy considerations, hoping not to have to expose the maximum device frame rate, but we have since then acknowledged the fact that this information is already readily available to page authors.

Another alternative would have been to propose a solution based on the requestAnimationFrame() API. However, we believe this API is not to be developed further for animation purposes and that any new animation-related capabilities should be added through the Web Animations API, barring any strong arguments against that.

Acknowledgements

Many thanks for valuable feedback and advice from: