CSS Timing Functions Level 1

W3C First Public Working Draft,

This version:
https://www.w3.org/TR/2017/WD-css-timing-1-20170221/
Latest published version:
https://www.w3.org/TR/css-timing-1/
Editor's Draft:
https://drafts.csswg.org/css-timing/
Editors:
(Mozilla)
(Apple Inc)
Matt Rakow (Microsoft)
(Google Inc)
Issue Tracking:
GitHub

Abstract

This CSS module describes a way for authors to define a transformation to be applied to the time of an animation. This can be used to produce animations that mimic physical phenomena such as momentum or to cause the animation to move in discrete steps producing robot-like movement.

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, in speech, etc.

Status of this document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at https://www.w3.org/TR/.

This document is a First Public Working Draft.

Publication as a First Public Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

GitHub Issues are preferred for discussion of this specification. When filing an issue, please put the text “css-timing” in the title, preferably like this: “[css-timing] …summary of comment…”. All issues and comments are archived, and there is also a historical archive.

This document was produced by the CSS Working Group (part of the Style Activity).

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

This document is governed by the 1 September 2015 W3C Process Document.

1. Introduction

This section is not normative.

It is often desirable to control the rate at which an animation progresses. For example, gradually increasing the speed at which an element moves can give the element a sense of weight as it appears to gather momentum. This can be used to produce user intuitive interface elements or convincing cartoon props that behave like their physical counterparts. Alternatively, it is sometimes desirable for animation to move forwards in distinct steps such as a segmented wheel that rotates such that the segments always appear in the same position.

Timing functions provide a means to transform animation time by taking an input progress value and producing a corresponding transformed output progress value.

Example of a timing function that produces an ease-in effect.
Example of a timing function that produces an ease-in effect. Given an input progress of 0.7, the timing function scales the value to produce an output progress of 0.52. By applying this timing function, the animation will progress more slowly at first but then gradually progress more quickly.

2. Timing functions

A timing function takes an input progress value and produces an output progress value.

A timing function must be a pure function meaning that for a given set of inputs, it always produces the same output progress value.

The input progress value is a real number in the range [-∞, ∞]. Typically, the input progress value is in the range [0, 1] but this may not be the case when timing functions are chained together.

The output progress value is a real number in the range [-∞, ∞].

Some types of timing function also take an additional boolean before flag input which is defined subsequently.

This specification defines four types of timing functions whose definitions follow.

2.1. The linear timing function

The linear timing function is an identity function meaning that its output progress value is equal to the input progress value for all inputs.

The syntax for the linear timing function is simply the linear keyword.

2.2. Cubic Bézier timing functions

A cubic Bézier timing function is a type of timing function defined by four real numbers that specify the two control points, P1 and P2, of a cubic Bézier curve whose end points P0 and P3 are fixed at (0, 0) and (1, 1) respectively. The x coordinates of P1 and P2 are restricted to the range [0, 1].

The mapping from input progress to output progress is performed by determining the corresponding y value (output progress value) for a given x value (input progress value). The evaluation of this curve is covered in many sources such as [FUND-COMP-GRAPHICS].

A cubic Bezier curve used as a timing function.
A cubic Bézier curve used as a timing function.
The shape of the curve is determined by the location of the control points P1 and P2.
Input progress values serve as x values of the curve, whilst the y values are the output progress values.

For input progress values outside the range [0, 1], the curve is extended infinitely using tangent of the curve at the closest endpoint as follows:

A cubic Bézier timing function may be specified as a string using the following syntax (using notation from [CSS3VAL]):

<cubic-bezier-timing-function> = ease | ease-in | ease-out | ease-in-out | cubic-bezier(<number>, <number>, <number>, <number>)

The meaning of each value is as follows:

ease

Equivalent to cubic-bezier(0.25, 0.1, 0.25, 1).

ease-in

Equivalent to cubic-bezier(0.42, 0, 1, 1).

