<style type="text/css">
  table.play-state-transitions {
    empty-cells: hide;
    border-spacing: 0px;
  }
  table.play-state-transitions td {
    padding: 0.2em 1em;
    border: 1px solid black;
  }
  table.play-state-transitions th {
    text-align: center;
  }
  table.play-state-transitions th.rowgroup {
    transform: rotate(-180deg);
    writing-mode: vertical-lr;
    padding-left: 5px;
  }

  table.event-state-transitions {
    width: 100%;
    border-spacing: 0px;
    border-collapse: collapse;
  }
  table.event-state-transitions th:first-child {
    width: 30%;
  }
  table.event-state-transitions th {
    text-align: center;
  }
  table.event-state-transitions td {
    padding: 0.2em 1em;
    border: 1px solid black;
  }
</style>

<pre class='metadata'>
Title: CSS Animations Level 2
Status: ED
Work Status: Exploring
Shortname: css-animations-2
Level: 2
Group: csswg
ED: https://drafts.csswg.org/css-animations-2/
Editor: L. David Baron, Mozilla https://www.mozilla.org/, https://dbaron.org/, w3cid 15393
Editor: Brian Birtles, Mozilla https://www.mozilla.org/, bbirtles@mozilla.com, w3cid 43194
!Issues List: <a href="https://www.w3.org/Bugs/Public/buglist.cgi?component=Animations&list_id=36653&product=CSS&query_format=advanced&resolution=---">In Bugzilla</a>

Abstract: This CSS module describes a way for authors to animate the values of CSS properties over time, using keyframes. The behavior of these keyframe animations can be controlled by specifying their duration, number of repeats, and repeating behavior.
Ignored Vars: auto-rewind
</pre>
<pre class="anchors">
urlPrefix: https://w3c.github.io/web-animations/; type: interface; spec: web-animations
    text: Animation
    text: KeyframeEffectReadOnly
urlPrefix: https://w3c.github.io/web-animations/; type: method; for: Animation; spec: web-animations
    text: play()
    text: pause()
urlPrefix: https://w3c.github.io/web-animations/; type: method; for: KeyframeEffectReadOnly; spec: web-animations
    text: getFrames()
urlPrefix: https://w3c.github.io/web-animations/; type: dfn; spec: web-animations
    text: active duration
    text: active phase
    text: active time
    text: after phase
    text: animation
    text: animation class
    text: animation effect
    text: animation playback rate
    text: before phase
    text: composite operation
    text: current iteration
    text: current time
    text: fill mode
    text: global animation list
    text: idle play state
    text: idle phase
    text: iteration duration
    text: iteration start
    text: pause an animation
    text: pending play state
    text: play an animation
    text: sampling
    text: start delay
    text: target element
    text: target effect
    text: target effect end
    text: unresolved
</pre>

<h2 id="delta">Delta specification</h2>

<p>This is a delta specification, meaning that it currently contains
only the differences from CSS Animations Level 1 [[!CSS3-ANIMATIONS]].
Once the Level 1 specification is closer to complete, it will be merged
with the additions here into a complete level 2 specification.</p>

# Animations # {#animations}

## Owning element ## {#owning-element-section}

The <dfn>owning element</dfn> of an animation refers to the element or
pseudo-element to which the 'animation-name' property was applied that generated
the animation.

If an animation was generated directly by script (e.g. using
the {{CSSAnimation}} constructor) then it has no <a>owning element</a>.

If an animation generated using the markup defined in this specification is
later disassociated from that markup by an update to the computed value of the
'animation-name' property on the <a>owning element</a>, the animation is
disassociated from its <a>owning element</a> (that is, it has no <a>owning
element</a> from that point forwards).

<div class="note">

In the example below, <code>animation</code>'s initial <a>owning element</a>
is <code>elem</code>. <code>animation</code> is disassociated from
<code>element</code> through an update to the computed value of
<code>elem</code>'s 'animation-name' property.

<pre class="example lang-javascript">
elem.style.animation = 'spin 1s';
let animation = elem.getAnimations()[0]; // animation's owning element is elem
elem.style.animation = ''; // animation no longer has an owning element
</pre>

