[d565449] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | exports.Animation = void 0;
|
---|
| 4 | const Global_1 = require("./Global");
|
---|
| 5 | const Util_1 = require("./Util");
|
---|
| 6 | const now = (function () {
|
---|
| 7 | if (Global_1.glob.performance && Global_1.glob.performance.now) {
|
---|
| 8 | return function () {
|
---|
| 9 | return Global_1.glob.performance.now();
|
---|
| 10 | };
|
---|
| 11 | }
|
---|
| 12 | return function () {
|
---|
| 13 | return new Date().getTime();
|
---|
| 14 | };
|
---|
| 15 | })();
|
---|
| 16 | class Animation {
|
---|
| 17 | constructor(func, layers) {
|
---|
| 18 | this.id = Animation.animIdCounter++;
|
---|
| 19 | this.frame = {
|
---|
| 20 | time: 0,
|
---|
| 21 | timeDiff: 0,
|
---|
| 22 | lastTime: now(),
|
---|
| 23 | frameRate: 0,
|
---|
| 24 | };
|
---|
| 25 | this.func = func;
|
---|
| 26 | this.setLayers(layers);
|
---|
| 27 | }
|
---|
| 28 | setLayers(layers) {
|
---|
| 29 | let lays = [];
|
---|
| 30 | if (layers) {
|
---|
| 31 | lays = Array.isArray(layers) ? layers : [layers];
|
---|
| 32 | }
|
---|
| 33 | this.layers = lays;
|
---|
| 34 | return this;
|
---|
| 35 | }
|
---|
| 36 | getLayers() {
|
---|
| 37 | return this.layers;
|
---|
| 38 | }
|
---|
| 39 | addLayer(layer) {
|
---|
| 40 | const layers = this.layers;
|
---|
| 41 | const len = layers.length;
|
---|
| 42 | for (let n = 0; n < len; n++) {
|
---|
| 43 | if (layers[n]._id === layer._id) {
|
---|
| 44 | return false;
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 | this.layers.push(layer);
|
---|
| 48 | return true;
|
---|
| 49 | }
|
---|
| 50 | isRunning() {
|
---|
| 51 | const a = Animation;
|
---|
| 52 | const animations = a.animations;
|
---|
| 53 | const len = animations.length;
|
---|
| 54 | for (let n = 0; n < len; n++) {
|
---|
| 55 | if (animations[n].id === this.id) {
|
---|
| 56 | return true;
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | return false;
|
---|
| 60 | }
|
---|
| 61 | start() {
|
---|
| 62 | this.stop();
|
---|
| 63 | this.frame.timeDiff = 0;
|
---|
| 64 | this.frame.lastTime = now();
|
---|
| 65 | Animation._addAnimation(this);
|
---|
| 66 | return this;
|
---|
| 67 | }
|
---|
| 68 | stop() {
|
---|
| 69 | Animation._removeAnimation(this);
|
---|
| 70 | return this;
|
---|
| 71 | }
|
---|
| 72 | _updateFrameObject(time) {
|
---|
| 73 | this.frame.timeDiff = time - this.frame.lastTime;
|
---|
| 74 | this.frame.lastTime = time;
|
---|
| 75 | this.frame.time += this.frame.timeDiff;
|
---|
| 76 | this.frame.frameRate = 1000 / this.frame.timeDiff;
|
---|
| 77 | }
|
---|
| 78 | static _addAnimation(anim) {
|
---|
| 79 | this.animations.push(anim);
|
---|
| 80 | this._handleAnimation();
|
---|
| 81 | }
|
---|
| 82 | static _removeAnimation(anim) {
|
---|
| 83 | const id = anim.id;
|
---|
| 84 | const animations = this.animations;
|
---|
| 85 | const len = animations.length;
|
---|
| 86 | for (let n = 0; n < len; n++) {
|
---|
| 87 | if (animations[n].id === id) {
|
---|
| 88 | this.animations.splice(n, 1);
|
---|
| 89 | break;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | static _runFrames() {
|
---|
| 94 | const layerHash = {};
|
---|
| 95 | const animations = this.animations;
|
---|
| 96 | for (let n = 0; n < animations.length; n++) {
|
---|
| 97 | const anim = animations[n];
|
---|
| 98 | const layers = anim.layers;
|
---|
| 99 | const func = anim.func;
|
---|
| 100 | anim._updateFrameObject(now());
|
---|
| 101 | const layersLen = layers.length;
|
---|
| 102 | let needRedraw;
|
---|
| 103 | if (func) {
|
---|
| 104 | needRedraw = func.call(anim, anim.frame) !== false;
|
---|
| 105 | }
|
---|
| 106 | else {
|
---|
| 107 | needRedraw = true;
|
---|
| 108 | }
|
---|
| 109 | if (!needRedraw) {
|
---|
| 110 | continue;
|
---|
| 111 | }
|
---|
| 112 | for (let i = 0; i < layersLen; i++) {
|
---|
| 113 | const layer = layers[i];
|
---|
| 114 | if (layer._id !== undefined) {
|
---|
| 115 | layerHash[layer._id] = layer;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | for (let key in layerHash) {
|
---|
| 120 | if (!layerHash.hasOwnProperty(key)) {
|
---|
| 121 | continue;
|
---|
| 122 | }
|
---|
| 123 | layerHash[key].batchDraw();
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | static _animationLoop() {
|
---|
| 127 | const Anim = Animation;
|
---|
| 128 | if (Anim.animations.length) {
|
---|
| 129 | Anim._runFrames();
|
---|
| 130 | Util_1.Util.requestAnimFrame(Anim._animationLoop);
|
---|
| 131 | }
|
---|
| 132 | else {
|
---|
| 133 | Anim.animRunning = false;
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | static _handleAnimation() {
|
---|
| 137 | if (!this.animRunning) {
|
---|
| 138 | this.animRunning = true;
|
---|
| 139 | Util_1.Util.requestAnimFrame(this._animationLoop);
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | exports.Animation = Animation;
|
---|
| 144 | Animation.animations = [];
|
---|
| 145 | Animation.animIdCounter = 0;
|
---|
| 146 | Animation.animRunning = false;
|
---|