A lightweight and fully vanilla (no libraries) confetti animation project built with HTML, CSS, and JavaScript to be easy to integrate into any project.
-
Create a
canvasin your HTML file.<canvas id="vanillaConfettiCanvas"></canvas>
-
Specify the
type="module"in your HTML file to enable module importation.<script src="./main.js" type="module"></script>
-
Clone the
vanillaConfetti.min.jsfile of this repository in your project. -
Import the
vanillaConfettiscript like that at the top of your JS file. (Specify the path to thevanillaConfetti.min.jsfile)import { generateConfetti } from "../vanillaConfetti.min.js";
-
Now you can simply call the
generateConfettifunction.generateConfetti(confettiConfigObj, canvasId);
The animation can be customized by modifying the confettiConfigObj in the JavaScript.
Example:
const confettiConfigObj = {
colorsArray: ["rgba(255, 180, 185, 1)", "rgba(255, 220, 185, 1)", "rgba(255, 255, 185, 1)", "rgba(185, 255, 200, 1)", "rgba(185, 225, 255, 1)", "rgba(215, 185, 255, 1)"],
velocity: 0.025,
quantity: 750,
minSize: 4,
maxSize: 12,
minOpacity: 0.75,
maxOpacity: 1,
infiniteLoop: false
};- Type: Array
- Description: Controls the colors of the confetti. Each color is randomly chosen from the array for every piece of confetti.
- Type: Number
- Description: Controls the speed at which confetti falls. A higher value makes the confetti fall faster, a lower value makes it fall slower.
- Type: Number
- Description: The number of confetti pieces to be generated.
- Type: Number
- Description: The minimum size of each confetti piece.
- Type: Number
- Description:The maximum size of each confetti piece.
- Type: Number
- Description: Controls how transparent the confetti pieces can be. A lower value makes confetti more transparent.
- Type: Number
- Description: Controls how opaque the confetti pieces can be. A higher value makes the confetti pieces more solid.
- Type: Boolean
- Description: Whether the confetti should continuously fall (infinite loop).
You can also specify the canvas id, by default, it is vanillaConfettiCanvas.
generateConfetti(confettiConfigObj, "yourCanvasId");