Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
body,html { margin:0; padding:0; } canvas { background-color:black; }
JavaScript
"use strict"; var maxParticles = 20000, particleSize = 1, emissionRate = 20, objectSize = 3; // drawSize of emitter/field var canvas = document.querySelector('canvas'); var ctx = canvas.getContext('2d'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; function Particle(point, velocity, acceleration) { this.position = point || new Vector(0, 0); this.velocity = velocity || new Vector(0, 0); this.acceleration = acceleration || new Vector(0, 0); } Particle.prototype.submitToFields = function(fields) { // our starting acceleration this frame var totalAccelerationX = 0; var totalAccelerationY = 0; // for each passed field for (var i = 0; i < fields.length; i++) { var field = fields[i]; // find the distance between the particle and the field var vectorX = field.position.x - this.position.x; var vectorY = field.position.y - this.position.y; // calculate the force via MAGIC and HIGH SCHOOL SCIENCE! var force = field.mass / Math.pow(vectorX * vectorX + vectorY * vectorY, 1.5); // add to the total acceleration the force adjusted by distance totalAccelerationX += vectorX * force; totalAccelerationY += vectorY * force; } // update our particle's acceleration this.acceleration = new Vector(totalAccelerationX, totalAccelerationY); }; Particle.prototype.move = function() { this.velocity.add(this.acceleration); this.position.add(this.velocity); }; function Field(point, mass) { this.position = point; this.setMass(mass); } Field.prototype.setMass = function(mass) { this.mass = mass || 100; this.drawColor = mass < 0 ? "#f00" : "#0f0"; } function Vector(x, y) { this.x = x || 0; this.y = y || 0; } Vector.prototype.add = function(vector) { this.x += vector.x; this.y += vector.y; } Vector.prototype.getMagnitude = function() { return Math.sqrt(this.x * this.x + this.y * this.y); }; Vector.prototype.getAngle = function() { return Math.atan2(this.y, this.x); }; Vector.fromAngle = function(angle, magnitude) { return new Vector(magnitude * Math.cos(angle), magnitude * Math.sin(angle)); }; function Emitter(point, velocity, spread) { this.position = point; // Vector this.velocity = velocity; // Vector this.spread = spread || Math.PI / 32; // possible angles = velocity +/- spread this.drawColor = "#999"; // So we can tell them apart from Fields later } Emitter.prototype.emitParticle = function() { // Use an angle randomized over the spread so we have more of a "spray" var angle = this.velocity.getAngle() + this.spread - (Math.random() * this.spread * 2); // The magnitude of the emitter's velocity var magnitude = this.velocity.getMagnitude(); // The emitter's position var position = new Vector(this.position.x, this.position.y); // New velocity based off of the calculated angle and magnitude var velocity = Vector.fromAngle(angle, magnitude); // return our new Particle! return new Particle(position, velocity); }; function addNewParticles() { // if we're at our max, stop emitting. if (particles.length > maxParticles) return; // for each emitter for (var i = 0; i < emitters.length; i++) { // emit [emissionRate] particles and store them in our particles array for (var j = 0; j < emissionRate; j++) { particles.push(emitters[i].emitParticle()); } } } function plotParticles(boundsX, boundsY) { // a new array to hold particles within our bounds var currentParticles = []; for (var i = 0; i < particles.length; i++) { var particle = particles[i]; var pos = particle.position; // If we're out of bounds, drop this particle and move on to the next if (pos.x < 0 || pos.x > boundsX || pos.y < 0 || pos.y > boundsY) continue; // Update velocities and accelerations to account for the fields particle.submitToFields(fields); // Move our particles particle.move(); // Add this particle to the list of current particles currentParticles.push(particle); } // Update our global particles reference particles = currentParticles; } function drawParticles() { ctx.fillStyle = 'rgb(0,0,255)'; for (var i = 0; i < particles.length; i++) { var position = particles[i].position; ctx.fillRect(position.x, position.y, particleSize, particleSize); } } function drawCircle(object) { ctx.fillStyle = object.drawColor; ctx.beginPath(); ctx.arc(object.position.x, object.position.y, objectSize, 0, Math.PI * 2); ctx.closePath(); ctx.fill(); } var particles = []; var midX = canvas.width / 2; var midY = canvas.height / 2; // Add one emitter located at `{ x : 100, y : 230}` from the origin (top left) // that emits at a velocity of `2` shooting out from the right (angle `0`) var emitters = [new Emitter(new Vector(midX - 150, midY), Vector.fromAngle(0, 2))]; // Add one field located at `{ x : 400, y : 230}` (to the right of our emitter) // that repels with a force of `140` var fields = [new Field(new Vector(midX + 150, midY), -140)]; function loop() { clear(); update(); draw(); queue(); } function clear() { ctx.clearRect(0, 0, canvas.width, canvas.height); } function update() { addNewParticles(); plotParticles(canvas.width, canvas.height); } function draw() { drawParticles(); fields.forEach(drawCircle); emitters.forEach(drawCircle); } function queue() { window.requestAnimationFrame(loop); } loop();
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>JavaScript粒子系统-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号