146 lines
4.0 KiB
JavaScript
146 lines
4.0 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.Animation = void 0;
|
|
const Global_1 = require("./Global");
|
|
const Util_1 = require("./Util");
|
|
var now = (function () {
|
|
if (Global_1.glob.performance && Global_1.glob.performance.now) {
|
|
return function () {
|
|
return Global_1.glob.performance.now();
|
|
};
|
|
}
|
|
return function () {
|
|
return new Date().getTime();
|
|
};
|
|
})();
|
|
class Animation {
|
|
constructor(func, layers) {
|
|
this.id = Animation.animIdCounter++;
|
|
this.frame = {
|
|
time: 0,
|
|
timeDiff: 0,
|
|
lastTime: now(),
|
|
frameRate: 0,
|
|
};
|
|
this.func = func;
|
|
this.setLayers(layers);
|
|
}
|
|
setLayers(layers) {
|
|
var lays = [];
|
|
if (!layers) {
|
|
lays = [];
|
|
}
|
|
else if (layers.length > 0) {
|
|
lays = layers;
|
|
}
|
|
else {
|
|
lays = [layers];
|
|
}
|
|
this.layers = lays;
|
|
return this;
|
|
}
|
|
getLayers() {
|
|
return this.layers;
|
|
}
|
|
addLayer(layer) {
|
|
var layers = this.layers, len = layers.length, n;
|
|
for (n = 0; n < len; n++) {
|
|
if (layers[n]._id === layer._id) {
|
|
return false;
|
|
}
|
|
}
|
|
this.layers.push(layer);
|
|
return true;
|
|
}
|
|
isRunning() {
|
|
var a = Animation, animations = a.animations, len = animations.length, n;
|
|
for (n = 0; n < len; n++) {
|
|
if (animations[n].id === this.id) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
start() {
|
|
this.stop();
|
|
this.frame.timeDiff = 0;
|
|
this.frame.lastTime = now();
|
|
Animation._addAnimation(this);
|
|
return this;
|
|
}
|
|
stop() {
|
|
Animation._removeAnimation(this);
|
|
return this;
|
|
}
|
|
_updateFrameObject(time) {
|
|
this.frame.timeDiff = time - this.frame.lastTime;
|
|
this.frame.lastTime = time;
|
|
this.frame.time += this.frame.timeDiff;
|
|
this.frame.frameRate = 1000 / this.frame.timeDiff;
|
|
}
|
|
static _addAnimation(anim) {
|
|
this.animations.push(anim);
|
|
this._handleAnimation();
|
|
}
|
|
static _removeAnimation(anim) {
|
|
var id = anim.id, animations = this.animations, len = animations.length, n;
|
|
for (n = 0; n < len; n++) {
|
|
if (animations[n].id === id) {
|
|
this.animations.splice(n, 1);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
static _runFrames() {
|
|
var layerHash = {}, animations = this.animations, anim, layers, func, n, i, layersLen, layer, key, needRedraw;
|
|
for (n = 0; n < animations.length; n++) {
|
|
anim = animations[n];
|
|
layers = anim.layers;
|
|
func = anim.func;
|
|
anim._updateFrameObject(now());
|
|
layersLen = layers.length;
|
|
if (func) {
|
|
needRedraw = func.call(anim, anim.frame) !== false;
|
|
}
|
|
else {
|
|
needRedraw = true;
|
|
}
|
|
if (!needRedraw) {
|
|
continue;
|
|
}
|
|
for (i = 0; i < layersLen; i++) {
|
|
layer = layers[i];
|
|
if (layer._id !== undefined) {
|
|
layerHash[layer._id] = layer;
|
|
}
|
|
}
|
|
}
|
|
for (key in layerHash) {
|
|
if (!layerHash.hasOwnProperty(key)) {
|
|
continue;
|
|
}
|
|
layerHash[key].batchDraw();
|
|
}
|
|
}
|
|
static _animationLoop() {
|
|
var Anim = Animation;
|
|
if (Anim.animations.length) {
|
|
Anim._runFrames();
|
|
Util_1.Util.requestAnimFrame(Anim._animationLoop);
|
|
}
|
|
else {
|
|
Anim.animRunning = false;
|
|
}
|
|
}
|
|
static _handleAnimation() {
|
|
if (!this.animRunning) {
|
|
this.animRunning = true;
|
|
Util_1.Util.requestAnimFrame(this._animationLoop);
|
|
}
|
|
}
|
|
}
|
|
exports.Animation = Animation;
|
|
Animation.animations = [];
|
|
Animation.animIdCounter = 0;
|
|
Animation.animRunning = false;
|