js实现canvas大火燃烧火焰动画效果代码
代码语言:html
所属分类:动画
代码描述:js实现canvas大火燃烧火焰动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<html> <head> <style> body { background: #000; height: 100vh; overflow: hidden; } canvas { -webkit-animation: fade-in 2000ms 1000ms forwards; animation: fade-in 2000ms 1000ms forwards; background: #000; background: radial-gradient(circle at 50% 75%, #311, #000); border-radius: 50%; bottom: 0; box-shadow: inset 0 0 0 10px rgba(255, 255, 255, 0.05); left: 0; margin: auto; opacity: 0; position: absolute; right: 0; top: 0; } @-webkit-keyframes fade-in { to { opacity: 1; } } @keyframes fade-in { to { opacity: 1; } } .static { -webkit-animation: fade-in2 2000ms 1000ms forwards; animation: fade-in2 2000ms 1000ms forwards; border-radius: 50%; bottom: 0; height: 400px; margin: auto; left: 0; opacity: 0; position: absolute; right: 0; top: 0; width: 400px; } @-webkit-keyframes fade-in2 { to { opacity: 0.06; } } @keyframes fade-in2 { to { opacity: 0.06; } } </style> </head> <bod> <canvas width="400" height="400"></canvas> <div class="static"></div> <script> console.clear(); let $ = {}; /* |=|ˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉ/ [::{}{}{}{}|=| UTILITY |=|________________________________/ */ $.PI = Math.PI; $.TAU = $.PI * 2; $.rand = (min, max) => { return Math.random() * (max - min) + min; }; $.hsla = (h, s, l, a) => { return `hsla(${h}, ${s}%, ${l}%, ${a})`; }; $.baseRange = (base, range) => { return base + $.rand(-range, range); }; /* |=|ˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉ/ [::{}{}{}{}|=| POOL |=|________________________________/ */ $.Pool = class { constructor(base, preallocateAmount) { this.base = base; this.preallocateAmount = preallocateAmount || 0; this.alive = []; this.dead = []; this.length = 0; this.deadLength = 0; if (this.preallocateAmount) { this.preallocate(); } } preallocate() { for (var i = 0; i < this.preallocateAmount; i++) { this.dead.push(new this.base()); this.deadLength++; } } create(opt) { if (this.deadLength) { var obj = this.dead.pop(); obj.init(opt); this.alive.push(obj); this.deadLength--; this.length++; return obj; } else { var newItem = new this.base(); newItem.init(opt); this.alive.push(newItem); this.length++; return newItem; } } release(obj) { var i = this.alive.indexOf(obj); if (i > -1) { this.dead.push(this.alive.splice(i, 1)[0]); this.length--; this.deadLength++; } } empty() { this.alive.length = 0; this.dead.length = 0; this.length = 0; this.deadLength = 0; } each(action, asc) { var i = this.length; while (i--) { this.alive[i][action](i); } }}; /* |=|ˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉ/ [::{}{}{}{}|=| PARTICLE |=|________________________________/ */ $.Particle = class { constructor() {} init(opt) { Object.assign(this, opt); this.life = 1; } step() { this.velocity += this.acceleration; this.angle += $.rand(-this.wander, this.wander); this.x += Math.cos(this.angle) * this.velocity; this.y += Math.sin(this.angle) * this.velocity; this.life -= this.decay; this.alpha = this.fade ? this.life * 1.5: 1; if (this.life < 0) { this.parent.particles.release(this); } } draw() { $.ctx.beginPath(); $.ctx.arc(this.x, this.y, this.radius, 0, $.TAU); $.ctx.fillStyle = $.hsla(this.hue, this.saturation, this.lightness, this.alpha); $.ctx.fill(); }}; /* |=|ˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉ/ [::{}{}{}{}|=| PARTICLE EMITTER |=|________________________________/ */ $.ParticleEmitter = class { constructor(opt) { Object.assign(this, opt); this.particles = new $.Pool($.Particle, 100); } step() { if ($.tick % this.interval === 0) { this.particles.create({ parent: this, x: $.baseRange(this.x.base, this.x.range), y: $.baseRange(this.y.base, this.y.range), radius: $.baseRange(this.radius.base, this.radius.range), angle: $.baseRange(this.angle.base, this.angle.range), velocity: $.baseRange(this.velocity.base, this.velocity.range), acceleration: $.baseRange(this.acceleration.base, this.acceleration.range), decay: $.baseRange(this.decay.bas.........完整代码请登录后点击上方下载按钮下载查看
网友评论0