Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
body, html { position: absolute; margin: 0; padding: 0; width: 100%; height: 100%; overflow: hidden; } canvas { position: absolute; width: 100%; height: 100%; background: #000; cursor: pointer; }
JavaScript
/* Adapted from Tom Carden, May 2004 a simple particle system with naive gravitational attraction https://processing.org/exhibition/works/metropop/ */ { // a point in space with a velocity class Particle { constructor() { // changing these parameters can give very different results this.damp = 0.00002; // remember a very small amount of the last direction this.accel = 100; // move very quickly this.init(); } init() { this.x = Math.random() * canvas.width; this.y = Math.random() * canvas.height; this.vx = this.accel * (Math.random() - Math.random()); this.vy = this.accel * (Math.random() - Math.random()); } step() { // move towards every attractor // at a speed inversely proportional to distance squared // (much slower when further away, very fast when close) for (const a of attractors) { // calculate the square of the distance // from this particle to the current attractor const dx = a.x - this.x; const dy = a.y - this.y; const d2 = dx * dx + dy * dy; if (d2 > 0.1) { // make sure we don't divide by zero // accelerate towards each attractor this.vx += this.accel * dx / d2; this.vy += this.accel * dy / d2; } } // move by the velocity this.x += this.vx; this.y += this.vy; // scale the velocity back for the next frame this.vx *= this.damp; this.vy *= this.damp; // draw particle ctx.fillRect(this.x, this.y, 0.5, 0.5); } } // init canvas const canvas = { init() { this.frame = 0; this.elem = document.createElement("canvas"); document.body.appendChild(this.elem); this.resize(); // reset on mouse click window.addEventListener("click", e => this.reset(), false); this.elem.addEventListener("touchstart", e => this.reset(), false); return this.elem.getContext("2d"); }, resize () { this.width = this.elem.width = this.elem.offsetWidth; this.height = this.elem.height = this.elem.offsetHeight; }, reset() { ctx.globalCompositeOperation = "source-over"; this.resize(); ctx.fillStyle = "#321"; ctx.globalCompositeOperation = "lighter"; for (const p of particles) p.init(); for (const a of attractors) a.init(); this.frame = 0; } }; // init pen const ctx = canvas.init(); const attractors = Array.from({ length: 8 }, () => new Particle()); const particles = Array.from({ length: 1000 }, () => new Particle()); canvas.reset(); // move and draw particles const run = () => { requestAnimationFrame(run); if (canvas.frame++ < 1000) { for (const p of particles) p.step(); } }; run(); }
粒子
时间
文字
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号