最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • 让你的博客下个雪吧

    正文概述 掘金(Mongola)   2021-02-09   436

    前言

    一直以来都不怎么敢玩canvas,前些日子看见有篇文章讲解了DOM实现雪花飘落的效果,今天我也用canvas试一试。

    起步

    第一步先画出一个雪花

    让你的博客下个雪吧

    量产雪花

    通过循环生成随机大小,位置不一的雪花

    let count = 100
    let list = []
    for (let i = 0; i < count; i++) {
      list.push({
        // 雪花的位置
        x: Math.round(Math.random() * width ),
        y: Math.round(Math.random() * height),
        // 雪花的半径
        r: Math.round(Math.random() * 10 + 1),
        // 雪花x、y轴的滑落速度
        speedX: Math.random() + 1,
        speedY: Math.random() + 1,
      })
    }
    

    通过上面的代码生成100个雪花的信息

    绘制雪花

    canvas 绘制和CSS不同,不能直接用样式处理

    let Context = canvas.getContext('2d')
    // 参数分别时 圆的位置x,y 半径 r 开始位置 结束位置
    Context.arc(100, 100, 10, 0, Math.PI * 2, true)
    // 辐射渐变,参数 对应着 位置x,y,内圈半径 位置x,y 外圈半径
    let radialGradient = Context.createRadialGradient(100, 100, 0, 100, 100, 10)
    radialGradient.addColorStop(0, '#fff')
    radialGradient.addColorStop(0.7, 'rgba(255, 255, 255, 0)')
    Context.fillStyle = radialGradient
    Context.fill()
    

    上面通过canvas绘制一个渐变圆

    结合上面的雪花信息列表,循环绘制出所有的圆

    让你的博客下个雪吧

    让雪花飞舞

    let width = window.innerWidth
    let height = window.innerHeight
    let Can = document.getElementsById('canvas')
    Can.width = width
    Can.height = height
    let Context = Can.getContext('2d')
    let nodeInfoList = []
    let count = 100
    
    init()
    window.requestAnimationFrame(draw)
    
    function init() {
      for (let i = 0; i < count; i++) {
        nodeInfoList.push({
          // 雪花的位置
          x: Math.round(Math.random() * width),
          y: Math.round(Math.random() * height),
          // 雪花的半径
          r: Math.round(Math.random() * 15 + 1),
          // 雪花x、y轴的滑落速度
          speedX: Math.random() + 1,
          speedY: Math.random() + 1,
        })
      }
    }
    
    function draw() {
      Context.clearRect(0, 0, width, height)
      Context.beginPath()
      for (let i = 0; i < count; i++) {
        const node = nodeInfoList[i]
        Context.arc(node.x, node.y, node.r, 0, Math.PI * 2, true)
        let radialGradient = Context.createRadialGradient(node.x, node.y, 0, node.x, node.y, node.r)
        radialGradient.addColorStop(0, '#fff')
        radialGradient.addColorStop(0.7, 'rgba(255, 255, 255, 0)')
        Context.fillStyle = radialGradient
        Context.fillRect(node.x - node.r, node.y - node.r, node.r * 2, node.r * 2)
      }
      calcNextInfo()
      window.requestAnimationFrame(draw)
    }
    
    function calcNextInfo() {
      for (let i = 0; i < count; i++) {
        const node = nodeInfoList[i]
        node.x += node.speedX
        node.y += node.speedY
        if (node.x > width || node.y > height) {
          nodeInfoList[i] = {
          // 雪花的位置
          x: Math.round(Math.random() * width),
          y: Math.round(Math.random() * height),
          // 雪花的半径
          r: Math.round(Math.random() * 15 + 1),
          // 雪花x、y轴的滑落速度
          speedX: Math.random() + 1,
          speedY: Math.random() + 1,
        }
        }
      }
    }
    

    运行代码将会看到满屏的雪花飞舞。

    让你的博客下个雪吧


    起源地下载网 » 让你的博客下个雪吧

    常见问题FAQ

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

    发表评论

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

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

    联系作者

    请选择支付方式

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