Particle Emitter

Developer
Size
3,105 Kb
Views
10,120

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 Previews

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();
})();
Particle Emitter - Script Codes
Particle Emitter - Script Codes
Home Page Home
Developer Chris Hanson
Username chrishanson
Uploaded October 01, 2022
Rating 3
Size 3,105 Kb
Views 10,120
Do you need developer help for Particle Emitter?

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!

Chris Hanson (chrishanson) Script Codes
Create amazing art & images with AI!

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!