Note that although the <a>owning element</a> is often equal to the <a>target
element</a> of an animation's <a>target effect</a>, this is not always the case.
The following example demonstrates some of the situations where these two
elements may differ.

<pre class="example lang-javascript">
elem.style.animation = 'move 1s';
let animation = elem.getAnimations()[0];
// animation.effect.target == elem == animation's owning element

let mutableEffect = animation.effect.clone();
animation.effect = mutableEffect;
animation.effect.target = elem2;
// animation.effect.target == elem2 != animation's owning element

animation.effect = null;
// animation.effect.target is undefined != animation's owning element
</pre>

</div>

## Animation composite order ## {#animation-composite-order}

<a spec='web-animations'>Animations</a> generated from the markup or interfaces
(e.g. the {{CSSAnimation}} constructor) defined in this specification have an
<a>animation class</a> of &lsquo;CSS Animation&rsquo;.

CSS Animations <em>with</em> an <a>owning element</a> have a <em>later</em>
composite order than CSS Transitions but an <em>earlier</em> composite order
than animations without a specific <a>animation class</a>.

Within the set of CSS Animations <em>with</em> an <a>owning element</a>, two
animations <var>A</var> and <var>B</var> are sorted in composite order (first to
last) as follows:

1.  If the <a>owning element</a> of <var>A</var> and <var>B</var>
    differs, sort <var>A</var> and <var>B</var> by <a>tree order</a>
    of their corresponding <a>owning elements</a>.
    With regard to pseudo-elements, the sort order is as follows:

    *  element
    *  ::marker
    *  ::before
    *  ::after
    *  element children

1.  Otherwise, sort <var>A</var> and <var>B</var> based on their position in the
    computed value of the 'animation-name' property of the (common) <a>owning
    element</a>.

The composite order of CSS Animations <em>without</em> an <a>owning element</a>
is based on their position in the <a>global animation list</a>.

Issue: This differs from the behavior defined for transitions. We should
probably sort transitions first, then animation, then use the global animation
list. The reason being that when developer tools etc. hang on to orphaned
animations and transitions in order to replay them, they should maintain
roughly the same composite order.

CSS Animations generated using the markup defined in this specification are
<em>not</em> added to the <a>global animation list</a> when they are created.
Instead, these animations are appended to the <a>global animation list</a> at
the first moment when they transition out of the <a>idle play state</a> after
being disassociated from their <a>owning element</a>.
CSS Animations that have been disassociated from their <a>owning element</a>
but are still <a lt="idle play state">idle</a> do not have a defined
composite order.

Note, this behavior relies on the fact that disassociating an animation
from its <a>owning element</a> always causes it to enter (or remain) in the
<a>idle play state</a>.

CSS Animations created using the {{CSSAnimation}} constructor are appended
to the <a>global animation list</a> at the moment they are constructed.

# Keyframes # {#keyframes}

## The 'animation-play-state' property ## {#animation-play-state}

### Interaction between 'animation-play-state' and the Web Animations API ### {#interaction-between-animation-play-state-and-web-animations-API}

Both this specification and the Web Animations specification [[WEB-ANIMATIONS]]
define mechanisms for pause control, specifically the 'animation-play-state'
property, and the {{Animation/play()}} and {{Animation/pause()}} methods
respectively.

<div class="note">

The interaction of these methods can be summarized as follows:

*   Calling {{Animation/pause()}} is sticky. The animation will remain paused
    until there is a subsequent call to {{Animation/play()}} after which point
    the animation will begin tracking changes to the computed value of the
    'animation-play-state' property on the <a>owning element</a> from the next
    moment it becomes newly ''paused''.
*   If the computed 'animation-play-state' on the <a>owning element</a> is
    ''paused'', calling {{Animation/play()}} will temporarily override the
    'animation-play-state' until it next becomes ''running''.

</div>

With regards to the pausing, an animation can be considered to be in one of
five mutually-exclusive states:

<ul style="list-style-type: upper-latin">
<li>Running
<li>Running and temporarily overriding 'animation-play-state': ''paused''
<li>Paused and sticky overriding 'animation-play-state': ''running''
<li>Paused and sticky overriding 'animation-play-state': ''paused''
<li>Paused by 'animation-play-state'
</ul>

A state transition chart follows:

