html5开花动画效果代码,HTML5 Canvas藤曼系植物生长开花动画

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

class Line {

constructor(x, y, a) {

this.x = x;

this.y = y;

this.ang = a;

this.angVel = 0;

this.angAcc = 0.4;

}

anim() {

ctx.moveTo(this.x, this.y + 1);

this.x += 6 * Math.cos(this.ang);

this.y += 6 * Math.sin(this.ang);

this.angVel += 0.2 * (Math.random() - Math.random());

this.angVel *= this.angAcc;

this.ang += this.angVel;

if (Math.random() > 0.98) {

if (this.angAcc === 0.4) this.angAcc = 0.98;

else this.angAcc = 0.4;

}

ctx.lineTo(this.x, this.y);

if (Math.random() > 0.96 && numLines < 20) {

lines.add(new Line(width / 2, height, -Math.PI / 2));

numLines++;

}

if (this.x < -20 || this.x > width + 20 || this.y > height || this.y < 30) {

if (numLines > 1) {

numLines--;

lines.delete(this);

ctx.beginPath();

for (let i = 0; i < 2 * Math.PI; i += 0.314) {

ctx.moveTo(this.x, this.y);

ctx.lineTo(this.x + (5 * Math.random() + 12) * Math.cos(i), this.y + (5 * Math.random() + 12) * Math.sin(i));

}

ctx.stroke();

}

}

}

}

const canvas = document.getElementById('c');

const ctx = canvas.getContext('2d');

const width = canvas.width = canvas.offsetWidth * 1;

const height = canvas.height = canvas.offsetHeight * 1;

ctx.strokeStyle = "#654";

const lines = new Set();

let numLines = 0;

let y = 0;

ctx.fillStyle = "#fff";

ctx.fillRect(0, 0, width, height);

lines.add(new Line(width / 2, height, -Math.PI / 2));

function run() {

requestAnimationFrame(run);

ctx.drawImage(canvas, 0, 1);

for (let line of lines) {

ctx.beginPath();

line.anim();

ctx.stroke();

}

}

run();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值