1.0.0 • Published 4 months ago
angular-dotlottie v1.0.0
Angular DotLottie
An Angular wrapper for the @dotlottie/player-component library, which allows you to easily integrate .lottie animations into your Angular applications.
Features
- 🚀 Easy integration with Angular projects
- 🎨 Full support for all DotLottie features
- 🔄 Two-way data binding for animation controls
- 📦 Component and directive approaches for flexibility
- 🛠 TypeScript support
- 🎬 Event emitters for animation lifecycle events
- 🔧 Zone-optimized for performance
Installation
npm install angular-dotlottie @dotlottie/player-component
Basic Usage
1. Import the Module
Add the AngularDotLottieModule
to your app module:
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { AngularDotLottieModule } from "angular-dotlottie";
import { AppComponent } from "./app.component";
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AngularDotLottieModule],
bootstrap: [AppComponent],
})
export class AppModule {}
2. Component Approach
Use the ng-dotlottie-player
component in your templates:
<ng-dotlottie-player src="assets/animations/animation.lottie" [autoplay]="true" [loop]="true" [controls]="true" (ready)="onAnimationReady($event)" (complete)="onAnimationComplete($event)"> </ng-dotlottie-player>
3. Directive Approach
Use the ngDotLottie
directive on any element:
<div [ngDotLottie]="'assets/animations/animation.lottie'" [autoplay]="true" [loop]="true"></div>
API Reference
Inputs
Input | Type | Default | Description |
---|---|---|---|
src | string | null | URL or path to the .lottie animation file |
autoplay | boolean | false | Automatically start animation |
loop | boolean | false | Loop the animation |
controls | boolean | false | Display animation controls |
mode | PlayMode | Forward | Animation play mode (Forward, Reverse, Bounce) |
speed | number | 1 | Animation playback speed |
intermission | number | 0 | Time between loops (in milliseconds) |
backgroundColor | string | null | Background color for the animation |
height | string|number | 'auto' | Height of the animation container |
width | string|number | '100%' | Width of the animation container |
customControls | Controls | null | Custom controls configuration |
Outputs (Events)
Output | Description |
---|---|
ready | Animation is ready to play |
play | Animation started playing |
pause | Animation was paused |
stop | Animation was stopped |
complete | Animation completed |
frame | Frame change event |
load | Animation loaded successfully |
error | Error occurred |
freeze | Animation was frozen |
loopComplete | Animation loop completed |
enterFrame | New animation frame entered |
Methods
Method | Description | Parameters |
---|---|---|
playAnimation() | Start playing the animation | - |
pauseAnimation() | Pause the animation | - |
stopAnimation() | Stop the animation | - |
setSpeed() | Change animation speed | speed: number |
setDirection() | Change animation direction | direction: 1 | -1 |
setLooping() | Enable/disable looping | loop: boolean |
setMode() | Change play mode | mode: PlayMode |
seekTo() | Go to a specific frame | frame: number |
takeSnapshot() | Take a snapshot of the animation | download?: boolean |
freezeAnimation() | Freeze the animation | - |
loadAnimation() | Load a new animation | src: string |
Enums
enum PlayMode {
Forward = "forward",
Reverse = "reverse",
Bounce = "bounce",
}
Advanced Usage
Controlling Animations with ViewChild
import { Component, ViewChild } from "@angular/core";
import { DotLottiePlayerComponent, PlayMode } from "angular-dotlottie";
@Component({
selector: "app-animation-controller",
template: `
<ng-dotlottie-player #player src="assets/animations/animation.lottie" [loop]="true"> </ng-dotlottie-player>
<div class="controls">
<button (click)="player.playAnimation()">Play</button>
<button (click)="player.pauseAnimation()">Pause</button>
<button (click)="player.stopAnimation()">Stop</button>
<button (click)="changeSpeed(2)">2x Speed</button>
<button (click)="player.takeSnapshot(true)">Download Snapshot</button>
</div>
`,
})
export class AnimationControllerComponent {
@ViewChild("player") player: DotLottiePlayerComponent;
changeSpeed(speed: number): void {
this.player.setSpeed(speed);
}
}
Dynamic Animation Loading
import { Component } from "@angular/core";
import { DotLottiePlayerComponent, PlayMode } from "angular-dotlottie";
@Component({
selector: "app-dynamic-animations",
template: `
<ng-dotlottie-player #player [loop]="true" [autoplay]="true"> </ng-dotlottie-player>
<div class="animation-selector">
<button (click)="loadAnimation('success')">Success</button>
<button (click)="loadAnimation('error')">Error</button>
<button (click)="loadAnimation('loading')">Loading</button>
</div>
`,
})
export class DynamicAnimationsComponent {
@ViewChild("player") player: DotLottiePlayerComponent;
animations = {
success: "assets/animations/success.lottie",
error: "assets/animations/error.lottie",
loading: "assets/animations/loading.lottie",
};
loadAnimation(animationKey: keyof typeof this.animations): void {
this.player.loadAnimation(this.animations[animationKey]);
}
}
Custom Animation Service
import { Injectable } from "@angular/core";
import { PlayMode } from "angular-dotlottie";
@Injectable({
providedIn: "root",
})
export class AnimationService {
private baseUrl = "assets/animations/";
getAnimationPath(name: string): string {
return `${this.baseUrl}${name}.lottie`;
}
getDefaultConfig(type: "indicator" | "feedback" | "illustration") {
switch (type) {
case "indicator":
return { loop: true, autoplay: true, mode: PlayMode.Forward };
case "feedback":
return { loop: false, autoplay: true, mode: PlayMode.Forward };
case "illustration":
return { loop: true, autoplay: false, mode: PlayMode.Forward };
}
}
}
Browser Support
Angular DotLottie supports all browsers that are supported by Angular and the DotLottie player component.
Troubleshooting
Animation Not Displaying
- Make sure your .lottie file path is correct
- Check that the container has a non-zero width and height
- Verify that the animation file is a valid .lottie format
Performance Issues
The component runs animations outside of Angular's zone for better performance. If you experience issues:
- Make sure you're not manually triggering change detection excessively
- Consider using simpler animations for mobile devices
- Set
[controls]="false"
to reduce DOM elements
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- DotLottie Web - The core player this wrapper is built upon
- LottieFiles - For creating the DotLottie format
Made with ❤️ for the Angular community
1.0.0
4 months ago