CSS Animations Level 2

Status: ED
Work Status: Exploring
Shortname: css-animations-2
Level: 2
Group: csswg
ED: http://dev.w3.org/csswg/css-animations-2/
Editor: L. David Baron, Mozilla, dbaron@dbaron.org
!Issues List: In Bugzilla

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.
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: animation
    text: animation playback rate
    text: animation type
    text: composite operation
    text: current iteration
    text: current time
    text: global animation list
    text: idle play state
    text: pause an animation
    text: play an animation
    text: sampling
    text: start delay
    text: target element
    text: target effect
    text: target effect end

Delta specification

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.

# Animations # {#animations} ## Owning element ## {#owning-element-section} The owning element 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 owning element. 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 owning element, the animation is disassociated from its owning element (that is, it has no owning element from that point forwards).
In the example below, animation's initial owning element is elem. animation is disassociated from element through an update to the computed value of elem's 'animation-name' property.
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
Note that although the owning element is often equal to the target element of an animation's target effect, this is not always the case. The following example demonstrates some of the situations where these two elements may differ.
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
## Animation composite order ## {#animation-composite-order} Animations generated from the markup or interfaces (e.g. the {{CSSAnimation}} constructor) defined in this specification have an animation type of ‘CSS Animation’. CSS Animations with an owning element have a later composite order than CSS Transitions but an earlier composite order than animations without a specific animation type. Within the set of CSS Animations with an owning element, two animations A and B are sorted in composite order (first to last) as follows: 1. If the owning element of A and B differs, sort A and B by tree order of their corresponding owning elements. With regard to pseudo-elements, the sort order is as follows: * element * ::before * ::after * element children 1. Otherwise, sort A and B based on their position in the computed value of the 'animation-name' property of the (common) owning element. The composite order of CSS Animations without an owning element is based on their position in the global animation list. 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 not added to the global animation list when they are created. Instead, these animations are appended to the global animation list at the first moment when they transition out of the idle play state after being disassociated from their owning element. CSS Animations that have been disassociated from their owning element but are still idle do not have a defined composite order. Note, this behavior relies on the fact that disassociating an animation from its owning element always causes it to enter (or remain) in the idle play state. CSS Animations created using the {{CSSAnimation}} constructor are appended to the global animation list 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.
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 owning element from the next moment it becomes newly ''paused''. * If the computed 'animation-play-state' on the owning element is ''paused'', calling {{Animation/play()}} will temporarily override the 'animation-play-state' until it next becomes ''running''.
With regards to the pausing, an animation can be considered to be in one of five mutually-exclusive states: A state transition chart follows:
Initial state
Event A B C D E
Resulting state {{Animation/play()}} A B A B B
{{Animation/pause()}} C D C D D
'animation-play-state' → ''running'' A A C C A
'animation-play-state' → ''paused'' E B D D E
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 pause an animation 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 play an animation 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 → A) the auto-rewind flag for that procedure must be set to false; otherwise it must be set to true. ## The 'animation-composition' property ## {#animation-composition} The 'animation-composition' property defines the composite operation used when multiple animations affect the same property simultaneously.
Name: animation-composition
Value: <>#
Initial: replace
Applies to: all elements, ::before and ::after pseudo-elements
Inherited: none
Animatable: no
Percentages: N/A
Media: interactive
Computed value: As specified
Canonical order: per grammar
<single-animation-composition> = replace | add | accumulate The values of 'animation-composition' have the meaning defined for the corresponding values of the composite operation 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.
For example, the following stylesheet defines two different animations targetting the 'scale' property.
    @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;
    }
  
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.
    .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%);
      }
    }
  
Issue: Create pictures of these examples and verify they make sense.
# Animation Events # {#events} ## Types of AnimationEvent ## {#event-animationevent} The additional types of animation events that can occur are:
animationcancel
The animationcancel event occurs when the animation stops running in a way that does not fire an animationend event, such as a change in the 'animation-name' that removes the animation, or the animating element or one of its ancestors becoming ''display:none''.
## 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 using the Web Animations API [[WEB-ANIMATIONS]]. For the purpose of determining which events to dispatch, an animation can be considered to be in one of four mutually-exclusive event states determined using the following procedure: 1. If the animation is idle or has no target effect it is idle. 1. Otherwise, if the animation has a current time less than the start delay of its target effect, or, if the animation's playback rate is less than zero and it has a current time less than or equal to the start delay of its target effect, it is left-active. 1. Otherwise, if the animation has a current time greater than its target effect end, or, if the animation's playback rate is greater than or equal to zero and it has a current time greater than or equal to its target effect end, it is right-active. 1. Otherwise, it is active. Each time the animation is sampled, the events to dispatch are determined by comparing the event state before and after the sample as follows: : not activeactive :: animationstart : left-activeright-active : right-activeleft-active :: animationstart, animationend : activeleft-active : activeright-active :: animationend : activeactive :: animationiteration if there has been a change to the current iteration of the animation's target effect. : not idleidle :: animationcancel Issue: Define the value of elapsedTime for each case. # DOM Interfaces # {#interface-dom} ## The CSSAnimation interface ## {#the-CSSAnimation-interface}
interface CSSAnimation : Animation {
  readonly attribute DOMString animationName;
};
: animationName :: The key used to find matching keyframes rules that define target effect 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 animationName argument that was passed to the {{CSSAnimation}} constructor.
We need to define a constructor for CSSAnimation. Perhaps something like the following:
[Constructor (Animatable? target,
              DOMString animationName,
              optional (unrestricted double or KeyframeEffectOptions) options,
              optional DOMString defaultEasing = "ease"),
 Constructor (Animatable? target,
              DOMString animationName,
              (unrestricted double or KeyframeEffectOptions) options,
              DOMString defaultEasing,
              AnimationTimeline? timeline)]
partial interface CSSAnimation { };
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,
[Constructor (DOMString keyframesName, DOMString defaultEasing)]
interface CSSKeyframeEffectReadOnly : KeyframeEffectReadOnly {
  readonly attribute DOMString keyframesName;
  readonly attribute DOMString defaultEasing;
};
## Requirements on pending style changes ## {#requirements-on-pending-style-changes} Various operations may affect the computed values 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.
As an example, in the following code fragment, when the specified style of elem is initially updated, a user agent may defer recalculating the computed value of the 'animation' property. However, the {{Animatable/getAnimations()}} method called on elem 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 elem's 'animation' property and create the requested {{CSSAnimation}} object before returning its result.
elem.style.animation = 'fadeOut 1s';
elem.getAnimations()[0].pause();