LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Create Confetti Explosion Background On Click Using Pure JavaScript

Create Confetti Explosion Background On Click Using Pure JavaScript

Create-Confetti-Explosion-Background-On-Click-Using-Pure-JavaScript
Confetti.js is a lightweight JavaScript plugin to create a confetti celebration explosion effect. It draws confetti graphics on the HTML canvas element. The plugin allows you to render the confetti effect on any JavaScript event, like click, hover, or resize. So, you can use this animation effect for various purposes on the webpage.

The confetti effect is useful to implement on the welcome, congratulations, or celebration webpage. Anyhow, you can also integrate it for various purposes like the background of the hero section.

Basically, there are no configurable options to customize the working of the plugin. But you can modify the function file to get the desired output. You can redefine colors, confetti size, spinning values, and the round of confetti, etc. Moreover, you have full control over the canvas element, you can style it according to your needs.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Table of Contents

Recommended For You:
Awesome And Simple Pure JavaScript MASONRY Grids

Features:

  1. Light Weight.
  2. Pure CSS/JavaScript.
  3. Cross Browser.
  4. No JQuery Files.
  5. Fully Customizable.
  6. Responsive.

How To Create Confetti Explosion Background On Click Using Pure JavaScript ?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

CSS:

<style type="text/css">
.confetti{
max-width: 640px;
display: block;
margin: 0 auto;
border: 1px solid #ddd;
user-select: none;
}
</style>

HTML:

<canvas class="confetti" id="canvas"></canvas>

JavaScript:

<script type="text/javascript">
//-----------Var Inits--------------
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
cx = ctx.canvas.width / 2;
cy = ctx.canvas.height / 2;

let confetti = [];
const confettiCount = 300;
const gravity = 0.5;
const terminalVelocity = 5;
const drag = 0.075;
const colors = [
{ front: 'red', back: 'darkred' },
{ front: 'green', back: 'darkgreen' },
{ front: 'blue', back: 'darkblue' },
{ front: 'yellow', back: 'darkyellow' },
{ front: 'orange', back: 'darkorange' },
{ front: 'pink', back: 'darkpink' },
{ front: 'purple', back: 'darkpurple' },
{ front: 'turquoise', back: 'darkturquoise' }];


//-----------Functions--------------
resizeCanvas = () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
cx = ctx.canvas.width / 2;
cy = ctx.canvas.height / 2;
};

randomRange = (min, max) => Math.random() * (max - min) + min;

initConfetti = () => {
for (let i = 0; i < confettiCount; i++) {
confetti.push({
color: colors[Math.floor(randomRange(0, colors.length))],
dimensions: {
x: randomRange(10, 20),
y: randomRange(10, 30) },

position: {
x: randomRange(0, canvas.width),
y: canvas.height - 1 },

rotation: randomRange(0, 2 * Math.PI),
scale: {
x: 1,
y: 1 },

velocity: {
x: randomRange(-25, 25),
y: randomRange(0, -50) } });


}
};

//---------Render-----------
render = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);

confetti.forEach((confetto, index) => {
let width = confetto.dimensions.x * confetto.scale.x;
let height = confetto.dimensions.y * confetto.scale.y;

// Move canvas to position and rotate
ctx.translate(confetto.position.x, confetto.position.y);
ctx.rotate(confetto.rotation);

// Apply forces to velocity
confetto.velocity.x -= confetto.velocity.x * drag;
confetto.velocity.y = Math.min(confetto.velocity.y + gravity, terminalVelocity);
confetto.velocity.x += Math.random() > 0.5 ? Math.random() : -Math.random();

// Set position
confetto.position.x += confetto.velocity.x;
confetto.position.y += confetto.velocity.y;

// Delete confetti when out of frame
if (confetto.position.y >= canvas.height) confetti.splice(index, 1);

// Loop confetto x position
if (confetto.position.x > canvas.width) confetto.position.x = 0;
if (confetto.position.x < 0) confetto.position.x = canvas.width;

// Spin confetto by scaling y
confetto.scale.y = Math.cos(confetto.position.y * 0.1);
ctx.fillStyle = confetto.scale.y > 0 ? confetto.color.front : confetto.color.back;

// Draw confetti
ctx.fillRect(-width / 2, -height / 2, width, height);

// Reset transform matrix
ctx.setTransform(1, 0, 0, 1, 0, 0);
});

// Fire off another round of confetti
if (confetti.length <= 10) initConfetti();

window.requestAnimationFrame(render);
};

//---------Execution--------
initConfetti();
render();

//----------Resize----------
window.addEventListener('resize', function () {
resizeCanvas();
});

//------------Click------------
window.addEventListener('click', function () {
initConfetti();
});
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Recommended For You:
How To Keep Scrolling A DIV Background Image Using JavaScript?

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

2 Responses to “Create Confetti Explosion Background On Click Using Pure JavaScript”

  1. OmneelabWMS says:

    Great article! You have provided clear and concise information on the topic. The step-by-step breakdown of confetti explosion background process was particularly helpful in understanding the logistics involved. Thank you for sharing this valuable information.

    • EXEIdeas says:

      Welcome here and thanks for reading our article and sharing your view. This will be very helpful to us to let us motivate to provide you with more awesome and valuable content from a different mind. Thanks again. Please contact us via the contact form for more info.

Leave a Reply

Your email address will not be published. Required fields are marked *