Aperture UI
Content development

Animations

CSS animations and transitions, the Web Animations API, requestAnimationFrame, Lottie and the C++ animation driver, with the limits of each.

Aperture UI provides CSS animations, CSS transitions and a JavaScript animation API. CSS animations, CSS transitions and script animations all advance on one clock, and that clock moves only when the host calls view->tick(deltaSeconds). Pause the tick and every animation pauses with it. requestAnimationFrame callbacks run from the same call, so they run at your game's frame rate and not at a fixed rate. With several views, the clock advances once per frame, from the first view that is ticked (see Your own C++ host).

CSS Animations

Define keyframes and apply them with the animation shorthand or its longhands:

@keyframes fade-in {
  from { opacity: 0; transform: translateY(-20px); }
  to   { opacity: 1; transform: translateY(0); }
}

.panel {
  animation: fade-in 0.3s ease-out forwards;
}

The animation-* properties are all parsed:

PropertyNotes
animation-nameAn @keyframes name
animation-durationA time, in s or ms
animation-timing-functionSee the easing table below
animation-delayA time, in s or ms
animation-iteration-countA number (fractions are allowed) or infinite
animation-directionnormal, reverse, alternate, alternate-reverse
animation-fill-modenone, forwards, backwards, both
animation-play-staterunning, paused
animation-compositionreplace, add, accumulate

A page can listen for animationstart, animationiteration, animationend and animationcancel. Pseudo-elements animate too (a test page covers ::before and ::after).

Not available: scroll-driven and view-driven timelines. animation-timeline and the related properties are not implemented.

CSS Transitions

.button {
  background: #3a3a5e;
  transform: scale(1);
  transition:
    background 0.2s ease,
    transform  0.1s ease-out;
}

.button:hover {
  background: #5a5a8e;
  transform: scale(1.05);
}

transition and its longhands (transition-property, -duration, -timing-function, -delay) are supported. The allow-discrete keyword in the transition shorthand lets properties that cannot be interpolated take part; they switch at the halfway point. There is no separate transition-behavior property. @starting-style gives an element its first style, so an element that has just been added can transition in. A test page covers it. The events are transitionrun, transitionstart, transitionend and transitioncancel.

Easing Functions

FormIn CSSIn animate() options
linearYesYes
ease, ease-in, ease-out, ease-in-outYes. ease is the default: slow start, fast middle, slow end.Yes
cubic-bezier(x1, y1, x2, y2)Yes. Solved with Newton-Raphson iteration.Yes
steps(n) with jump-start, jump-end, jump-none or jump-both, and step-start, step-endYesFalls back to linear
linear(...), the piecewise form from CSS Easing Level 3Yes, up to a fixed number of stopsFalls back to linear

Web Animations API (JavaScript)

element.animate(keyframes, options) returns an Animation you can pause, reverse, finish or cancel. It accepts keyframes as an array of objects or as an object of arrays, and a duration in milliseconds as the second argument or an options object with duration, delay, iterations, direction, fill, easing, composite and id. getAnimations() is available on elements and on document, and document.timeline exists.

const animation = panel.animate(
  [{ opacity: 0 }, { opacity: 1 }],
  { duration: 300, easing: "ease-out", fill: "forwards" }
);

animation.onfinish = () => console.log("done");

A per-keyframe easing or composite is ignored. Set them in the options object, where they apply to the whole animation.

An Animation has play(), pause(), reverse(), finish() and cancel(), and the properties currentTime, startTime, playbackRate, playState, effect, timeline, id, onfinish and oncancel. finished and ready are promises.

Script animations cover a fixed list of properties

animate() turns only the properties below into animation tracks. A keyframe property that is not on the list is dropped without a warning or an error. For any other property, define the animation in CSS and toggle a class.

The properties animate() understands are opacity, color, backgroundColor, width, height, the four margin* and padding* sides, top, right, bottom, left, fontSize, fontWeight, letterSpacing, lineHeight, borderRadius, zIndex, flexGrow, flexShrink, outlineWidth, outlineColor and outlineOffset, in camelCase (kebab-case also works for the names that have a hyphen).

