Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
*, *:before, *:after { box-sizing: border-box; position: relative; } body, html { height: 100%; width: 100%; margin: 0; padding: 0; background: black; } canvas { display: block; width: 100%; height: 100%; } .instructions { position: absolute; top: 0; left: 0; padding: 0.5em; margin: 0.5em; background: rgba(0, 0, 0, 0.3); color: #FFF; font-size: 10px; font-family: monospace; }
JavaScript
console.clear(); /* Play with these values! */ const PARTICLE_COUNT = 100; const SAFE_DISTANCE = 130; const INFECTED_DISTANCE = 15; const INFECTION_RATE = 0.25; const RECOVERY_TIME = 14000; const STAY_AT_HOME = 0.1; /* ---------------------------------- */ let particles = []; const STATUSES = { HEALTHY: "HEALTHY", INFECTED: "INFECTED", RECOVERED: "RECOVERED" }; const elBody = document.body; const elCanvas = document.querySelector("#canvas"); const ctx = elCanvas.getContext("2d"); let width, height; function resize() { width = elCanvas.width = elBody.clientWidth; height = elCanvas.height = elBody.clientHeight; } resize(); window.addEventListener("resize", resize); /* ---------------------------------- */ class Particle { constructor() { this.x = Math.random() * width; this.y = Math.random() * height; this.radius = 3; this.color = "white"; this.speed = Math.random() < STAY_AT_HOME ? 0 : 1; this.directionAngle = Math.floor(Math.random() * 360); this.vector = { x: Math.cos(this.directionAngle) * this.speed, y: Math.sin(this.directionAngle) * this.speed }; this.status = STATUSES.HEALTHY; if (Math.random() < INFECTION_RATE) { this.infect(); } } infect() { if ( this.status === STATUSES.INFECTED || this.status === STATUSES.RECOVERED ) { return; } this.color = "green"; this.status = STATUSES.INFECTED; setTimeout(() => { this.recover(); }, RECOVERY_TIME); } recover() { this.status = STATUSES.RECOVERED; this.color = "hotpink"; } draw(drawCtx) { drawCtx.beginPath(); drawCtx.arc(this.x, this.y, this.radius, 0, Math.PI * 2); drawCtx.closePath(); drawCtx.fillStyle = this.color; drawCtx.fill(); } update() { this.checkBoundaries(); this.x += this.vector.x; this.y += this.vector.y; } checkBoundaries() { if (this.x > width || this.x < 0) { this.vector.x *= -1; /* Ensure the dots are pushed inside */ this.x = Math.max(0, Math.min(width, this.x)); } if (this.y > height || this.y < 0) { this.vector.y *= -1; /* Ensure the dots are pushed inside */ this.y = Math.max(0, Math.min(height, this.y)); } } } /* ---------------------------------- */ function distance(x1, y1, x2, y2) { var dx = x1 - x2; var dy = y1 - y2; return Math.sqrt(dx * dx + dy * dy); } function linkParticles(particle, otherParticles, drawCtx) { for (const p of otherParticles) { const d = distance(particle.x, particle.y, p.x, p.y); if (d > SAFE_DISTANCE) { continue; } // Infect other particle! if (particle.status === STATUSES.INFECTED && d < INFECTED_DISTANCE) { p.infect(); } const opacity = 0.8 - (d / SAFE_DISTANCE) * 0.8; drawCtx.lineWidth = 1; drawCtx.strokeStyle = particle.color; //rgba(255,255,255,${opacity})`; drawCtx.globalAlpha = opacity; drawCtx.beginPath(); drawCtx.moveTo(particle.x, particle.y); drawCtx.lineTo(p.x, p.y); drawCtx.closePath(); drawCtx.stroke(); drawCtx.globalAlpha = 1; } } /* ---------------------------------- */ function render() { try { requestAnimationFrame(render); ctx.clearRect(0, 0, width, height); particles.forEach(particle => { particle.update(); if (particle.status === STATUSES.INFECTED) { linkParticles(particle, particles, ctx); } particle.draw(ctx); }); } catch (e) { throw e; } } render(); /* ---------------------------------- */ function reset() { particles = []; for (var i = 0; i < PARTICLE_COUNT; i++) { particles.push(new Particle()); } } reset(); document.addEventListener("click", reset);
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>2D粒子画布效果-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号