Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
html,body { margin:0; padding:0; height:100%; } body { background-color:#000; display:flex; align-items:center; justify-content:center; overflow:hidden; } canvas { flex-shrink:0; image-rendering:-moz-crisp-edges; image-rendering:-webkit-crisp-edges; image-rendering:pixelated; object-fit:contain; }
JavaScript
const PI = Math.PI, TWO_PI = Math.PI * 2; Util = {}; Util.timeStamp = function() { return window.performance.now(); }; Util.random = function(min, max) { return min + Math.random() * (max - min); }; Util.map = function(a, b, c, d, e) { return (a - b) / (c - b) * (e - d) + d; }; Util.lerp = function(value1, value2, amount) { return value1 + (value2 - value1) * amount; }; Util.array2D = function(tableau, array_width){ var result = []; for (var i = 0; i < tableau.length; i += array_width) result.push(tableau.slice(i, i + array_width)); return result; }; Util.threeAngle = function(p0,p1,p2){ var b = Math.pow(p1.x-p0.x,2) + Math.pow(p1.y-p0.y,2), a = Math.pow(p1.x-p2.x,2) + Math.pow(p1.y-p2.y,2), c = Math.pow(p2.x-p0.x,2) + Math.pow(p2.y-p0.y,2); return Math.acos( (a+b-c) / Math.sqrt(4*a*b) ); } Tween = {}; Tween.linear = function(currentTime, start, degreeOfChange, duration) { return degreeOfChange * currentTime / duration + start; }; Tween.easeInOutQuad = function(t, b, c, d) { t /= d / 2; if (t < 1) return c / 2 * t * t + b; t--; return -c / 2 * (t * (t - 2) - 1) + b; }; Tween.easeInOutExpo = function(t, b, c, d) { t /= d / 2; if (t < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b; t--; return c / 2 * (-Math.pow(2, -10 * t) + 2) + b; }; class Vector{ constructor(x,y){ this.x = x || 0; this.y = y || 0; } set(x,y){ this.x = x; this.y = y; } reset(){ this.x = 0; this.y = 0; } fromAngle(angle){ let x = Math.cos(angle), y = Math.sin(angle); return new Vector(x,y); } add(vector){ this.x += vector.x; this.y += vector.y; } sub(vector){ this.x -= vector.x; this.y -= vector.y; } mult(scalar){ this.x *= scalar; this.y *= scalar; } div(scalar){ this.x /= scalar; this.y /= scalar; } dot(vector){ return vector.x * this.x + vector.y * this.y; } limit(limit_value){ if(this.mag() > limit_value) this.setMag(limit_value); } mag(){ return Math.hypot(this.x,this.y); } setMag(new_mag){ if(this.mag() > 0){ this.normalize(); }else{ this.x = 1; this.y = 0; } this.mult(new_mag); } normalize(){ let mag = this.mag(); if(mag > 0){ this.x /= mag; this.y /= mag; } } heading(){ return Math.atan2(this.x,this.y); } setHeading(angle){ let mag = this.mag(); this.x = Math.cos(angle) * mag; this.y = Math.sin(angle) * mag; } dist(vector){ return new Vector(this.x - vector.x,this.y - vector.y).mag(); } angle(vector){ return Math.atan2(vector.y - this.y, vector.x - this.x); } copy(){ return new Vector(this.x,this.y); } } // init canvas let canvas = document.createElement("canvas"), ctx = canvas.getContext("2d"), H = (canvas.height = 600), W = (canvas.width = 600); document.body.appendChild(canvas); //document.body.addEventListener("mousemove", event => mousemove(event), false); let cursor = new Vector(W/2,H/2); function mousemove(event) { cursor.x = event.pageX - canvas.offsetLeft; cursor.y = event.pageY - canvas.offsetTop; } class Point { constructor(x, y) { this.position = new Vector(x, y); this.acceleration = new Vector(0, 0).fromAngle(Util.random(0,TWO_PI)); this.velocity = new Vector(0, 0); this.radius = Util.random(6,30); this.zone = this.radius + 40; this.c = Util.random(0,360); this.min_radius = 0.2; this.max_radius = this.radius; } display() { let color = "hsl("+ Util.map(this.velocity.mag(), 2, 0, 0, 360)+",70%,"+Util.map(this.radius, this.min_radius, this.max_radius, 100, 20)+"%)"; ctx.globalAlpha = Util.map(this.radius, this.min_radius, this.max_radius, 1, 0); ctx.fillStyle = color; ctx.beginPath(); ctx.arc(this.position.x, this.position.y, this.radius, 0, 2 * Math.PI); ctx.fill(); } integrate() { let force = new Vector(0,0).fromAngle(this.position.angle(cursor)); force.setMag(0.1); this.addForce(force); this.radius = Util.map(this.velocity.mag(), 0, 2, this.min_radius, this.max_radius); this.velocity.add(this.acceleration); //this.velocity.mult(0.98); this.velocity.limit(2); this.position.add(this.velocity); this.acceleration.reset(); } addForce(force) { this.acceleration.add(force); } detection(others) { for (let i = 0; i < others.length; i++) { let other = others[i]; if (this === other) continue; let dist = this.position.dist(other.position), max_dist = this.zone + other.radius; if ( max_dist - dist >= 0) { let angle = other.position.angle(this.position); let force = new Vector().fromAngle(angle); force.setMag(Util.map(dist, 0, max_dist, 0.2, 0)); this.addForce(force); } } } bound() { let dist = this.position.dist(cursor); if (dist > W/3) { let force = new Vector(0,0).fromAngle(this.position.angle(cursor)); force.setMag(Util.map(dist,0,W/3,0,0.02)); this.addForce(force); } } } let points = []; for (let i = 0; i < 80; i++) { points.push(new Point(cursor.x,cursor.y)); } function loop() { ctx.globalCompositeOperation = "source-over"; ctx.globalAlpha = 0.16; ctx.fillStyle= "black"; ctx.fillRect(0, 0, W, H); ctx.globalAlpha = 1; ctx.globalCompositeOperation = "lighter"; points.forEach(point => { point.detection(points); }); points.forEach(point => { point.display(); //point.bound(); point.integrate(); }); requestAnimationFrame(loop); } loop();
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>粒子动画-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号