<pre class=metadata>
Title: CSS Easing Functions Level 1
Status: ED
Work Status: Refining
Shortname: css-easing
Level: 1
Group: csswg
ED: https://drafts.csswg.org/css-easing/
TR: https://www.w3.org/TR/css-easing-1/
Previous version: https://www.w3.org/TR/2018/WD-css-easing-1-20181009/
Previous version: https://www.w3.org/TR/2018/WD-css-timing-1-20180926/
Previous version: https://www.w3.org/TR/2017/WD-css-timing-1-20170221/
Editor: Brian Birtles, Mozilla https://www.mozilla.org/, bbirtles@mozilla.com, w3cid 43194
Editor: Dean Jackson, Apple Inc https://www.apple.com/, dino@apple.com, w3cid 42080
Editor: Matt Rakow, Microsoft, w3cid 62267
Former Editor: Shane Stephens, Google Inc, shans@google.com, w3cid 47691

Abstract: This CSS module describes a way for authors to define a transformation
    that controls the rate of change of some value.
    Applied to animations, such transformations 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.

!Participate: IRC: <a href="ircs://irc.w3.org:6667/css">#css</a> on W3C's IRC
Repository: w3c/csswg-drafts
!Tests: <a href="https://github.com/web-platform-tests/wpt/tree/master/css/css-easing">web-platform-tests css/css-easing</a>
</pre>

<pre class=biblio>
{
  "FUND-COMP-GRAPHICS": {
    "title": "Fundamentals of Computer Graphics",
    "authors": [
      "Peter Shirley",
      "Michael Ashikhmin",
      "Steve Marschner"
    ],
    "date": "2009",
    "publisher": "A K Peters Limited"
  }
}
</pre>

Introduction {#introduction}
============================

<em>This section is not normative.</em>

It is often desirable to control the rate at which some value changes.
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 intuitive user 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.

Similarly, controlling the rate of change of gradient interpolation can be
used to produce different visual effects such as suggesting a concave or convex
surface, or producing a striped effect.

[=Easing functions=] provide a means to transform such values by taking an
input progress value and producing a corresponding transformed output progress
value.

<figure>
  <img src="easing-function-example.svg" width="400"
    alt="Example of an easing function that produces an ease-in effect.">
  <figcaption>
    Example of an easing function that produces an ease-in effect.<br>
    Given an input progress of 0.7, the easing function scales the
    value to produce an output progress of 0.52.<br>
    Applying this easing function to an animation would cause it to progress
    more slowly at first but then gradually progress more quickly.
  </figcaption>
</figure>

<h2 id=easing-functions oldids=timing-functions>Easing functions</h2>

An <dfn export lt="easing function|timing function">easing function</dfn> takes
an [=input progress value=] and produces an [=output progress value=].

An [=easing function=] must be a pure function meaning that for a given set of
inputs, it always produces the same [=output progress value=].

The <dfn>input progress value</dfn> is a real number in the range [-&infin;,
&infin;].
Typically, the [=input progress value=] is in the range [0, 1] but this may
not be the case when [=easing functions=] are chained together.

The <dfn>output progress value</dfn> is a real number in the
range [-&infin;, &infin;].

Some types of easing functions also take an additional boolean [=before flag=]
input which is defined subsequently.

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

The syntax for specifying an [=easing function=] is as follows:

<div class="prod"><dfn type>&lt;easing-function&gt;</dfn> =
  ''linear'' |
  <<cubic-bezier-easing-function>> |
  <<step-easing-function>></div>


<h3 id=the-linear-easing-function oldids=linear-timing-function-section>The linear easing function: ''linear''</h3>

The <dfn export lt="linear easing function|linear timing function">linear easing
function</dfn> 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 easing function=] is simply the
<dfn dfn-type=value for=easing-function>linear</dfn> keyword.


<h3 id=cubic-bezier-easing-functions oldids=cubic-bezier-timing-functions>Cubic
B&eacute;zier easing functions:
''ease'', ''ease-in'', ''ease-out'', ''ease-in-out'', ''cubic-bezier()''</h3>

