Particle Emitter
How do I make an particle emitter?
First attempt at making my own emitter. What is a particle emitter? How do you make a particle emitter? This script and codes were developed by Chris Hanson on 01 October 2022, Saturday.
Particle Emitter - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Particle Emitter</title> <script src="https://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <style> /* NOTE: The styles were added inline because Prefixfree needs access to your styles and they must be inlined if they are on local disk! */ html, body { height: 100%; margin: 0; padding: 0;
}
canvas { display: block;
} </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
</head>
<body> <canvas></canvas> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Particle Emitter - Script Codes CSS Codes
html, body { height: 100%; margin: 0; padding: 0;
}
canvas { display: block;
}
Particle Emitter - Script Codes JS Codes
(function() { 'use strict'; var canvas = document.querySelector('canvas'), ctx = canvas.getContext('2d'), W = canvas.width = window.innerWidth, H = canvas.height = window.innerHeight, particles = [], maxParticles = 2000, emitterSpeed = 1, // ms emitPerCycle = 20, emitAngle = 360, // deg maxSize = 3, // px minSize = 1, //px minVelocity = 5, // px maxVelocity = 10, // px alphaDecrease = 0.01; function Emitter(pos, speed) { var _this = this; this.position = pos || new Vector(W/2, H/2); this.speed = speed || emitterSpeed; window.setInterval(function() { for (var i = 0; i < emitPerCycle; i++) _this.emit(); }, this.speed); } Emitter.prototype = { constructor: Emitter, emit: function() { if (particles.length < maxParticles) { particles.push(new Particle( new Vector(this.position.x, this.position.y) )); } }, checkOutOfBounds: function() { var particlesInBound = []; for (var i=0, l=particles.length; i<l; i++) { var particle = particles[i], pos = particle.position; if (pos.x > W || pos.x < 0 || pos.y > H || pos.y < 0 || particle.alpha < 0) continue; particlesInBound.push(particle); } particles = particlesInBound; } }; function Vector(x, y) { this.x = x; this.y = y; } function Particle(position, velocity, size, angle, alpha) { this.position = position; this.velocity = velocity || Math.random() * maxVelocity + minVelocity; this.size = size || Math.random() * maxSize + minSize; this.angle = Math.random() * emitAngle / 180 * Math.PI; this.color = { r: Math.round(Math.random()) * 255, g: Math.round(Math.random()) * 255, b: Math.round(Math.random()) * 255, a: alpha || Math.random() + 0.5, toRGBA: function() { return this.r + ',' + this.g + ',' + this.b + ',' + this.a; } }; } function draw() { ctx.fillStyle = 'black'; ctx.fillRect(0,0,W,H); emitter.checkOutOfBounds(); for (var i=0, l=particles.length; i<l; i++) { var particle = particles[i]; drawParticle(particle); particle.position.x += particle.velocity * Math.sin(particle.angle); particle.position.y += particle.velocity * Math.cos(particle.angle); particle.color.a -= alphaDecrease; } window.requestAnimationFrame(draw); } function drawParticle(particle) { ctx.fillStyle = 'rgba(' + particle.color.toRGBA() + ')'; ctx.beginPath(); ctx.arc( particle.position.x, particle.position.y, particle.size * 2, Math.PI * 2, false ); ctx.fill(); } var emitter = new Emitter(); draw();
})();
Developer | Chris Hanson |
Username | chrishanson |
Uploaded | October 01, 2022 |
Rating | 3 |
Size | 3,105 Kb |
Views | 10,120 |
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 |
Nested rotating shapes | 2,646 Kb |
Fireworks | 3,514 Kb |
Custom branding | 9,737 Kb |
Rotating 3d cube | 2,957 Kb |
Hello World | 3,501 Kb |
Lf layout | 6,820 Kb |
Css spinner | 2,953 Kb |
Ui-select with tree navigation | 5,321 Kb |
Uiselect ie8 issues | 15,255 Kb |
Photo crop prototype | 19,337 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 |
Portfolio Page | KaylaMT | 1,983 Kb |
A Pen by Miro Olma | Programiro | 2,342 Kb |
CSS Grid Overlay | Cliffpyles | 3,090 Kb |
Base64 SVG Me | MrBambule | 44,786 Kb |
Plotting Points with D3.js | Laurakelly | 31,996 Kb |
Scifi-style Interative Form | Aaronchuo | 4,566 Kb |
Scarlett Johansson Tribute Page | Diomed | 3,233 Kb |
Nested flexbox layout for library catalog | Boycetrus | 3,271 Kb |
Opening Reveal Modal On Document Ready | Winghouchan | 1,787 Kb |
Border image | JohnRiordan | 2,120 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!