Animations
Overview
Ionic Animations is a tool that enables developers to create complex animations in a platform-agnostic manner, without requiring a specific framework or an Ionic app.
Creating efficient animations can be challenging, as it is limited by the available libraries and hardware resources of the device. Moreover, many animation libraries use a JavaScript-driven approach, which can reduce the scalability of animations and use up CPU time.
Ionic Animations, on the other hand, uses the Web Animations API, which offloads all the computation and running of animations to the browser. This approach allows the browser to optimize the animations and ensure their smooth execution. In cases where Web Animations are not supported, Ionic Animations will fall back to CSS Animations, which should have a negligible difference in performance.
Installation
- JavaScript
- TypeScript
- Angular
- React
- Vue
Developers using Ionic Core and JavaScript should install the latest version of @ionic/core
.
import { createAnimation } from 'https://cdn.jsdelivr.net/npm/@ionic/core@latest/dist/esm/index.mjs';
...
const animation = createAnimation()
.addElement(myElementRef)
.duration(1000)
.fromTo('opacity', '1', '0.5');
}
Developers using Ionic Core and TypeScript should install the latest version of @ionic/core
.
import { createAnimation, Animation } from '@ionic/core';
...
const animation: Animation = createAnimation('')
.addElement(myElementRef)
.duration(1000)
.fromTo('opacity', '1', '0.5');
}
Developers using Angular should install the latest version of @ionic/angular
. Animations can be created via the AnimationController
dependency injection.
import { Animation, AnimationController } from '@ionic/angular';
...
constructor(private animationCtrl: AnimationController) {
const animation: Animation = this.animationCtrl.create()
.addElement(myElementRef)
.duration(1000)
.fromTo('opacity', '1', '0.5');
}
Developers using React should install the latest version of @ionic/react
. React wrappers are in beta. Please report any issues on GitHub!
import { CreateAnimation, Animation } from '@ionic/react';
...
<CreateAnimation
duration={1000}
fromTo={{
property: 'opacity',
fromValue: '1',
toValue: '0.5'
}}
>
...
</CreateAnimation>
Developers using Ionic Vue should install the latest version of @ionic/vue
.
import { createAnimation } from '@ionic/vue';
import { ref } from 'vue';
...
const myElementRef = ref();
...
const animation = createAnimation()
.addElement(myElementRef.value)
.duration(1000)
.fromTo('opacity', '1', '0.5');
}
Basic Animations
Usage
- JavaScript
- Angular
- React
- Vue
createAnimation()
.addElement(document.querySelector('.square'))
.duration(1500)
.iterations(Infinity)
.fromTo('transform', 'translateX(0px)', 'translateX(100px)')
.fromTo('opacity', '1', '0.2');
this.animationCtrl.create()
.addElement(document.querySelector('.square'))
.duration(1500)
.iterations(Infinity)
.fromTo('transform', 'translateX(0px)', 'translateX(100px)')
.fromTo('opacity', '1', '0.2');
<CreateAnimation
duration={1500}
iterations={Infinity}
fromTo={[
{ property: 'transform', fromValue: 'translateX(0px)', toValue: 'translateX(100px)' },
{ property: 'opacity', fromValue: '1', toValue: '0.2' }
]}
>
...
</CreateAnimation>
import { createAnimation } from '@ionic/vue';
import { ref } from 'vue';
...
const elementRef = ref();
...
createAnimation()
.addElement(elementRef.value)
.duration(1500)
.iterations(Infinity)
.fromTo('transform', 'translateX(0px)', 'translateX(100px)')
.fromTo('opacity', '1', '0.2');
In the example above, an animation that changes the opacity on the .square
element and moves it from left to right along the X axis has been created. This animation will run an infinite number of times, and each iteration of the animation will last 1500ms.
By default, all Ionic Animations are paused until the play
method is called.
Keyframe Animations
Ionic Animations allows you to control the intermediate steps in an animation using keyframes. Any valid CSS property can be used here, and you can even use CSS Variables as values.
Hyphenated CSS properties should be written using camel case when writing keyframes. For example, border-radius
should be written as borderRadius
. This also applies to the fromTo()
, from(),
and to()
methods.
Usage
- JavaScript
- Angular
- React
- Vue
createAnimation()
.addElement(document.querySelector('.square'))
.duration(3000)
.iterations(Infinity)
.keyframes([
{ offset: 0, background: 'red' },
{ offset: 0.72, background: 'var(--background)' },
{ offset: 1, background: 'green' }
]);
this.animationCtrl.create()
.addElement(this.square.nativeElement)
.duration(3000)
.iterations(Infinity)
.keyframes([
{ offset: 0, background: 'red' },
{ offset: 0.72, background: 'var(--background)' },
{ offset: 1, background: 'green' }
]);
<CreateAnimation
duration={3000}
iterations={Infinity}
keyframes={[
{ offset: 0, background: 'red' },
{ offset: 0.72, background: 'var(--background)' },
{ offset: 1, background: 'green' }
]}
>
...
</CreateAnimation>
import { createAnimation } from '@ionic/vue';
import { ref } from 'vue';
...
const squareRef = ref();
...
createAnimation()
.addElement(squareRef.value)
.duration(3000)
.iterations(Infinity)
.keyframes([
{ offset: 0, background: 'red' },
{ offset: 0.72, background: 'var(--background)' },
{ offset: 1, background: 'green' }
]);
In the example above, the .square
element will transition from a red background color, to a background color defined by the --background
variable, and then transition on to a green background color.
Each keyframe object contains an offset
property. offset
is a value between 0 and 1 that defines the keyframe step. Offset values must go in ascending order and cannot repeat.
Grouped Animations
Multiple elements can be animated at the same time and controlled via a single parent animation object. Child animations inherit properties such as duration, easing, and iterations unless otherwise specified. A parent animation's onFinish
callback will not be called until all child animations have completed.