Fireworks fountain
How do I make an fireworks fountain?
What is a fireworks fountain? How do you make a fireworks fountain? This script and codes were developed by Max Huang on 26 December 2022, Monday.
Fireworks fountain - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Fireworks fountain</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <canvas id="canvas"></canvas>
<script src="//cdnjs.cloudflare.com/ajax/libs/stats.js/r11/Stats.js" type="text/javascript"></script>
<div class="title">move your mouse on</div> <script src="js/index.js"></script>
</body>
</html>
Fireworks fountain - Script Codes CSS Codes
html, body { margin: 0px; }
.title { color: #FFF; position: absolute; top: 8px; left: 12px; font-family: verdana; text-transform: uppercase; font-size: 10px; font-weight: bold; } canvas { display: block; background: #111; }
Fireworks fountain - Script Codes JS Codes
(function (window) { "use strict"; var Particle, p; Particle = function (x, y, speed, direction) { this.initialize(x, y, speed, direction); }; p = Particle.prototype; p.initialize = function (x, y, speed, direction) { this.position = new Vector(x, y); this.velocity = new Vector(0, 0); this.velocity.set(speed, direction); this.gravity = new Vector(0, 0); this.friction = 1; return this; }; p.setGravity = function (gravity) { this.gravity = new Vector(0, gravity); }; p.setFriction = function (friction) { this.friction = friction; }; p.accelerate = function (accel) { this.velocity.add(accel); }; p.update = function () { // add gravity this.velocity.add(this.gravity); // multiply friction this.velocity.multiply(this.friction); // position this.position.add(this.velocity); }; window.Particle = Particle;
}(window));
(function (window) { "use strict"; var Vector, p; Vector = function (x, y) { this.initialize(x, y); }; p = Vector.prototype; p.initialize = function (x, y) { this.x = x || 0; this.y = y || 0; return this; }; p.set = function (length, angle) { this.x = length * Math.cos(angle); this.y = length * Math.sin(angle); }; p.set2 = function (x, y) { this.x = x; this.y = y; }; p.getAngle = function () { return Math.atan2(this.y, this.x); }; p.getLength = function () { return Math.sqrt(this.x * this.x + this.y * this.y); }; p.add = function (vector) { this.x += vector.x; this.y += vector.y; }; p.multiply = function (value) { this.x *= value; this.y *= value; }; window.Vector = Vector;
}(window));
var canvas = document.getElementById("canvas"), context = canvas.getContext("2d"), width = canvas.width = window.innerWidth, height = canvas.height = window.innerHeight, particles = [], fireforks = [], mouseX = 0, mouseY = 0, size = 3, hue = 0, count = 0, dense = 2;
var stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms, 2: mb
// align top-left
stats.domElement.style.position = 'absolute';
stats.domElement.style.right = '0px';
stats.domElement.style.top = '0px';
document.body.appendChild(stats.domElement);
document.body.addEventListener("mousemove", function (event) { mouseX = event.clientX; mouseY = event.clientY;
});
function createParticle() { var p = new Particle(width / 2, height, 10 + 5 * Math.random(), (1.4 + Math.random() * 0.2) * Math.PI); p.size = size; p.hue = hue; p.lightness = 40 + 40 * Math.random(); p.setGravity(0.1); p.setFriction(0.99); particles.push(p);
}
function createFirefork(x, y, _size, _hue) { var p = new Particle(x, y, 6 + 4 * Math.random(), Math.random() * 2 * Math.PI); p.size = _size / 2; p.hue = _hue; p.lightness = 40 + 40 * Math.random(); p.setGravity(0.1); p.setFriction(0.95); fireforks.push(p);
}
window.setInterval(function () { var i; if (count % 2 === 0) { for (i = 1; i <= dense; i++) { createParticle() } } if (count % 3 === 0) { for (i = 1; i <= dense; i++) { createParticle() } } if (count % 5 === 0) { for (i = 1; i <= dense; i++) { createParticle() } } count++;
}, 200);
update();
function update() { stats.begin(); context.clearRect(0, 0, canvas.width, canvas.height); var i, p, length, j, f, reducedParticles = [], reducedFireworks = []; hue += 0.5; for (i = 0, length = particles.length; i < length; i++) { p = particles[i]; p.update(); if (p.position.x > 0 && p.position.x < width && p.position.y < height && !p.explode) { reducedParticles.push(p); } } particles = reducedParticles; for (i = 0, length = particles.length; i < length; i++) { p = particles[i]; p.size += 0.015; p.lightness -= 0.3; p.hue += 1; if (Math.random() < 0.001 || Math.sqrt(Math.pow((mouseX - p.position.x), 2) + Math.pow(mouseY - p.position.y, 2)) < 3 * p.size) { p.explode = true; for (j = 0; j < 15; j++) { createFirefork(p.position.x, p.position.y, p.size, p.hue); } } context.beginPath(); context.arc(p.position.x, p.position.y, p.size, 0, Math.PI * 2, false); context.fillStyle = 'hsl(' + p.hue + ',100%, ' + p.lightness + '%)'; context.fill(); } for (i = 0; i < fireforks.length; i++) { f = fireforks[i]; if (f.lightness > 0) { reducedFireworks.push(f); } } fireforks = reducedFireworks; for (i = 0; i < fireforks.length; i++) { f = fireforks[i]; f.update(); f.lightness -= 0.5; context.beginPath(); context.arc(f.position.x, f.position.y, f.size, 0, Math.PI * 2, false); context.fillStyle = 'hsl(' + f.hue + ',100%, ' + f.lightness + '%)'; context.fill(); } stats.end(); requestAnimationFrame(update);
}
Developer | Max Huang |
Username | nosir |
Uploaded | December 26, 2022 |
Rating | 4 |
Size | 3,155 Kb |
Views | 22,264 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Pixel Isometric Flappy Bird | 3,836 Kb |
Lissajous Particles | 3,201 Kb |
Build Monument Valley by Obelisk.js | 3,775 Kb |
Pixel Isometric Cube Generator | 2,195 Kb |
Obelisk Test Case | 3,253 Kb |
Segments mouse following | 2,909 Kb |
Obelisk Test Case - Depth Sorting | 2,707 Kb |
Pixel Isometric Input Text Rendering | 3,248 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Haml Calendar | Katydecorah | 5,643 Kb |
Simple CSS loader. | Cabrera | 2,574 Kb |
Slider for stackoverflow | KKOKU | 3,222 Kb |
Resize image | Happyhj | 1,892 Kb |
Prism | Pyrografix | 2,843 Kb |
Slide In Panel | Vikvarg | 2,811 Kb |
CSS3 Form Page Design | Rssatnam | 3,613 Kb |
Formations | Cantelope | 5,731 Kb |
RRC wrapSwitch | Pshrmn | 2,922 Kb |
Css3 loader | Clknap | 2,391 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!