uniapp,vue的cavans飘落效果

4 篇文章 1 订阅

首先效果图。
飘落个数,速度,同时最多出现几个一起飘可以控制
在这里插入图片描述

代码

<canvas canvas-id="myCanvas" class="my-canvans" v-if='canvansShow'/>
// canvansShow 默认true
onShow () {
    this.createStar(this.imgUrl)
},
mothods{
	createStar (imgUrl) {
      // #ifdef MP-WEIXIN
      let starImage = ''
      uni.getImageInfo({
        src: imgUrl,
        success: res => {
          starImage = res.path
        }
      })
      // #endif
      let { requestAnimationFrame } = this
      function Star (x, y, radius) {
        this.x = x
        this.y = y
        this.sx = 0
        this.sy = 0
        this.deg = 0
        this.radius = radius
        this.ax = Math.random() < 0.5 ? 0.01 : -0.01
      }
      Star.prototype.update = function () {
        const deltaDeg = Math.random() * 0.6 + 0.2
        this.sx += this.ax
        if (this.sx >= SPEED_LIMIT_X || this.sx <= -SPEED_LIMIT_X) {
          this.ax *= -1
        }
        if (this.sy < SPEED_LIMIT_Y) {
          this.sy += G
        }
        this.deg += deltaDeg
        this.x += this.sx
        this.y += this.sy
      }
      Star.prototype.draw = function () {
        const { radius } = this
        ctx.save()
        ctx.translate(this.x, this.y)
        ctx.rotate((this.deg * Math.PI) / 180)
        // #ifdef MP-WEIXIN
        ctx.drawImage(starImage, -radius, -radius * 1.8, radius * 5, radius * 5)
        // #endif
        // #ifdef MP-ALIPAY
        ctx.drawImage(imgUrl, -radius, -radius * 1.8, radius * 5, radius * 5)
        // #endif
        ctx.restore()
      }
      const stars = []
      // 下落的加速度
      const G = 13
      // 速度上限,避免速度过快
      const SPEED_LIMIT_X = 1
      const SPEED_LIMIT_Y = 1
      const W = uni.getSystemInfoSync().windowWidth
      const H = uni.getSystemInfoSync().windowHeight
      const starCount = 10 // 星星总的数量
      let starNum = 0 // 隔多少个设定的毫秒数生成一个星星
      const deltaTime = 1 // 每次增加的星星数量
      const ctx = uni.createCanvasContext('myCanvas')
      let starAllBottom = 0 // 到达底部的星星数量
      let trueStarCount = 0 // 真正生成的星星数量
      const starLoop = () => {
        requestAnimationFrame = setTimeout(() => {
          starLoop()
        }, 1000 / 60)
        ctx.clearRect(0, 0, W, H)
        starNum += deltaTime
        if (starNum > starCount && trueStarCount < starCount) {
          stars.push(new Star(Math.random() * W, 0, Math.random() * 5 + 5))
          starNum %= starCount
          trueStarCount += 1
        }
        // isEmpty是lodash库的方法
        if (isEmpty(stars)) return
        stars.forEach((s, i) => {
          //重复绘制
          s.update()
          s.draw()
          if (s.y >= H) {
            //大于屏幕高度的就从数组里去掉
            starAllBottom += 1
            stars.splice(i, 1)
            if (starAllBottom === starCount) {
             // 星星全部到达底部,动画结束
              clearTimeout(requestAnimationFrame)
              this.canvansShow && (this.canvansShow = false)
              // 后续操作
              // xxxxx
            }
          }
        })
        ctx.draw()
      }
      starLoop()
    },
}

借鉴大佬的代码,在原代码上进行修改
大佬代码链接奉上:https://developers.weixin.qq.com/community/develop/article/doc/000e443b1247a039fd99230b457013

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
uniApp 是一款基于微信小程序平台开发跨平台应用的框架,它允许开发者利用一套代码即可构建出能在微信、支付宝、百度等多个平台运行的应用程序。uniApp 支持多种前端技术栈,包括 Vue.js 和 React 等,使得开发者能够选择最适合项目需求的技术方案。 **uniApp + Vue3 的组合** 当使用 uniApp 结合 Vue3 进行开发时,可以充分发挥 Vue3 的优势以及 uniApp 的跨平台能力。Vue3 相比于 Vue2 引入了多项改进和优化,如更高效的渲染引擎(使用 Composition API 或 Options API)、性能提升、更好的语法支持等,这都让应用开发变得更加高效和便捷。 **uniApp 中使用 Jest 进行测试** Jest 是一个用于 JavaScript 项目的快速集成式测试工具,特别适合于单文件组件、模块化应用的单元测试。在 uniApp 项目中引入 Jest 可以帮助开发者对 Vue 组件进行详尽的自动化测试,确保每个功能模块的正常运行,同时提高代码的质量和维护性。 在 uniApp Vue3 应用中,你可以使用 Jest 来编写测试脚本来验证组件的交互行为、数据处理逻辑、以及 API 调用等功能。为了在 uniApp 中设置和运行 Jest 测试,你需要首先确保你的环境已安装了必要的依赖包,如 `@vue/test-utils`、`jest-plugin-vue` 等,并按照官方文档配置相应的测试设置和环境。 **相关问题 - 实践案例:** 1. **如何在 uniApp Vue3 项目中集成 Jest 进行单元测试?** 首先需要安装 Jest 和相关的 Vue 插件,然后在项目目录下创建测试文件夹并编写针对各个组件和功能的测试用例。确保在 `package.json` 文件中添加了测试脚本命令,以便运行 Jest。 2. **在 uniApp Vue3 项目中如何编写和运行测试用例?** 编写测试用例时,可以利用 Vue Test Utils 和其他 Jest 具有的断言库来进行组件交互模拟、状态变更检测等操作。通过 Jest 提供的 CLI 工具,可以直接从命令行运行所有的测试文件,获取测试结果和反馈信息。 3. **uniApp Vue3 项目中的测试覆盖率应该如何监控和提高?** 利用 Jest 内置的报告工具,如 Codecov 或 istanbul,可以帮助监控测试覆盖率情况。通过分析覆盖率报告,找出未覆盖的代码路径,进一步完善测试用例,以达到更高的代码覆盖率目标。同时,持续改进测试策略和代码设计,可以使测试过程更为有效,减少潜在的 Bug 出现概率。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值