波浪线动画html,Canvas波浪线动画特效

var c = document.querySelector('.c') /* canvas element */ ,

w /* canvas width */ , h /* canvas height */ ,

ctx = c.getContext('2d') /* canvas context */ ,

/* previous & current coordinates */

x0, y0, x, y,

t = 0,

t_step = 1 / 10,

tmp,

/* just me being lazy */

exp = Math.exp,

pow = Math.pow,

sqrt = Math.sqrt,

PI = Math.PI,

sin = Math.sin,

cos = Math.cos;

/* FUNCTIONS */

/* a random number between min & max */

var rand = function(max, min) {

var b = (max === 0 || max) ? max : 1,

a = min || 0;

return a + (b - a) * Math.random();

};

var trimUnit = function(input_str, unit) {

return parseInt(input_str.split(unit)[0], 10);

};

var initCanvas = function() {

var s = getComputedStyle(c);

w = c.width = trimUnit(s.width, 'px');

h = c.height = trimUnit(s.height, 'px');

};

var wave = function() {

x0 = -1, y0 = h / 1;

ctx.clearRect(0, 0, w, h);

tmp = pow(t, 1.75) / 10; /* keep computation out of loop */

for (x = 0; x < w; x = x + 3) {

y = 9 * sqrt(x) * sin(x / 45 / PI + t / 3 + sin(x / 29 + t)) +

32 * sin(t) * cos(x / 19 + t / 7) +

16 * cos(t) * sin(sqrt(x) + rand(3, 2) * tmp) + h / 2;

ctx.beginPath();

ctx.moveTo(x0, y0);

ctx.lineTo(x, y);

ctx.lineWidth = 1;

ctx.strokeStyle = 'hsl(' + (2 * x / w + t) * 180 + ', 100%, 65%)';

ctx.stroke();

x0 = x;

y0 = y;

}

t += t_step;

requestAnimationFrame(wave);

};

/* START THE MADNESS */

setTimeout(function() {

initCanvas();

wave();

/* fix looks on resize */

addEventListener('resize', initCanvas, false);

}, 15);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值