A <dfn export>cubic B&eacute;zier easing function</dfn> is
a type of [=easing function=] defined by four real numbers
that specify the two control points,
<var>P1</var> and <var>P2</var>, of a cubic B&eacute;zier curve whose
end points <var ignore>P0</var> and <var ignore>P3</var>
are fixed at (0, 0) and (1, 1) respectively.
The <var>x</var> coordinates of <var>P1</var> and <var>P2</var>
are restricted to the range [0, 1].

<figure>
  <img src="cubic-bezier-easing-curve.svg" width="400"
      alt="A cubic Bezier curve used as an easing function.">
  <figcaption>
    A cubic B&eacute;zier curve used as an easing function.<br>
    The shape of the curve is determined by the location of the control
    points <var>P1</var> and <var>P2</var>.<br>
    Input progress values serve as <var>x</var> values of the curve,
    whilst the <var>y</var> values are the output progress values.
  </figcaption>
</figure>

A <a>cubic B&eacute;zier easing function</a> has the following syntax
(using notation from [[!CSS3VAL]]):

<div class="prod"><dfn type>&lt;cubic-bezier-easing-function&gt;</dfn> =
  ''ease'' | ''ease-in'' | ''ease-out'' | ''ease-in-out'' |
  <span class="atom"><a lt="cubic-bezier()"
  function>cubic-bezier</a>(<<number>>, <<number>>, <<number>>,
  <<number>>)</span></div>

The meaning of each value is as follows:

<dl dfn-type="value" dfn-for="cubic-bezier-easing-function, <cubic-bezier-easing-function>">

:   <dfn>ease</dfn>
::  Equivalent to ''cubic-bezier(0.25, 0.1, 0.25, 1)''.
:   <dfn>ease-in</dfn>
::  Equivalent to ''cubic-bezier(0.42, 0, 1, 1)''.
:   <dfn>ease-out</dfn>
::  Equivalent to ''cubic-bezier(0, 0, 0.58, 1)''.
:   <dfn>ease-in-out</dfn>
::  Equivalent to ''cubic-bezier(0.42, 0, 0.58, 1)''.
:   <dt><dfn function lt="cubic-bezier()">cubic-bezier(<<number>>, <<number>>, <<number>>, <<number>>)</dfn></dt>
::  Specifies a <a>cubic B&eacute;zier easing function</a>.
    The four numbers specify points <var>P1</var> and <var>P2</var> of
    the curve as (<var ignore>x1</var>, <var ignore>y1</var>, <var
    ignore>x2</var>, <var ignore>y2</var>).
    Both <var>x</var> values must be in the range [0, 1] or the definition is
    invalid.

</dl>

The keyword values listed above are illustrated below.

<figure>
  <img src="curve-keywords.svg" width="500"
      alt="The easing functions produced by keyword values.">
  <figcaption>
    The easing functions produced by each of the cubic B&eacute;zier easing
    function keyword values.
  </figcaption>
</figure>

### Output of a cubic b&eacute;zier easing function ### {#cubic-bezier-algo}

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

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:

*   For [=input progress values=] less than zero,

    1.   If the <var>x</var> value of P1 is greater than zero, use
         a straight line that passes through P1 and P0 as the tangent.

    1.   Otherwise, if the <var>x</var> value of P2 is greater than
         zero, use a straight line that passes through P2 and P0 as the tangent.

    1.   Otherwise, let the [=output progress value=] be zero for all
         [=input progress values=] in the range [-&infin;, 0).

*   For [=input progress values=] greater than one,

    1.   If the <var>x</var> value of P2 is less than one, use
         a straight line that passes through P2 and P3 as the tangent.

    1.   Otherwise, if the <var>x</var> value of P1 is less than
         one, use a straight line that passes through P1 and P3 as the tangent.

    1.   Otherwise, let the [=output progress value=] be one for all
         [=input progress values=] in the range (1, &infin;].