Transforms are written as separate numeric properties: translateX, translateY and translateZ in pixels, scaleX, scaleY and scaleZ, and rotate, rotateX, rotateY and rotateZ in degrees. The transform key is not a transform list here. It maps to a single rotation track, so write { translateY: -20 } and not { transform: "translateY(-20px)" }:

panel.animate(
  [{ opacity: 0, translateY: -20 }, { opacity: 1, translateY: 0 }],
  { duration: 300, easing: "ease-out", fill: "forwards" }
);

commitStyles() is a no-op, so the animated values do not stay after the animation ends.

requestAnimationFrame

requestAnimationFrame and cancelAnimationFrame work as in a browser:

let start;
function spin(ts) {
  if (!start) start = ts;
  const elapsed = ts - start;
  spinner.style.transform = `rotate(${elapsed * 0.36}deg)`;
  requestAnimationFrame(spin);
}
requestAnimationFrame(spin);

Callbacks fire from UIView::tick(), so they run at your game's frame rate, not at a fixed rate. Writing element.style.transform or opacity every frame is a cheap way to drive an animation from script: the engine recognizes a change confined to paint-only properties and skips layout for it. See Rendering performance.

Composite Operations

Element.animate() accepts a composite option:

ValueMeaning
replace (default)New animation values replace the underlying value
addEffect adds to the current animated value
accumulateTreated identically to add in this engine. It is not true per-iteration accumulation.

Lottie Animations

Drop in .json files exported from Adobe After Effects with the Bodymovin plugin. The rlottie library renders them, and it is part of the standard SDK download.

<lottie-player src="animations/victory.json" autoplay loop></lottie-player>

There is no JavaScript control API for <lottie-player>. The attributes control it. Lottie JSON files are compact and animation-rich, which makes them a good fit for icons, logo intros and small illustrative animations in place of a GIF or a video. See Lottie for the attributes, the cost and what rlottie does not render.

Animatable Properties

CategoryExamplesWhat an animation frame costs
Paint-onlyopacity, transform, color, background-color, border-colorRepaint only, no layout
Layoutwidth, height, padding-*, margin-*, border-width, top, left, right, bottomLayout for the affected elements, then repaint

Prefer paint-only properties for animation that runs every frame. Animating width or top on every frame forces a layout pass on every tick for the elements it touches. Use a transform when the visual result is the same.

Stacking and Compositing

  • opacity below 1, filter, backdrop-filter, a blend mode and a mask each draw their element into an off-screen target, so many of them animating at once adds up.
  • will-change creates a stacking context for transform, opacity, filter and backdrop-filter. It does not create an off-screen target, so it is not a way to buy a cached layer. Neither do transforms.

See Rendering architecture and Rendering performance.

C++ Animation Driver

A host built against the engine source can create animations from C++, for animations driven by game logic rather than by CSS or script:

auto element = view->getDomDocument()->getElementById("health-bar-fill");
auto animation = element->animate(std::move(tracks), animOptions);
animation->Play();

animate() is a method on DOMElement (not DOMWindow), taking a keyframe track list and options, with no separate element argument. It returns a controllable Animation with PascalCase methods: Play(), Pause(), Stop(), Reset(), Reverse() and Seek().

The 26.08.0.1 package includes the DOM and animation headers needed for this path. Include APHTML/DOM/DOMDocument.h and APHTML/DOM/DOMElement.h, keep calls on the view thread, and destroy animation and element handles before the view. CSS classes and page functions remain convenient alternatives (see Scripting).

Performance Notes

  • The animation engine ticks once per UIView::tick() call, and animations advance by deltaSeconds.
  • Animation work is on the thread that ticks the view. Compositing happens in your renderer.
  • Animating opacity, transform and colors needs a repaint and not layout. This is the same in CSS, in script and in animate().
  • Animating width, height, margin, padding, top or left, including a CSS transition on a layout property, runs layout for the affected elements on every frame of the animation. Prefer a transform when the visual result is the same.
  • Measure animation cost in the host. The details are in Rendering performance.

On this page