Animate from code
Call animate() on an element to play keyframes you choose at runtime, such as a pulse on tap or a shake on an error. It returns a handle you can pause, resume or cancel.
Run an animation
src/app/pulse.ts
nativeElementisn't a DOM element. On Lynx,viewChild()gives you anElementRefwhosenativeElementis AngularLynx's wrapper around a native element. It hasanimate(), but nostyle,classListor other DOM properties.- Start it straight from the tap handler, as on the web.
- Cancel the previous animation first. Each call starts a new animation alongside any that are still running.
- Keyframes are an array of objects whose keys are CSS property names. Only some properties animate.
Set the options
The second argument takes these common options:
- A number on its own is the duration:
animate(keyframes, 300). - Without
fill: 'forwards', the element goes back to its own styles when the animation ends. - AngularLynx also accepts Lynx's CSS-style names:
timingFunction,iterationCountandfillMode. namelabels the animation in its element's events (see below).
The animation API reference lists every option.
Pause, resume and cancel
animate() returns a LynxAnimation handle with three methods:
src/app/spinner.ts
pause()freezes the animation at its current frame, andplay()resumes it.cancel()stops the animation for good. Start a new one withanimate().- There's no
finish(), nofinishedpromise and noonfinishcallback. To run code when an animation ends, start asetTimeoutfor its total length. - On iOS and Android, the element's
(bindanimationstart)and(bindanimationend)fire for it, with itsnamein$event.params.animation_name. The web preview doesn't fire them, which is why asetTimeoutis the way that works everywhere. - Removing the element cancels its animations for you. You don't need to clean up when a component is destroyed.
Where it runs
AngularLynx runs your app on two threads (see Threads). Only the main thread copy animates real elements, and that's where event handlers run, so calling animate() from a handler works.
The background thread copy has no native elements. There, animate() does nothing and returns a handle whose methods do nothing. Development builds log a warning when that happens.
Limitations
- Don't call
animate()from an event that the animation can fire again. Starting or canceling an animation can re-run event handlers on the same element or one nearby. Fading an<image>in from its own(bindload)handler, for example, can loop until the app crashes. If you must, guard the handler so it runs once. - On Android, an element may need
flatten="false"to show its animation. Android can draw a plain element straight into its parent instead of giving it a native view of its own. Such an element's animation runs and fires its events, but nothing moves. Seeflatten. - No Web Animations API extras. There's no
document.getAnimations(),KeyframeEffectorcurrentTime.
Related
- Animations: CSS transitions and
@keyframes - Enter and leave transitions
- Animation API reference
- Main-thread scripts: animate on every frame, such as while dragging
