71
loading...
This website collects cookies to deliver better user experience
Check out a live demo I've created that you can interact with as part of my Angular Animation Explorer.
npm install --save motion
If you run into any typings issue from the library, try adding skipLibCheck: true
to your tsconfig.json.
<div #myElement>...</div>
ViewChild
decorator to access the element defined above.import { Component, ViewChild, ElementRef } from '@angular/core';
@Component({
...
})
export class MotionOneDemoComponent {
@ViewChild('myElement') myElement: ElementRef;
}
import { Component, ViewChild, ElementRef } from '@angular/core';
import { animate } from 'motion';
@Component({
...
})
export class MotionOneDemoComponent {
@ViewChild('myElement') myElement: ElementRef;
animateMyElement(): void {
animate(
this.myElement.nativeElement,
{ rotate: 180 },
{ duration: 0.5, easing: 'ease-in' }
).finished.then(() => {
// animation completed
})
.catch(() => {
// if an error happens
});
}
}
spring
and glide
which, you can use by passing in their respective functions with any additional configurations. The snippet below is how you create a basic spring animation using Motion One:import { Component, ViewChild, ElementRef } from '@angular/core';
import { animate, spring } from 'motion';
@Component({
...
})
export class MotionOneDemoComponent {
@ViewChild('myElement') myElement: ElementRef;
animateMyElement(): void {
animate(
this.myElement.nativeElement,
{ rotate: 180 },
{ duration: 0.5, easing: spring() } // 👈 modify the easing
).finished.then(() => {
// animation completed
})
.catch(() => {
// if an error happens
});
}
}
timeline
function.timeline
function works similarly to Greensock's timeline feature. The code snippet below shows how you chain and sequence a translation of a box.import { Component, ViewChild, ElementRef } from '@angular/core';
import { timeline } from 'motion';
@Component({
...
})
export class MotionOneDemoComponent {
@ViewChild('myElement') myElement: ElementRef;
animateMyElement(): void {
const sequence = [
[this.myElement.nativeElement, { x: 100 }, { duration: 0.5 }],
[this.myElement.nativeElement, { y: 100 }, { duration: 0.5 }],
[this.myElement.nativeElement, { x: 0, y: 0 }, { duration: 1 }],
];
timeline(sequence)
.finished.then(() => {
// animation completed
})
.catch(() => {
// if an error happens
});
}
}