<h3 id=step-easing-functions oldids=step-timing-functions>Step easing functions:
''step-start'', ''step-end'', ''steps()''</h3>

A <dfn>step easing function</dfn> is a type of [=easing function=]
that divides the input time into a specified number of intervals that
are equal in length.
It is defined by a number of <dfn>steps</dfn>, and a <dfn>step position</dfn>.
It has following syntax:

<div class="prod">
  <dfn type>&lt;step-easing-function&gt;</dfn> =
  ''step-start'' | ''step-end'' |
  <span class="atom"><a lt="steps()" function>steps</a>(<<integer>>[,
    <<step-position>>]?)</span>

  <dfn type>&lt;step-position&gt;</dfn> =
    ''jump-start'' | ''jump-end'' |
    ''jump-none'' | ''jump-both'' |
    ''start'' | ''end''</div>

The meaning of each value is as follows:

<dl dfn-type=value dfn-for="step-easing-function, <step-easing-function>">

:   <dfn>step-start</dfn>
::  Computes to ''steps(1, start)''
:   <dfn>step-end</dfn>
::  Computes to ''steps(1, end)''

    <figure>
      <img src="step-easing-keyword-examples.svg" width="500"
          alt="Example step easing keywords.">
      <figcaption>
        Example step easing function keyword values.
      </figcaption>
    </figure>

:   <dfn function lt="steps()">steps(&lt;integer&gt;[, &lt;step-position&gt; ]?)</dfn>
::  The first parameter specifies the number of intervals in the function.
    It must be a positive integer greater than 0
    unless the second parameter is <a value for="steps()">jump-none</a>
    in which case it must be a positive integer greater than 1.

    The second parameter, which is optional, specifies the [=step position=]
    using one of the following values:

    <dl dfn-type=value dfn-for="step-position, <step-position>">

    :   <dfn value for="steps()">jump-start</dfn>
    ::  The first rise occurs at [=input progress value=] of 0.
    :   <dfn value for="steps()">jump-end</dfn>
    ::  The last rise occurs at [=input progress value=] of 1.
    :   <dfn value for="steps()">jump-none</dfn>
    ::  All rises occur within the range (0, 1).
    :   <dfn value for="steps()">jump-both</dfn>
    ::  The first rise occurs at [=input progress value=] of 0
        and the last rise occurs at [=input progress value=] of 1.
    :   <dfn value for="steps()">start</dfn>
    ::  Behaves as <a value for="steps()">jump-start</a>.
    :   <dfn value for="steps()">end</dfn>
    ::  Behaves as <a value for="steps()">jump-end</a>.

    </dl>

    If the second parameter is omitted, the value ''end'' is assumed.

    These values are illustrated below:

    <figure>
      <img src="step-easing-func-examples.svg" width="500"
          alt="Example step easing functions.">
      <figcaption>
        Example step easing functions.
      </figcaption>
    </figure>

</dl>

<h4 id=step-easing-algo oldids=step-timing-function-algo>Output of
a step easing function</h4>

At the exact point where a step occurs, the result of the function is
conceptually the top of the step. However, an additional <dfn>before flag</dfn>
passed as input to the [=step easing function=], if true, will cause the
result of the function to correspond to the bottom of the step at the step
point.

<div class=example>

As an example of how the [=before flag=] affects the behavior of this function,
consider an animation with a [=step easing function=] whose [=step
position=] is <a value for="steps()">start</a> and which has a positive
delay and backwards fill.

For example, using CSS animation:

<pre class='lang-css'>
animation: moveRight 5s 1s steps(5, start);
</pre>

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 easing 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 easing function will correspond to the top of the first step.

</div>

For the purposes of calculating the [=output progress value=], the
[=step position=] <a value for="steps()">start</a> is considered equivalent to
<a value for="steps()">jump-start</a>.
Likewise <a value for="steps()">end</a> is considered equivalent to <a value
for="steps()">jump-end</a>.
As a result, the following algorithm does not make explicit reference to
<a value for="steps()">start</a> or <a value for="steps()">end</a>.

