DAILY DOCDAILY DOC
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
  • BFC 块级格式化上下文
  • Note
  • WebAssembly
  • public api
  • 位运算
  • bitwise operator
  • css实现隐藏效果
  • css snippets
  • 抖音点赞
  • js 相等判断
  • fetch ReadableStream
  • git
  • Github Actions 工作流
  • google search
  • RPC vs HTTP
  • gravatar
  • hhkb
  • Init project
  • input 文件上传
  • mac

    • Mac 使用技巧
    • alfred
    • mac shortcuts
    • shortcuts text edit
    • mac 修改host
  • 微前端
  • mock
  • nginx dump
  • nginx
  • NirCmd
  • npm
  • Operator Precedence
  • package.json
  • url query 解析
  • pnpm
  • JavaScript Precise countdown
  • react 模版
  • regexp
  • setup web development
  • telegram

    • telegram bot
  • timeFunction ease
  • 视频裁剪
  • vscode

    • vscode 高级指南
    • bracketPairs
    • jsconfig.json
    • vscode pipe into code
    • social project
    • vscode tasks
  • draggable resizable
  • windows 激活
  • 前端截图实现
  • 文本配音 富文本实现
  • 图片处理
  • 前端坐标
  • 定时任务
  • work efficient
  • 微信小程序动画实现方案
  • 排列组合
  • 数列
  • 语音驱动文字
  • 浏览器
  • 状态管理
  • 移动盒子
  • 移动端开发常用snippets
  • 设计模式
  • web performance

timeFunction ease

实现一个 ease 函数,用于根据指定的时间函数调用 callback,可以按照以下步骤进行:

  1. 定义一个 ease 函数,接受时间函数和回调函数作为参数。
  2. 在 ease 函数内部,使用 requestAnimationFrame 实现动画效果。
  3. 使用时间函数来计算当前动画进度,并调用回调函数。

示例代码

function ease(timeFunction, duration, callback) {
  const startTime = performance.now();

  function animate(currentTime) {
    const elapsedTime = currentTime - startTime;
    const progress = Math.min(elapsedTime / duration, 1);
    const easedProgress = timeFunction(progress);

    callback(easedProgress);

    if (elapsedTime < duration) {
      requestAnimationFrame(animate);
    }
  }

  requestAnimationFrame(animate);
}

// 示例时间函数(ease-in-out 函数)
function easeInOutQuad(t) {
  return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
}

// 使用 ease 函数
ease(easeInOutQuad, 2000, (progress) => {
  console.log(`Progress: ${progress}`);
  // 在此处可以更新动画,例如改变元素的样式
});

解释

  1. ease 函数:

    • timeFunction: 一个函数,接受当前进度(0 到 1 之间的值)并返回经过处理的进度值。
    • duration: 动画的持续时间(毫秒)。
    • callback: 每次动画帧更新时调用的函数,接受一个参数 progress,表示当前的动画进度。
    • startTime: 动画开始的时间。
    • animate: 一个递归函数,通过 requestAnimationFrame 调用自己,更新动画进度并调用 callback。
  2. easeInOutQuad 时间函数:

    • 这是一个简单的 ease-in-out 函数,它根据当前进度返回经过处理的进度值,使动画开始和结束时较慢,中间较快。
  3. 使用 ease 函数:

    • 调用 ease 函数,传入时间函数、持续时间和回调函数。
    • 在回调函数中,更新动画进度,例如改变 DOM 元素的样式。

这个示例展示了如何创建一个通用的 ease 函数来处理动画效果。你可以根据需要修改时间函数和回调函数,实现不同的动画效果。

timeFunction
// 线性
function linear(t) {
  return t;
}

// 缓入
function easeInQuad(t) {
  return t * t;
}

// 缓出
function easeOutQuad(t) {
  return t * (2 - t);
}

// 缓入缓出
function easeInOutQuad(t) {
  return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
}

// 缓入立方
function easeInCubic(t) {
  return t * t * t;
}

// 缓出立方
function easeOutCubic(t) {
  return (--t) * t * t + 1;
}

// 缓入缓出立方
function easeInOutCubic(t) {
  return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
}

// 缓入四次方
function easeInQuart(t) {
  return t * t * t * t;
}

// 缓出四次方
function easeOutQuart(t) {
  return 1 - (--t) * t * t * t;
}

// 缓入缓出四次方
function easeInOutQuart(t) {
  return t < 0.5 ? 8 * t * t * t * t : 1 - 8 * (--t) * t * t * t;
}

// 缓入五次方
function easeInQuint(t) {
  return t * t * t * t * t;
}

// 缓出五次方
function easeOutQuint(t) {
  return 1 + (--t) * t * t * t * t;
}

// 缓入缓出五次方
function easeInOutQuint(t) {
  return t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * (--t) * t * t * t * t;
}
Last Updated:
Contributors: rosendo
Next
视频裁剪