Confetti
html5 confetti
by vovkasolovev
HTML
<div id="content">
<div class="buttonContainer">
<button id="stopButton">Stop Confetti</button>
<button id="startButton">Drop Confetti</button>
<button id="someButton">Some Confetti</button>
</div>
</div>
<canvas id="canvas"></canvas>
CSS
* {
margin: 0;
padding: 0;
}
body {
/*You can use any kind of background here.*/
background: #11bb88;
}
canvas {
display: block;
position: relative;
z-index: 1;
pointer-events: none;
}
#content {
position: absolute;
}
JavaScript
(function () {
// globals
var canvas;
var ctx;
var W;
var H;
var mp = 150; //max particles
var particles = [];
var particlesCache = [];
var angle = 0;
var tiltAngle = 0;
var confettiActive = true;
var animationComplete = true;
var deactivationTimerHandler;
var reactivationTimerHandler;
var animationHandler;
// objects
var particleColors = {
colorOptions: ["#f0f0f0","#017","#07f","#5b0","#fc0","#d07","#81b",],
colorIndex: 0,
colorIncrementer: 0,
colorThreshold: 10,
getColor: function () {
if (this.colorIncrementer >= 10) {
this.colorIncrementer = 0;
this.colorIndex++;
if (this.colorIndex >= this.colorOptions.length) {
this.colorIndex = 0;
}
}
this.colorIncrementer++;
return this.colorOptions[this.colorIndex];
}
}
function confettiParticle(color) {
this.x = Math.random() * W; // x-coordinate
this.y = (Math.random() * H) - H; //y-coordinate
this.r = RandomFromTo(3, 10); //radius;
this.d = (Math.random() * mp) + 10; //density;
this.color = color;
this.tilt = Math.floor(Math.random() * 100) - 10;
this.tiltAngleIncremental = (Math.random() * 0.07) + .05;
this.tiltAngle = 200;
this.draw = function () {
ctx.beginPath();
ctx.lineWidth = this.r;
ctx.strokeStyle = this.color;
ctx.moveTo(this.x + this.tilt + (this.r*3), this.y);
ctx.lineTo(this.x + this.tilt, this.y + this.tilt + (this.r ));
return ctx.stroke();
}
}
$(document).ready(function () {
SetGlobals();
InitializeButton();
InitializeConfetti();
$(window).resize(function () {
W = window.innerWidth;
H = window.innerHeight;
canvas.width =...