[JS]封装嵌套动画效果

[JS]封装嵌套动画效果

封装

主 动画

  • AnimationManager.js
import SubAnimation from './SubAnimation'

export default class AnimationManager {
  constructor(duration) {
    this.duration = duration
    this.subAnimations = []
    this.startTime = null
    this.pausedTime = null
    this.isPlaying = false
    this.animationFrame = null
    this.currentProgress = 0
  }

  addSubAnimation(startRatio, endRatio, animationFunction, completionFunction = null) {
    this.subAnimations.push(new SubAnimation(startRatio, endRatio, animationFunction, completionFunction))
  }

  start() {
    if (!this.isPlaying) {
      this.isPlaying = true
      this.startTime = performance.now() - this.currentProgress * this.duration * 1000
      this.animate()
    }
  }

  pause() {
    if (this.isPlaying) {
      this.isPlaying = false
      cancelAnimationFrame(this.animationFrame)
      this.currentProgress = this.calculateProgress()
    }
  }

  reset() {
    this.pause()
    this.startTime = null
    this.pausedTime = null
  }

  setProgress(newProgress) {
    if (newProgress < 0 || newProgress > 1) throw new Error('[确保进度在 0 到 1 之间]')

    this.currentProgress = newProgress
    this.resetAnimationState()

    if (this.isPlaying) {
      cancelAnimationFrame(this.animationFrame)
      this.startTime = performance.now() - this.currentProgress * this.duration * 1000
      this.animate()
    } else {
      this.executeAnimationsAtProgress(newProgress)
    }
  }

  resetAnimationState() {
    this.subAnimations.forEach(subAnim => {
      subAnim.hasCompleted = false
    })
  }

  calculateProgress() {
    return Math.min((performance.now() - this.startTime) / (this.duration * 1000), 1)
  }

  executeAnimationsAtProgress(progress) {
    this.subAnimations.forEach(subAnim => {
      if (progress >= subAnim.startRatio && progress <= subAnim.endRatio) {
        const subProgress = (progress - subAnim.startRatio) / (subAnim.endRatio - subAnim.startRatio)
        subAnim.animationFunction(subProgress)
      } else if (progress > subAnim.endRatio && !subAnim.hasCompleted) {
        subAnim.completionFunction && subAnim.completionFunction()
        subAnim.hasCompleted = true
      } else if (progress < subAnim.startRatio) {
        subAnim.hasCompleted = false
      }
    })
  }

  animate() {
    const progress = this.calculateProgress()
    this.executeAnimationsAtProgress(progress)
    if (progress < 1) {
      this.animationFrame = requestAnimationFrame(() => this.animate())
    } else {
      this.isPlaying = false
    }
  }
}

子 动画

  • SubAnimation.js
export default class SubAnimation {
  constructor(startRatio, endRatio, animationFunction, completionFunction = null) {
    this.startRatio = startRatio
    this.endRatio = endRatio
    this.animationFunction = animationFunction
    this.completionFunction = completionFunction
    this.hasCompleted = false
  }

  execute(progress) {
    if (progress >= this.startRatio && progress <= this.endRatio) {
      const subProgress = (progress - this.startRatio) / (this.endRatio - this.startRatio)
      this.animationFunction(subProgress)
      this.hasCompleted = false
    } else if (progress > this.endRatio && !this.hasCompleted) {
      if (this.completionFunction) {
        this.completionFunction()
      }
      this.hasCompleted = true
    }
  }
}

方法

  • 创建10s动画
const animManager = new AnimationManager(10);
  • 当前播放状态
const isPlaying = animManager.isPlaying
  • 暂停
animManager.pause()
  • 继续
animManager.start()
  • 重置
animManager.reset()
  • 移动到指定进度[0-1]
animManager.setProgress(0.1)

使用

const animManager = new AnimationManager(10);

// 1. (0-0.1)
animManager.addSubAnimation(0, 0.3, (progress) => {
  console.log("0-0.1", progress);
});

// 2. (0.1-0.3)
let movingPoint;
animManager.addSubAnimation(
  0.3,
  0.7,
  (progress) => {
    console.log("0.3-0.7", progress);
  },
  () => {
    console.log("0.3-0.7 结束");
  }
);

// 4. (0.7-1)
animManager.addSubAnimation(
  0.5,
  1,
  (progress) => {
    console.log("0.5-1", progress);
  },
  () => {
    console.log("完全结束");
  }
);

animManager.start();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的Vue组件封装嵌套案例: ```html <!-- 外层组件 --> <template> <div> <h3>{{title}}</h3> <inner-component :items="items"></inner-component> </div> </template> <script> import InnerComponent from './InnerComponent.vue'; export default { name: 'OuterComponent', components: { InnerComponent }, props: { title: { type: String, required: true }, items: { type: Array, required: true } } } </script> ``` ```html <!-- 内层组件 --> <template> <div> <ul> <li v-for="(item, index) in items" :key="index">{{item}}</li> </ul> </div> </template> <script> export default { name: 'InnerComponent', props: { items: { type: Array, required: true } } } </script> ``` 在这个例子中,我们创建了一个外层组件 `OuterComponent` 和一个内层组件 `InnerComponent`。外层组件接收一个 `title` 属性和一个 `items` 属性,然后将 `items` 属性传递给内层组件。内层组件接收 `items` 属性并将其渲染为一个无序列表。 使用这个组件可以这样: ```html <template> <outer-component title="My List" :items="['Item 1', 'Item 2', 'Item 3']"></outer-component> </template> <script> import OuterComponent from './OuterComponent.vue'; export default { name: 'App', components: { OuterComponent } } </script> ``` 这将渲染一个外层组件,标题为 `My List`,并将一个字符串数组作为 `items` 属性传递给外层组件。外层组件将 `items` 属性传递给内层组件,内层组件将其渲染为一个无序列表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值