KeyframeEffect.setKeyframes()

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The setKeyframes() method of the KeyframeEffect interface replaces the keyframes that make up the affected KeyframeEffect with a new set of keyframes.

Syntax

existingKeyframeEffect.setKeyframes(keyframes);

Parameters

keyframes
A keyframe object or null. If set to null, the keyframes are replaced with a sequence of empty keyframes.

There are two different ways to format keyframes:

  1. An object containing key-value pairs consisting of the property to animate and an array of values to iterate over.

    element.animate({
      opacity: [ 0, 1 ],          // [ from, to ]
      color:   [ "#fff", "#000" ] // [ from, to ]
    }, 2000);
    
  2. An array of objects (keyframes) consisting of properties and values to iterate over. This is the canonical format returned by the getKeyframes() method.

    element.animate([ 
      { // from
        opacity: 0,
        color: "#fff"
      }, 
      { // to
        opacity: 1,
     ​   color: "#000"
      }
    ], 2000);

    With this latter form it is also possible to specify offsets for each keyframe by providing an offset value.

    element.animate([ { opacity: 1 },
                      { opacity: 0.1, offset: 0.7 },
                      { opacity: 0 } ],
                    2000);
    

    Note: offset values, if provided, must be between 0.0 and 1.0 and arranged in ascending order.

    Furthermore, using this form it is also possible to specify easing to apply between keyframes by providing an easing value as illustrated below.

    element.animate([ { opacity: 1, easing: 'ease-out' },
                      { opacity: 0.1, easing: 'ease-in' },
                      { opacity: 0 } ],
                    2000);
    

    In this example, the specified easing only applies from the keyframe where it is specified until the next keyframe. Any easing value specified on the options argument, however, applies across a single iteration of the animation — for the entire duration.

 

Attributes

You can specify as many property value pairs as are contained in each keyframe of the animation. Each property value pairs support the standard CSS properties, but also the following additional attributes:

offset

The offset of the keyframe specified as a number between 0.0 and 1.0 inclusive or null. This is equivalent to specifying start and end states in percentages in CSS stylesheets using @keyframes. This will be null if the keyframe is automatically spaced using KeyframeEffect.spacing.

computedOffset

The computed offset for this keyframe, calculated when the list of computed keyframes was produced according to KeyframeEffect.spacing. Unlike offset, above, the computedOffset is never null.

easing

The timing function used from this keyframe until the next keyframe in the series.

composite

The KeyframeEffectReadOnly.composite operation used to combine the values specified in this keyframe with the underlying value. This will be absent if the composite operation specified on the effect is being used.

 

 

Return value

Void.

Exceptions

Exception Explanation
TypeError One or more of the frames were not of the correct type of object, the keyframes were not loosely sorted by offset, or a keyframe existed with an offset of less than 0 or more than 1.

Note: If the keyframes cannot be processed or are malformed, the KeyframeEffect's keyframes are not modified.

Examples

// passing an array of keyframe objects
existingKeyframeEffect.setKeyframes(
[
  { color: 'blue' },
    { color: 'green', left: '10px' }
  ]
);

// passing an object with arrays for values
existingKeyframeEffect.setKeyframes(
  {
    color: ['blue', 'green'],
    left: [ '0', '10px']
  }
);

// passing a single-member object
existingKeyframeEffect.setKeyframes(
  {
    color: 'blue'
  }
);

Specifications

Specification Status Comment
Web Animations
The definition of 'KeyframeEffect.setKeyframes()' in that specification.
Working Draft Editor's draft.

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support Yes ?481 No No No
Clone constructor (2nd type of constructor) Yes ? No2 No No No
setKeyframes No No No No No No
composite ? ? ? ? ? ?
iterationComposite ? ? ? ? ? ?
target ? ? ? ? ? ?
getKeyframes ? ? ? ? ? ?
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes Yes ?481 No No Yes
Clone constructor (2nd type of constructor) Yes Yes ? No2 No No Yes
setKeyframes No No No No No No No
composite ? ? ? ? ? ? ?
iterationComposite ? ? ? ? ? ? ?
target ? ? ? ? ? ? ?
getKeyframes ? ? ? ? ? ? ?

1. The Web Animations API is only enabled by default in Firefox Nightly builds. It was enabled in Developer Edition until 58, when it is no longer enabled by default. You can enable it in beta and release builds by setting the preference dom.animations-api.core.enabled to true, and can disable it in any Firefox version by setting this preference to false.

2. Only enabled in Firefox 52 and later Nightly edition. Turned off in Beta/release.

See also

Document Tags and Contributors

 Contributors to this page: fscholz, birtles, rachelnabors
 Last updated by: fscholz,