</canvas>css html, body {...">
最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • canvas 绘制特效 小球连接线动画

    正文概述 掘金(rudy_zhou)   2021-01-04   540

    引言

       一个很经典的特效,花了一点时间自己手动写了一个,先上图:

    canvas 绘制特效 小球连接线动画

    详细代码

    html

    <canvas id="canvas_bg"></canvas>
    

    css

    html, body {
      height: 100%;
      padding: 0;
      margin: 0;
    }
    #canvas_bg {
      display: block;
    }
    

    javascript

    class circle {
     constructor(num) {
       this.canvas = document.getElementById('canvas_bg');
       this.canvas.width = document.documentElement.clientWidth;
       this.canvas.height = document.documentElement.clientHeight;
       this.ctx = this.canvas.getContext('2d');
    
       // 创建随机状态小球
       this.arr = Array.from(new Array(num)).map(item => ({
         x: Math.random() * this.canvas.width,
         y: Math.random() * this.canvas.height,
         speed: Math.random() * 1.5 + 0.5,
         xDir: Math.random() > 0.5 ? -1:1,
         yDir: Math.random() > 0.5 ? -1:1,
         r: 2
       }))
       // 小球连线距离
       this.dist = 100
    
       this.animation()
    
       window.onresize = ()=> {
         this.canvas.width = document.documentElement.clientWidth;
         this.canvas.height = document.documentElement.clientHeight;
       }
     }
     // 计算小球位置并判断方向与绘制
     drawCircle() {
       this.arr.forEach(item => {
         item.x += item.xDir * item.speed
         item.y += item.yDir * item.speed
    
         item.x <= 0 && (item.xDir = 1) 
         item.x > this.canvas.width - 1 && (item.xDir = -1, item.x = this.canvas.width - 1)
    
         item.y <= 0 && (item.yDir = 1) 
         item.y > this.canvas.height - 1 && (item.yDir = -1, item.y = this.canvas.height - 1)
    
         this.ctx.beginPath();
         this.ctx.arc(item.x, item.y, item.r, 0, 2 * Math.PI);
         this.ctx.fill();
       })
     }
    
     // 计算连线距离内的小球
     calcLine() {
       var arr = this.arr.concat()
       this.lineArr = []
       for(var i = 0,len = arr.length; i < len; i++){
         for(let y = i+1; y < len; y++){
           let val = Math.sqrt(Math.pow(arr[i].x - arr[y].x, 2) + Math.pow(arr[i].y - arr[y].y, 2) ,2);
           if(val < this.dist){
             this.lineArr.push({
               start: arr[i],
               end: arr[y],
               val: val,
               ratio: (val / this.dist)
             })
           }
         }
       }
    }
    
    // 绘制链接线条
     drawLine() {
       while(this.lineArr.length){
         this.ctx.beginPath()
         let item = this.lineArr.shift();
         let c = 255 * item.ratio
    
         this.ctx.strokeStyle = `rgb(${c},${c},${c})`
         this.ctx.moveTo(item.start.x, item.start.y)
         this.ctx.lineTo(item.end.x, item.end.y); 
         this.ctx.stroke();
       }
     }
    
     // 动画过渡
     animation() {
       this.canvas.width = this.canvas.width
       this.drawCircle()
       this.calcLine()
       this.drawLine()
       setTimeout(() => {
         this.animation()
       }, 30)
     }
    }
    
    var circleObj = new circle(100);
    

    demo演示

    点击进入小球动画演示Demo

    解释

    这个动画整体效果其实很简单,画布初始化什么的就不说了。

    1.首先创建一些随机状态的小球,有位置、移动方向、移动速率等

    2.通过双层循环判断每个小球之间的距离(就用初中学的勾股定理,直角三角形两边的平方和等于斜边的平方,斜边就是小球距离),符合条件的(我设置的小于100)都存起来

    3.最后绘制小球与线条,加上动画渲染(我这里每隔30毫秒渲染一次,每次渲染都要重新计算上面提到的参数)

    结语

    详细看看上方的demo中演示与代码,代码量很少,理解起来也不难,只要你有初中知识就能理解。

    以上如有问题或疏漏,欢迎指正


    起源地下载网 » canvas 绘制特效 小球连接线动画

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元