html图片特效波动,HTML5 Canvas一些炫酷的波动曲线光束

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

var canvasWavey = function(info) {

obj = {

init: function(info) {

this.canvas = document.querySelectorAll(info.selector)[0]

this.canvas.width = window.innerWidth * 2;

this.canvas.height = window.innerHeight * 2;

this.ctx = this.canvas.getContext('2d')

this.points = [];

this.color = [0, 0, 0];

this.colorChange = 2;

this.timer = 30;

this.createPoints();

this.draw();

},

createPoints: function() {

var x = -100;

while (x < this.canvas.width + 200) {

this.createPoint(x)

x += 100;

}

},

createPoint: function(x) {

this.color[this.colorChange] += 10

for (var i = 0; i < 3; i++) {

if (this.color[i] > 100) {

this.color[i] = Math.floor(Math.random() * 100);

this.colorChange = Math.floor(Math.random() * 3)

}

}

this.points.push({

x: x,

y: (this.canvas.height * 0.8) * Math.random() + (this.canvas.height * 0.1),

xspeed: Math.random() * 5 + 3,

yspeed: Math.random() * 6 - 3,

color: [this.color[0], this.color[1], this.color[2]]

})

},

draw: function() {

var that = this;

window.requestAnimationFrame(function() {

that.draw()

})

this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);

this.timer -= 1;

if (this.timer < 0) {

console.log('new')

this.createPoint(this.canvas.width + 500);

this.timer = 30;

this.points.shift();

}

for (var o = 1; o < this.points.length; o++) {

this.points[o].x -= this.points[o].xspeed;

this.points[o].y += this.points[o].yspeed;

var p1 = this.points[o]

var p2 = this.points[o - 1]

var p3 = this.points[o - 2] || this.points[o - 1]

var dir1 = {

x: p2.x - p1.x,

y: p2.y - p1.y

}

var dir2 = {

x: p3.x - p2.x,

y: p3.y - p2.y

}

var per1 = {

x: -dir1.y,

y: dir1.x

}

var per2 = {

x: -dir2.y,

y: dir2.x

}

var spread = 10;

this.ctx.strokeStyle = `rgb(${p1.color[0]}, ${p1.color[1]}, ${p1.color[2]})`

for (var i = -5; i <= 5; i++) {

this.drawLine({

p1: this.offxy(p1, per1, i * spread),

p2: this.offxy(p2, per2 || per1, i * spread)

})

}

}

},

offxy: function(cur, per, length) {

len = Math.sqrt(per.x * per.x + per.y * per.y);

var x = cur.x + (per.x / len) * length;

var y = cur.y + (per.y / len) * length;

return {

x: cur.x + (per.x / len) * length,

y: cur.y + (per.y / len) * length

}

},

drawRect: function(x, y, width, height) {

this.ctx.fillRect(x - width / 2, y - height / 2, width, height);

},

drawLine: function(points) {

this.ctx.beginPath()

this.ctx.moveTo(points.p1.x, points.p1.y)

this.ctx.lineTo(points.p2.x, points.p2.y)

this.ctx.stroke()

}

}

obj.init(info);

return obj;

}

canvasWavey({

selector: 'canvas'

})

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值