欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 综合教程 >内容正文

综合教程

canvas点阵函数波动,类似飘带或水波

发布时间:2023/12/13 综合教程 32 生活家
生活随笔 收集整理的这篇文章主要介绍了 canvas点阵函数波动,类似飘带或水波 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

canvas动画利用函数波动特点制作水波动画

<canvas id="myCanvas" width="500" height="400"></canvas>

var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
var i=0;
setInterval(move,30);
function move(){

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

  var dig=Math.PI/24;
  var colorArr = ['red','yellow','green','blue','purple']
  var colorIndex = 0
  for (k=0;k<=14;k++){

    for (n=0;n<=17;n++){
      colorIndex++
      if (colorIndex >= 5) {
        colorIndex = 0
      }
      ctx.fillStyle = colorArr[colorIndex]
      x0=30*n;
      y0=30*k;
      var x=24*Math.sin((i+k*2+n*3)*dig)+x0;
      var y=24*Math.cos((i+k*2+n*3)*dig)+y0;
      ctx.beginPath();
      ctx.arc(x,y,8,0,Math.PI*2,true);
      ctx.closePath();
      ctx.fill();
    }
   }

   i=i+1;
   if (i>=48) i=0;
 }

直接可复制代码查看效果

总结

以上是生活随笔为你收集整理的canvas点阵函数波动,类似飘带或水波的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。