ease-out

Equivalent to cubic-bezier(0, 0, 0.58, 1).

ease-in-out

Equivalent to cubic-bezier(0.42, 0, 0.58, 1).

cubic-bezier(<number>, <number>, <number>, <number>)

Specifies a cubic Bézier timing function. The four numbers specify points P1 and P2 of the curve as (x1, y1, x2, y2). Both x values must be in the range [0, 1] or the definition is invalid.

The keyword values listed above are illustrated below.

The timing functions produced by keyword values.
The timing functions produced by each of cubic Bézier timing function keyword values.

2.3. Step timing functions

A step timing function is a type of timing function that divides the input time into a specified number of intervals that are equal in length.

Some example step timing functions are illustrated below.

Example step timing functions.
Example step timing functions. In each case the domain is the input progress whilst the range represents the output progress produced by the step function.
The first row shows the function for each transition point when only one step is specified whilst the second row shows the same for three steps.

A step timing function is defined by a non-zero positive number of steps, and a step position property that may be either start or end.

At the exact point where a step occurs the result of the function is conceptually the top of the step. However, an additional before flag passed as input to the step timing function, if true, will cause the result of the function to correspond to the bottom of the step at the step point.

The output progress value is calculated from the input progress value and before flag as follows:

  1. Calculate the current step as floor(input progress value × steps).

  2. If the step position property is start, increment current step by one.

  3. If both of the following conditions are true:

    decrement current step by one.

  4. If input progress value ≥ 0 and current step < 0, let current step be zero.

  5. If input progress value ≤ 1 and current step > steps, let current step be steps.

    Steps 4 and 5 in this procedure ensure that given an input progress value in the range [0, 1], a step timing function does not produce an output progress value outside that range.

    For example, although mathematically we might expect that a step timing function with a step position of start would step up when the input progress value is 1, intuitively, when we apply such a timing function to a forwards-filling animation, we expect it to produce an output progress value of 1 as the animation fills forwards.

    A similar situation arises for a step timing function with a step position of end when applied to an animation during its delay phase.

  6. The output progress value is current step / steps.

As an example of how the before flag affects the behavior of this function, consider an animation with a step timing function whose step position is start and which has a positive delay and backwards fill.

For example, using CSS animation:

animation: moveRight 5s 1s steps(5, start);

During the delay phase, the input progress value will be zero but if the before flag is set to indicate that the animation has yet to reach its animation interval, the timing function will produce zero as its output progress value, i.e. the bottom of the first step.

At the exact moment when the animation interval begins, the input progress value will still be zero, but the before flag will not be set and hence the result of the timing function will correspond to the top of the first step.

The syntax for specifying a step timing function is as follows:

<step-timing-function> = step-start | step-end | steps(<integer>[, [ start | end ] ]?)

The meaning of each value is as follows:

step-start

Equivalent to steps(1, start);

step-end

Equivalent to steps(1, end);

steps(<integer>[, [ start | end ] ]?)

Specifies a step timing function. The first parameter specifies the number of intervals in the function. It must be a positive integer greater than 0. The second parameter, which is optional, is either the value start or end, and specifies the step position. If the second parameter is omitted, it is given the value end.

2.4. Frames timing functions

A frames timing function is a type of timing function that divides the input time into a specified number of intervals of equal length, each of which is associated with an output progress value of increasing value. The difference between a frames timing function and a step timing function is that a frames timing function returns the output progress values 0 and 1 for an equal portion of the input progress values in the range [0, 1]. This makes it suitable, for example, for using in animation loops where the animation should display the first and last frame of the animation for an equal amount of time as each other frame during each loop.

Some example frames timing functions are illustrated below.

Example frames timing functions.
Example frames timing functions. In each case the domain is the input progress whilst the range represents the output progress produced by the function.

A frames timing function is defined by an integral number of frames greater than one.