<table class="play-state-transitions">
<tr>
  <td rowspan="2"></td>
  <td></td>
  <th colspan="5">Initial state</th>
</tr>
<tr>
  <th>Event</th>
  <th>A</th>
  <th>B</th>
  <th>C</th>
  <th>D</th>
  <th>E</th>
</tr>
<tr>
  <th rowspan="4" class="rowgroup">Resulting state</th>
  <td>{{Animation/play()}}</td>
  <td>A</td>
  <td>B</td>
  <td>A</td>
  <td>B</td>
  <td>B</td>
</tr>
<tr>
  <td>{{Animation/pause()}}</td>
  <td>C</td>
  <td>D</td>
  <td>C</td>
  <td>D</td>
  <td>D</td>
</tr>
<tr>
  <td>'animation-play-state' &rarr; ''running''</td>
  <td>A</td>
  <td>A</td>
  <td>C</td>
  <td>C</td>
  <td>A</td>
</tr>
<tr>
  <td>'animation-play-state' &rarr; ''paused''</td>
  <td>E</td>
  <td>B</td>
  <td>D</td>
  <td>D</td>
  <td>E</td>
</tr>
</table>

If any change causes an animation to change from  one of the running states (A,
B) to one of the paused states (C, D, E), the user agent must run the <a>pause
an animation</a> procedure on the animation.

Likewise, for any change from one of the the paused states to one of the
running states, the user agent must run the <a>play an animation</a> procedure
on the animation. If the change is due to a change in the computed value of
'animation-play-state' (i.e. the transition E &rarr; A) the
<var>auto-rewind</var> flag for that procedure must be set to false; otherwise
it must be set to true.

</div>

## The 'animation-composition' property ## {#animation-composition}

The 'animation-composition' property defines the <a>composite operation</a>
used when multiple animations affect the same property simultaneously.

<pre class='propdef'>
Name: animation-composition
Value: <<single-animation-composition>>#
Initial: replace
Applies to: all elements
Inherited: no
Percentages: N/A
Computed value: list, each item a keyword as specified
Animation type: not animatable
Canonical order: per grammar
</pre>

<dfn>&lt;single-animation-composition></dfn> = replace | add | accumulate

The values of 'animation-composition' have the meaning defined for the
corresponding values of the <a>composite operation</a> defined in Web
Animations [[!WEB-ANIMATIONS]].

When specified in a keyframe, 'animation-composition' defines the composite
operation to use for each property specified in that keyframe until the next
keyframe specifying each property.

<div class='example'>
  For example, the following stylesheet defines two different animations
  targetting the 'scale' property.

  <pre>
    @keyframes heartbeat {
      from {
        scale: 1;
        animation-timing-function: ease-out;
      }
      30% {
        scale: 1.3;
      }
    }
    .heartbeat {
      animation: heartbeat 0.3s 2s infinite;
    }

    @keyframes throb {
      50% {
        scale: 1.8;
      }
    }
    .icon:mouseover {
      animation: throb 0.4s add;
    }
  </pre>

  If these two animations are applied to the same element, normally only
  one animation would apply, but by specifying ''add'' as the
  'animation-composition' on the second animation, the result of the two
  animations will be combined.

  Since CSS Transitions [[CSS3-TRANSITIONS]] have a lower composite
  order, it is possible to use 'animation-composition' to combine CSS
  Animations with underlying transitions as in the following example.

  <pre>
    .icon {
      filter: blur(20px);
      transition: filter 0.5s;
    }
    .icon:hover {
      filter: blur(0px);
      animation: brightness-pulse 3s infinite add;
    }

    @keyframes pulse {
      0% {
        scale: 1.1;
        filter: brightness(130%);
      }
      10% {
        scale: 1;
        filter: brightness(100%);
      }
    }
  </pre>

  Issue: Create pictures of these examples and verify they make sense.
</div>

# Animation Events # {#events}

## Event dispatch ## {#event-dispatch}

Note, this is a more general description of event dispatch than that of CSS
Animations Level 1 [[CSS3-ANIMATIONS]] since it must account for the
possibility of animations being seeked or reversed using the Web Animations API
[[WEB-ANIMATIONS]].