Note: User agents must still differentiate between
<a value for="steps()">jump-start</a> and <a value for="steps()">start</a> for
the purpose of serialization (see [[#serialization]]).

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

1.   Calculate the <var>current step</var> as
     <code>floor([=input progress value=] &times; [=steps=])</code>.

1.   If the [=step position=] property is one of:

     * <a value for="steps()">jump-start</a>,
     * <a value for="steps()">jump-both</a>,

     increment <var>current step</var> by one.

1.   If <em>both</em> of the following conditions are true:

     *   the [=before flag=] is set, <em>and</em>
     *   [=input progress value=] &times; [=steps=] mod 1 equals zero
         (that is, if [=input progress value=] &times; [=steps=] is
         integral), then

     decrement <var>current step</var> by one.

1.   If [=input progress value=] &ge; 0 and <var>current step</var> &lt; 0,
     let <var>current step</var> be zero.

1.   Calculate |jumps| based on the [=step position=] as follows:

      :   <a value for="steps()">jump-start</a> or
          <a value for="steps()">jump-end</a>
      ::  [=steps=]
      :   <a value for="steps()">jump-none</a>
      ::  [=steps=] - 1
      :   <a value for="steps()">jump-both</a>
      ::  [=steps=] + 1

1.   If [=input progress value=] &le; 1 and <var>current step</var> &gt;
     |jumps|, let <var>current step</var> be |jumps|.

     <div class=note>

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

     For example, although mathematically we might expect that a step easing
     function with a [=step position=] of <a value for="steps()">jump-start</a>
     would step up (i.e. beyond 1) when the [=input progress value=] is 1,
     intuitively,
     when we apply such an easing 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 easing function with
     a [=step position=] of <a value for="steps()">jump-end</a>
     when applied to an animation during its delay phase.

     </div>

1.   The [=output progress value=] is <code><var>current step</var> /
     |jumps|</code>.


<h3 id=serialization oldids=serializing-a-timing-function>Serialization</h3>

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

*   The keyword values ''ease'', ''linear'', ''ease-in'', ''ease-out'',
    and ''ease-in-out'' are serialized as-is, that is, they are
    <em>not</em> converted to the equivalent ''cubic-bezier()''
    function before serializing.

*   Step easing functions, whether they are specified using the
    ''steps()'' function or either of the ''step-start'' or ''step-end''
    keywords, are serialized as follows:

    1.   If the [=step position=] is ''jump-end'' or ''end'', serialize
         as <a lt="steps()" function>steps(&lt;integer&gt;)</a>.

    2.   Otherwise, serialize as <a lt="steps()"
         function>steps(&lt;integer&gt;, &lt;step-position&gt;)</a>.

Privacy and Security Considerations {#priv-sec}
===================================

This specification does not directly introduce any new capabilities to the
Web platform but rather provides common definitions that may be referenced by
other specifications.
As a result, it does not introduce any new privacy and security concerns.

Specifications referencing the features defined in this specification
should consider that while easing functions most commonly
take an [=input progress value=] in the range [0,1]
and produce an [=output progress value=] in the range [0, 1],
this is not always the case.
Applications of easing functions should
define the behavior for inputs and outputs outside this range
to ensure they do not introduce new security considerations.

Changes since last publication {#changes}
==============================

The following changes have been made since the <a
  href="https://www.w3.org/TR/2018/WD-css-easing-1-20181009/">9 October
2018 Working Draft</a>:

*   Reworded specification to refer to &ldquo;easing functions&rdquo;
    rather than &ldquo;timing functions&rdquo;
    and use more general language that does not assume these functions are only
    applied to animations.

Acknowledgements {#acknowledgements}
================

This specification is based on the <a
href="https://www.w3.org/TR/css3-transitions/">CSS Transitions</a> 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 <a
href="https://damp-lake-50659.herokuapp.com/">Animation at Work</a> slack
community for their feedback and contributions.