As with step timing functions, at the exact point where a step occurs the result of the function is conceptually the top of the step.

The output progress value is calculated from the input progress value and before flag as follows:

  1. Calculate the current frame as floor(input progress value × frames).

  2. Let the initial output progress value be current frame / (frames - 1).

  3. If input progress value ≤ 1 and output progress value > 1, let output progress value be 1.

The syntax for specifying a frames timing function is as follows:

<frames-timing-function> = frames(<integer>)

The parameter to the function specifies the number of frames. It must be a positive integer greater than 1.

3. The <single-timing-function> production

The syntax for specifying a timing function is as follows:

<single-timing-function> = linear | <cubic-bezier-timing-function> | <step-timing-function> | <frames-timing-function>

3.1. Serialization

Timing functions are serialized using the common serialization patterns defined in [CSSOM] with the following additional requirements:

4. Acknowledgements

This specification is based on the CSS Transitions specification edited by L. David Baron, Dean Jackson, David Hyatt, and Chris Marrin. The editors would also like to thank Douglas Stockwell, Steve Block, Tab Atkins, Rachel Nabors, Martin Pitt, and the Animation at Work slack community for their feedback and contributions.

Conformance

Document conventions

Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.

All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]

Examples in this specification are introduced with the words “for example” or are set apart from the normative text with class="example", like this:

This is an example of an informative example.

Informative notes begin with the word “Note” and are set apart from the normative text with class="note", like this:

Note, this is an informative note.

Advisements are normative sections styled to evoke special attention and are set apart from other normative text with <strong class="advisement">, like this: UAs MUST provide an accessible alternative.

Conformance classes

Conformance to this specification is defined for three conformance classes:

style sheet
A CSS style sheet.
renderer
A UA that interprets the semantics of a style sheet and renders documents that use them.
authoring tool
A UA that writes a style sheet.

A style sheet is conformant to this specification if all of its statements that use syntax defined in this module are valid according to the generic CSS grammar and the individual grammars of each feature defined in this module.

A renderer is conformant to this specification if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the features defined by this specification by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.)

An authoring tool is conformant to this specification if it writes style sheets that are syntactically correct according to the generic CSS grammar and the individual grammars of each feature in this module, and meet all other conformance requirements of style sheets as described in this module.

Requirements for Responsible Implementation of CSS

The following sections define several conformance requirements for implementing CSS responsibly, in a way that promotes interoperability in the present and future.

Partial Implementations

So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported property values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.

Implementations of Unstable and Proprietary Features

To avoid clashes with future stable CSS features, the CSSWG recommends following best practices for the implementation of unstable features and proprietary extensions to CSS.

Implementations of CR-level Features

Once a specification reaches the Candidate Recommendation stage, implementers should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec, and should avoid exposing a prefixed variant of that feature.

To establish and maintain the interoperability of CSS across implementations, the CSS Working Group requests that non-experimental CSS renderers submit an implementation report (and, if necessary, the testcases used for that implementation report) to the W3C before releasing an unprefixed implementation of any CSS features. Testcases submitted to W3C are subject to review and correction by the CSS Working Group.

Further information on submitting testcases and implementation reports can be found from on the CSS Working Group’s website at http://www.w3.org/Style/CSS/Test/. Questions should be directed to the public-css-testsuite@w3.org mailing list.

Index

Terms defined by this specification

Terms defined by reference

References

Normative References

[CSS3VAL]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 3. 29 September 2016. CR. URL: https://www.w3.org/TR/css-values-3/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://tools.ietf.org/html/rfc2119

Informative References

[CSSOM]
Simon Pieters; Glenn Adams. CSS Object Model (CSSOM). 17 March 2016. WD. URL: https://www.w3.org/TR/cssom-1/
[FUND-COMP-GRAPHICS]
Peter Shirley; Michael Ashikhmin; Steve Marschner. Fundamentals of Computer Graphics. 2009.