Using canvas confetti in angular

Abhinav Akhil
3 min readJan 13, 2024

--

Are you tired of your Angular applications looking bland and static? Do you want to add a touch of excitement and celebration to your user experience? Look no further! In this blog post, we’ll dive deep into the world of canvas confetti and show you how to integrate it seamlessly into your Angular projects. Get ready to make your applications pop with vibrant colors and lively animations!

canvas-confetti

Chapter 1: Understanding Canvas Confetti

Before we jump into the technicalities, let’s understand what canvas confetti is and why it’s a fantastic addition to your Angular arsenal. Canvas confetti is a JavaScript library that creates a festive atmosphere by simulating the falling of confetti on your web page. It utilizes the HTML5 canvas element to render dynamic and animated effects, providing a visually stunning user experience.

Chapter 2: Setting up Your Angular Project

Now, let’s get our hands dirty and set up an Angular project to incorporate canvas confetti. If you don’t have Angular CLI installed, run the following command:

npm install -g @angular/cli

Next, create a new Angular project using the CLI:

ng new my-confetti-app
cd my-confetti-app

Install the canvas-confetti library:

npm install canvas-confetti

Chapter 3: Integrating Canvas Confetti into Angular Components

With the project set up, it’s time to start using canvas confetti in your Angular components. Let’s create a simple example to celebrate a button click.

  1. Open your app.component.html file and add a button:
<button (click)="celebrate()">Celebrate!</button>

2. In your app.component.ts file, import the canvas-confetti library:

import * as confetti from 'canvas-confetti';

3. Define a method in your component to trigger the confetti effect:

celebrate() {
const duration = 3000; // in milliseconds

confetti({
particleCount: 100,
spread: 160,
origin: { y: 0.6 },
});

// Clear confetti after a certain duration
setTimeout(() => confetti.reset(), duration);
}

Chapter 4: Customizing Canvas Confetti

Canvas confetti provides a range of options for customization. You can control the number of particles, their colors, and the spread of the confetti. Experiment with these options to create the perfect celebratory effect for your application.

celebrate() {
const duration = 3000;

confetti({
particleCount: 150,
spread: 180,
origin: { y: 0.6 },
colors: ['#FF4500', '#008080', '#FFD700'],
});

setTimeout(() => confetti.reset(), duration);
}

Chapter 5: Advanced Integration — Using Angular Services

To take your confetti game to the next level, consider creating an Angular service to handle confetti animations globally. This allows you to trigger confetti from any component in your application.

  1. Generate a service using the CLI:
ng generate service confetti

2. Implement the confetti service:

// confetti.service.ts
import { Injectable } from '@angular/core';
import * as confetti from 'canvas-confetti';

@Injectable({
providedIn: 'root',
})
export class ConfettiService {
celebrate() {
const duration = 3000;

confetti({
particleCount: 150,
spread: 180,
origin: { y: 0.6 },
colors: ['#FF4500', '#008080', '#FFD700'],
});

setTimeout(() => confetti.reset(), duration);
}
}

3. Inject and use the service in your components:

// app.component.ts
import { Component } from '@angular/core';
import { ConfettiService } from './confetti.service';

@Component({
selector: 'app-root',
template: `
<button (click)="confettiService.celebrate()">Celebrate!</button>
`,
})
export class AppComponent {
constructor(public confettiService: ConfettiService) {}
}

Conclusion:

Congratulations! You’ve successfully integrated canvas confetti into your Angular project. With these techniques, you can add a festive touch to various user interactions, making your applications more engaging and enjoyable. Experiment with different configurations, explore additional features and let your creativity run wild. Happy coding, and may your applications be filled with confetti-filled celebrations!

Reference:

https://www.npmjs.com/package/canvas-confetti

--

--

Abhinav Akhil
Abhinav Akhil

Written by Abhinav Akhil

I create software, that tell stories.

Responses (2)