canvas 雪花连线

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
   *{padding:0; margin: 0;}
#canvas{display: block; background-color: black;}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
  <script>
  //1.获取屏幕宽高,
  //2.定义圆函数,将它放置数量里
  //3.存放随机变量值 ,创建attr 存放随机变量值;
  //4.重新绘制圆.然后判断宽高,速度随机;
  //5.定义多一个for循环 j 循环判定距离    //距离  x坐标减去另一个x坐标的平方 + y坐标减去另一个y坐标的平方 =根号  然后连线
  //6.用数组存放鼠标位置,在for循环第一层判断是否有鼠标进入,如果有就产生一个原点,并判断距离,




  var canvas =document.getElementById("canvas");
  var cxt=canvas.getContext("2d");
  canvas.width=window.innerWidth;
  canvas.height=window.innerHeight;
  var w=canvas.width;
  var h=canvas.height;
  var mun=250;
  var attr=[];
  var move={};
  document.οnmοusemοve=function(e){
  move.x=e.pageX;
  move.y=e.pageY;
  };
 
  for(var i=0; i<mun; i++)
  {
  attr[i]={ x:Math.random()*w, y:Math.random()*h, Cw:Math.random()*3-0.5, Cx:Math.random()*3-0.5 };
      Creen(attr[i].x,attr[i].y)
  }
 
  function Creen(x,y){
   cxt.save();
   cxt.beginPath();
             cxt.arc( x,   y,  10, 0, Math.PI * 2, true);
             cxt.closePath();
            cxt.fillStyle = 'white';
             cxt.fill();
  cxt.restore();
 
  }
  function anew(){
  cxt.save();
  cxt.beginPath();
  cxt.clearRect(0,0,w,h);
   
    for(var i=0; i<mun; i++)
  {
         attr[i].x+=attr[i].Cw;
          attr[i].y+=attr[i].Cx;
     Creen(attr[i].x,attr[i].y);
    if(attr[i].x>=w||attr[i].x<=0) attr[i].Cw=-attr[i].Cw;
    if(attr[i].y>=w||attr[i].y<=0) attr[i].Cx=-attr[i].Cx;
        for(var j=0; j<mun; j++)
        {
        if((attr[i].x-attr[j].x)*(attr[i].x-attr[j].x)+(attr[i].y-attr[j].y)*(attr[i].y-attr[j].y)<=50*50)
        {
        stok(attr[i].x,attr[i].y,attr[j].x,attr[j].y,false)
        }
        }
        if(move)
        {
        if((attr[i].x-move.x)*(attr[i].x-move.x)+(attr[i].y-move.y)*(attr[i].y-move.y)<=80*80)
        {
        stok(attr[i].x,attr[i].y,move.x,move.y,true)
       
        }
       
        }
        
  }
 
  function stok(x1,y1,x2,y2,is){
  if(is)
  {
   var grad  = cxt.createLinearGradient(0,0, 0,140);
  grad.addColorStop(0,'rgb(192, 80, 77)');  
          
            grad.addColorStop(1,'rgb(128, 100, 162)');   
           cxt.lineWidth = 1; 
             cxt.strokeStyle=grad;
           
  }
  else{
  cxt.strokeStyle="white";
  }
 
  cxt.save();
           
  cxt.moveTo(x1, y1);
            cxt.lineTo(x2,y2);
           cxt.lineWidth="3";
  cxt.stroke();
  cxt.restore();
  }
       
  cxt.restore();
  requestAnimationFrame(anew)
  }
  requestAnimationFrame(anew)
   
 
 
 
 
  </script>






</body>

</html>




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

对酒丶当歌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值