Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
body { margin: 0; width: 100%; height: 100%; overflow: hidden; background-color: rgba(0, 0, 0, 0.9); }
JavaScript
const canvas = document.querySelector("canvas"); const ctx = canvas.getContext("2d"); // helper functions const getRandomFloat = (min, max) => (max - min) * Math.random() + min; const getRandomInt = (min, max) => Math.floor(getRandomFloat(min, max + 1)); // viewport class class Viewport { constructor() { this.update(); } update() { this.width = window.innerWidth; this.height = window.innerHeight; this.center = { x: this.width / 2, y: this.height / 2 }; canvas.width = this.width; canvas.height = this.height; } } const vw = new Viewport(); const maxAmp = 250; const minAmp = 150; const numParticles = 100; class Tracks { constructor() { const numTracks = 15; this.tracks = []; for (let i = 0; i < numTracks; ++i) { const ratio = i / numTracks; this.tracks.push({ amp: (maxAmp - minAmp) * ratio + minAmp }); } } } const { tracks } = new Tracks(); class Particle { constructor() { this.init(); } init() { // constants this.baseAmp = tracks[getRandomInt(0, tracks.length - 1)].amp; this.rotSpeed = getRandomFloat(0.01, 0.015); this.ampSpeed = 1; this.baseRad = 3; this.grownAge = 10; this.age = 0; this.amp = this.baseAmp; this.rad = this.baseRad; this.angle = getRandomFloat(0, Math.PI * 2); this.pos = { x: 0, y: 0 }; } updateRadius() { let ratio = (this.amp / this.baseAmp) * 2.5 - 1.5; this.rad = ratio * this.baseRad; } updatePosition() { this.angle += this.rotSpeed; this.pos.x = vw.center.x + this.amp * Math.cos(this.angle) * 1.2; this.pos.y = vw.center.y + this.amp * Math.pow(Math.sin(this.angle), 3) * 0.8; } draw() { const { pos } = this; const ageAttack = this.age / this.grownAge; const rad = this.rad * ageAttack; const alpha = ageAttack; ctx.beginPath(); ctx.arc(pos.x, pos.y, rad, 0, Math.PI * 2); ctx.fillStyle = `rgba(255, 255, 255, ${alpha}`; ctx.fill(); } update() { if (this.age < this.grownAge) { this.age++; } this.amp -= this.ampSpeed; this.updateRadius(); if (this.rad > 0) { this.updatePosition(); this.draw(); } else { this.init(); } } } class Emitter { constructor() { this.particles = [...Array(numParticles).keys()].map(() => new Particle()); } update() { this.particles.forEach(particle => particle.update()); } } const emitter = new Emitter(); // init canvas color with scene overlap initCanvasColor = () => { ctx.fillStyle = `rgba(255, 255, 255, 1)`; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = `rgba(0, 0, 0, 0.08)`; ctx.fillRect(0, 0, canvas.width, canvas.height); }; // canvas renderer const render = () => { // alpha value affects trail length ctx.fillStyle = `rgba(0, 0, 0, 0.08)`; ctx.fillRect(0, 0, canvas.width, canvas.height); emitter.update(); requestAnimationFrame(render); }; initCanvasColor(); render(); window.addEventListener("resize", () => vw.update());
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>canvas粒子模拟卡冈图雅黑洞动画-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号