UNPKG

@antv/f2

Version:

Charts for mobile visualization.

101 lines 3.03 kB
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _typeof from "@babel/runtime/helpers/esm/typeof"; var requestAnimationFrame = (typeof window === "undefined" ? "undefined" : _typeof(window)) === 'object' && window.requestAnimationFrame ? window.requestAnimationFrame : function (fn) { return setTimeout(fn, 16); }; var cancelAnimationFrame = (typeof window === "undefined" ? "undefined" : _typeof(window)) === 'object' && window.cancelAnimationFrame ? window.cancelAnimationFrame : function (number) { return clearTimeout(number); }; var clock = (typeof performance === "undefined" ? "undefined" : _typeof(performance)) === 'object' && performance.now ? performance : Date; var Timeline = /*#__PURE__*/function () { function Timeline() { _classCallCheck(this, Timeline); this.playing = false; // 暂停中 this.paused = false; // 暂停的时间点 this.pausedTime = 0; } _createClass(Timeline, [{ key: "play", value: function play(duration, onUpdate, onEnd) { var _this = this; if (duration <= 0) { onEnd(); return; } // 上次动画未结束 if (this.playing) { return; } // 记录 duration、onUpdate、onEnd this.duration = duration; this.onUpdate = onUpdate; this.onEnd = onEnd; var paused = this.paused, pausedTime = this.pausedTime; this.playing = true; var startTime = clock.now(); // 如果当前正在暂停状态, 从暂停态继续播放 if (paused && pausedTime) { startTime = startTime - pausedTime; this.paused = false; this.pausedTime = 0; } var play = function play() { var now = clock.now(); var time = now - startTime; if (time >= duration) { onUpdate(duration); onEnd(); _this.playing = false; return; } if (_this.paused) { onUpdate(time); _this.pausedTime = time; _this.playing = false; return; } onUpdate(time); _this.animationFrameNumber = requestAnimationFrame(play); }; this.animationFrameNumber = requestAnimationFrame(play); } }, { key: "pause", value: function pause() { this.paused = true; } }, { key: "stop", value: function stop() { this.playing = false; } }, { key: "end", value: function end() { if (!this.playing) { return; } // 停掉动画 this.abort(); // 更新到最后一帧状态 this.onUpdate(this.duration); this.onEnd(); } }, { key: "abort", value: function abort() { if (!this.animationFrameNumber) { return; } cancelAnimationFrame(this.animationFrameNumber); this.playing = false; this.animationFrameNumber = null; } }]); return Timeline; }(); export default Timeline;