html canvas 水波纹

15 篇文章 0 订阅
5 篇文章 0 订阅

 

原理就是每个小圆都在自己围绕一个大圆做旋转, 只要让他们的初始角度不一样组合在一起看上去就像是在运动, 只要一直观察其中一个圆他们都在公转

 

<!DOCTYPE html>
<html>
  <head>
    <title>index.html</title>
  </head>
  
  <body>
    This is my HTML page. <br>
    
    <a href="pages">pages</a>
    
    <canvas id="canvas" style="display: bloack; margin: auto; background-color: black;" ></canvas>
  </body>
  
  <script>
  
      var canvas = document.getElementById("canvas");
      var ctx = canvas.getContext("2d");
      var width = window.innerWidth * 0.9;
      var height = 600;
      var cx = width / 2;
      var cy = height / 2;
      var size = 100;
      canvas.width = width;
      canvas.height = height;
      
      var list = new Array();
      
      drawGrid();
      setInterval(function(){
          clear();
          list.forEach(function(c){
              c.draw();
          });
      }, 50);
      
      function clear(){
          ctx.beginPath();
          ctx.clearRect(0, 0, width, height);
      }
      
      function ArcClass(){
          this.angle = -90;
          this.radian = 5;
          this.cx = 0;
          this.cy = 0;
          this.x = 0;
          this.y = 0;
          this.r = 8;
          this.size = 20;
      }
      ArcClass.prototype.setAngle = function(angle){
          this.angle += angle;
          if(this.angle >= 360 - 90)
              this.angle = -90;
      };
      ArcClass.prototype.draw = function(){
          var radian = this.angle * Math.PI / 180;
          this.x = Math.cos(radian) * this.size + this.cx;
		  this.y = Math.sin(radian) * this.size + this.cy;
		  ctx.beginPath();
		  ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2);
		  ctx.fillStyle = "white";
		  ctx.fill();
		  ctx.closePath();
		  this.setAngle(this.radian);
      };
      
      function drawGrid(){
          var sx = 200;
          var sy = 100;
          var x = 0;
          var y = 0;
          var c = null;
          for(var i = 0 ; i <= 20 ; i++){
              for(var j = 0 ; j <= 20 ; j++){
                  x = sx + i * 20;
                  y = sy + j * 20;
				  c = new ArcClass();
				  list.push(c);
				  c.cx = x;
				  c.cy = y;
				  c.angle += i * 18;
				  /*
				  
				  ctx.beginPath();
				  ctx.arc(x, y, 20, 0, Math.PI * 2);
				  ctx.strokeStyle = "white";
				  ctx.stroke();
				  ctx.closePath();
				  
				  */
              }
          }
      }
      
  </script>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值