Parcourir la source

增加animation方法

Gitea il y a 1 an
Parent
commit
57490a54d3
2 fichiers modifiés avec 25 ajouts et 1 suppressions
  1. 2 1
      main.js
  2. 23 0
      tools/animation.js

+ 2 - 1
main.js

@@ -1,4 +1,5 @@
 import { createChunk } from "./tools/createChunk.js";
 import { cutFile } from "./tools/cutFile.js";
+import animation from "./tools/animation.js";
 
-export { createChunk, cutFile }
+export { createChunk, cutFile, animation }

+ 23 - 0
tools/animation.js

@@ -0,0 +1,23 @@
+/**
+ * 执行动画函数
+ * @param {Function} callback 回调函数
+ * @param {boolean} isEnd 是否停止
+ * @param {number} duration 绘制的间隔时间(默认1s)
+ */
+export default function animation(callback, duration = 1000) {
+  // 判断是否为函数
+  if(typeof callback !== 'function') throw new Error('callback is not a function');
+  // 判断是否为数字
+  if(typeof duration !== 'number') throw new Error('duration is not a number');
+  // 判断是否 > 16
+  if(duration <= 16) throw new Error('duration is not a Less than or equal to 16');
+
+  let time = Date.now();
+  function _run() {
+    if (Date.now() - time > duration) {
+      time = Date.now();
+      callback(_run);
+    } else requestAnimationFrame(_run);
+  }
+  _run();
+}