diff --git a/README.md b/README.md new file mode 100644 index 0000000..0d7b3d1 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Confetti Animation + +A simple confetti animation in JavaScript, drawn on the HTML5 canvas. + +[Video Guide](https://youtu.be/GiA6ls9mOL4) diff --git a/confetti.js b/confetti.js new file mode 100644 index 0000000..838fe39 --- /dev/null +++ b/confetti.js @@ -0,0 +1,76 @@ +let canvas = document.getElementById('confetti'); + +canvas.width = 640; +canvas.height = 480; + +let ctx = canvas.getContext('2d'); +let pieces = []; +let numberOfPieces = 50; +let lastUpdateTime = Date.now(); + +function randomColor () { + let colors = ['#f00', '#0f0', '#00f', '#0ff', '#f0f', '#ff0']; + return colors[Math.floor(Math.random() * colors.length)]; +} + +function update () { + let now = Date.now(), + dt = now - lastUpdateTime; + + for (let i = pieces.length - 1; i >= 0; i--) { + let p = pieces[i]; + + if (p.y > canvas.height) { + pieces.splice(i, 1); + continue; + } + + p.y += p.gravity * dt; + p.rotation += p.rotationSpeed * dt; + } + + + while (pieces.length < numberOfPieces) { + pieces.push(new Piece(Math.random() * canvas.width, -20)); + } + + lastUpdateTime = now; + + setTimeout(update, 1); +} + +function draw () { + ctx.clearRect(0, 0, canvas.width, canvas.height); + + pieces.forEach(function (p) { + ctx.save(); + + ctx.fillStyle = p.color; + + ctx.translate(p.x + p.size / 2, p.y + p.size / 2); + ctx.rotate(p.rotation); + + ctx.fillRect(-p.size / 2, -p.size / 2, p.size, p.size); + + ctx.restore(); + }); + + requestAnimationFrame(draw); +} + +function Piece (x, y) { + this.x = x; + this.y = y; + this.size = (Math.random() * 0.5 + 0.75) * 15; + this.gravity = (Math.random() * 0.5 + 0.75) * 0.1; + this.rotation = (Math.PI * 2) * Math.random(); + this.rotationSpeed = (Math.PI * 2) * (Math.random() - 0.5) * 0.001; + this.color = randomColor(); +} + +while (pieces.length < numberOfPieces) { + pieces.push(new Piece(Math.random() * canvas.width, Math.random() * canvas.height)); +} + +update(); +draw(); diff --git a/index.html b/index.html new file mode 100644 index 0000000..75ee452 --- /dev/null +++ b/index.html @@ -0,0 +1,12 @@ + + +
+ +