For the purpose of determining which events to dispatch, the
[[web-animations#animation-effect-phases-and-states|phases]] defined in
the Web Animations model are used. These definitions apply to an <a>animation
effect</a>, however, for the purpose of dispatching events, we consider a
CSS Animation to have the same phase as its <a>target effect</a>. For example,
a CSS Animation is in the <a>before phase</a> if its <a>target effect</a>
is in the <a>before phase</a>.

A CSS Animation that does not have a <a>target effect</a> is considered to be in
the <a>idle phase</a> if its <a>current time</a> is <a>unresolved</a>, in the
<a>before phase</a> if its <a>current time</a> is less than zero, and in the
<a>after phase</a> otherwise.

Similarly, subsequent references to the <a>start delay</a>, <a>active
duration</a>, <a>current iteration</a>, <a>iteration start</a>, and
<a>iteration duration</a> of a CSS animation should be understood to refer
to the corresponding properties of the animation's <a>target effect</a>.

For calculating the {{AnimationEvent/elapsedTime}} of each event, the following
definitions are used:

*   <dfn>interval start</dfn> =
    <code>max(min(-<a>start delay</a>, <a>active duration</a>), 0)</code>
*   <dfn>interval end</dfn> =
    <code>max(min(<a>target effect end</a> - <a>start delay</a>,
                  <a>active duration</a>), 0)</code>

Each time an animation is <a lt="sampling">sampled</a> and is <em>not</em> <a
lt="pending play state">pending</a>, the events to dispatch are determined by
comparing the animation's phase before and after the sample as follows:

<table class="event-state-transitions">
  <thead>
    <tr>
      <th>Change</th>
      <th>Events dispatched</th>
      <th><dfn>Elapsed time</dfn> (ms)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a lt="idle phase">idle</a> or <a lt="before phase">before</a> &rarr;
          <a lt="active phase">active</a></td>
      <td>{{animationstart}}</td>
      <td><a>interval start</a>
      </td>
    </tr>
    <tr>
      <td rowspan="2">
        <a lt="idle phase">idle</a> or <a lt="before phase">before</a> &rarr;
        <a lt="after phase">after</a> <a
        href="#multiple-events-note">&#x66d;</a></td>
      <td>{{animationstart}}</td>
      <td><a>interval start</a>
      </td>
    </tr>
    <tr>
      <td>{{animationend}}</td>
      <td><a>interval end</a>
      </td>
    </tr>
    <tr>
      <td><a lt="active phase">active</a> &rarr;
          <a lt="before phase">before</a></td>
      <td>{{animationend}}</td>
      <td><a>interval start</a></td>
    </tr>
    <tr>
      <td><a lt="active phase">active</a> &rarr;
          <a lt="active phase">active</a>
          <em>and</em>
          the <a>current iteration</a> of the animation's <a>target effect</a>
          has changed since the previous sample
      </td>
      <td>{{animationiteration}}</td>
      <td>(See below)
          <a href="#animation-iteration-elapsed-time">&dagger;</a></td>
    </tr>
    <tr>
      <td><a lt="active phase">active</a> &rarr;
          <a lt="after phase">after</a></td>
      <td>{{animationend}}</td>
      <td><a>interval end</a></td>
    </tr>
    <tr>
      <td><a lt="after phase">after</a> &rarr;
          <a lt="active phase">active</a></td>
      <td>{{animationstart}}</td>
      <td><a>interval end</a></td>
    </tr>
    <tr>
      <td rowspan="2"><a lt="after phase">after</a> &rarr;
                      <a lt="before phase">before</a> <a
                      href="#multiple-events-note">&#x66d;</a></td>
      <td>{{animationstart}}</td>
      <td><a>interval end</a></td>
    </tr>
    <tr>
      <td>{{animationend}}</td>
      <td><a>interval start</a></td>
    </tr>
    <tr>
      <td><em>not</em> <a lt="idle phase">idle</a> and <em>not</em>
          <a lt="after phase">after</a>&rarr;
          <a lt="idle phase">idle</a></td>
      <td>{{animationcancel}}</td>
      <td>The <a>active time</a> of the animation at the moment it was cancelled
          calculated using a <a>fill mode</a> of both.</td>
    </tr>
  </tbody>
</table>

<p id="multiple-events-note">&#x66d; Where multiple events are listed for
a state change, all events are dispatched in the order listed and in immediate
succession.</p>

<p id="animation-iteration-elapsed-time">&dagger; The <a>elapsed time</a> for
an {{animationiteration}} event is defined as follows:</p>

1.  Let <var>previous current iteration</var> be the <a>current iteration</a>
    from the previous sample.

1.  If <var>previous current iteration</var> is greater than <a>current
    iteration</a>, let <var>iteration boundary</var> be <code><a>current
    iteration</a> + 1</code>, otherwise let it be <a>current iteration</a>.

1.  The <a>elapsed time</a> is the result of evaluating
    <code>(<var>iteration boundary</var> - <a>iteration start</a>) &times;
    <a>iteration duration</a>)</code>.

Since the <a>elapsed time</a> defined in the table and procedure above is
expressed in milliseconds, it must be divided by 1,000 to produce a value in
seconds before being assigned to the {{AnimationEvent/elapsedTime}} member of
the {{AnimationEvent}}.

# DOM Interfaces # {#interface-dom}

## The CSSAnimation interface ## {#the-CSSAnimation-interface}

<pre class="idl">
[Exposed=Window]
interface CSSAnimation : Animation {
  readonly attribute CSSOMString animationName;
};
</pre>

:   <dfn attribute for=CSSAnimation>animationName</dfn>
::  The key used to find matching keyframes rules that define <a>target
    effect</a> at the point when the animation was created.
    This is the value of the 'animation-name' property that caused this
    object to be generated or, if this object was generated using the
    programming interface, the <code>animationName</code> argument
    that was passed to the {{CSSAnimation}} constructor.

<div class="issue">

We need to define a constructor for {{CSSAnimation}}.
Perhaps something like the following:

<pre class="idl">
[Constructor (Animatable? target,
              CSSOMString animationName,
              optional (unrestricted double or KeyframeEffectOptions) options,
              optional CSSOMString defaultEasing = "ease"),
 Constructor (Animatable? target,
              CSSOMString animationName,
              (unrestricted double or KeyframeEffectOptions) options,
              CSSOMString defaultEasing,
              AnimationTimeline? timeline)]
partial interface CSSAnimation { };
</pre>

The difficulty is with liveness. The most useful and least magical
(but most complicated) approach is to define a subclass of
{{KeyframeEffectReadOnly}} that has the special behavior of tracking changes
to all @keyframes rules that match the supplied name and automatically
updating the set of keyframes returned by
{{KeyframeEffectReadOnly/getFrames()}} after filling-in the default easing.

Something like,

<pre class="idl">
[Exposed=Window,
 Constructor (CSSOMString keyframesName, CSSOMString defaultEasing)]
interface CSSKeyframeEffectReadOnly : KeyframeEffectReadOnly {
  readonly attribute CSSOMString keyframesName;
  readonly attribute CSSOMString defaultEasing;
};
</pre>

</div>

## Requirements on pending style changes ## {#requirements-on-pending-style-changes}

Various operations may affect the <a lt="computed value">computed values</a> of
properties on elements. User agents may, as an optimization, defer recomputing
these values until it becomes necessary.
However, all operations included in programming interface defined in this
specification, as well as those operations defined in Web Animations
[[!WEB-ANIMATIONS]] that may return objects defined by this specification,
must produce a result consistent with having fully processed any such pending
changes to computed values.

<div class="note">
As an example, in the following code fragment, when the specified style of
<code>elem</code> is initially updated, a user agent may defer recalculating
the computed value of the 'animation' property.

However, the {{Animatable/getAnimations()}} method called on <code>elem</code>
is specified by Web Animations and can return {{CSSAnimation}} objects as
defined in this specification.
Hence, as result of the requirements in this section, the user agent must
calculate the updated value of <code>elem</code>'s 'animation' property and
create the requested {{CSSAnimation}} object before returning its result.

<div><pre class="example lang-javascript">
elem.style.animation = 'fadeOut 1s';
elem.getAnimations()[0].pause();
</pre></div>

</div>



<h2 id="priv-sec">
Privacy and Security Considerations</h2>

This specification introduces no new privacy or security considerations.
