index.ts 1.0 KB

123456789101112131415161718192021222324252627282930
  1. // @ts-nocheck
  2. import {isBrowser} from '../isBrowser'
  3. // 是否支持被动事件监听
  4. export const supportsPassive = true;
  5. // 请求动画帧
  6. export function raf(fn: FrameRequestCallback): number {
  7. // 如果是在浏览器环境下,使用 requestAnimationFrame 方法
  8. if (isBrowser) {
  9. return requestAnimationFrame(fn); // 请求动画帧
  10. } else { // 在非浏览器环境下,使用 setTimeout 模拟
  11. return setTimeout(fn, 1000 / 30); // 使用 setTimeout 模拟动画帧,每秒钟执行 30 次
  12. }
  13. }
  14. // 取消动画帧
  15. export function cancelRaf(id: number) {
  16. // 如果是在浏览器环境下,使用 cancelAnimationFrame 方法
  17. if (isBrowser) {
  18. cancelAnimationFrame(id); // 取消动画帧
  19. } else { // 在非浏览器环境下,使用 clearTimeout 模拟
  20. clearTimeout(id); // 使用 clearTimeout 模拟取消动画帧
  21. }
  22. }
  23. // 双倍动画帧
  24. export function doubleRaf(fn: FrameRequestCallback): void {
  25. raf(() => raf(fn)); // 在下一帧回调中再次请求动画帧,实现双倍动画帧效果
  26. }