Initial commit
This commit is contained in:
141
node_modules/konva/lib/Animation.js
generated
vendored
Normal file
141
node_modules/konva/lib/Animation.js
generated
vendored
Normal file
@@ -0,0 +1,141 @@
|
||||
import { glob } from './Global.js';
|
||||
import { Util } from './Util.js';
|
||||
var now = (function () {
|
||||
if (glob.performance && glob.performance.now) {
|
||||
return function () {
|
||||
return glob.performance.now();
|
||||
};
|
||||
}
|
||||
return function () {
|
||||
return new Date().getTime();
|
||||
};
|
||||
})();
|
||||
export 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.requestAnimFrame(Anim._animationLoop);
|
||||
}
|
||||
else {
|
||||
Anim.animRunning = false;
|
||||
}
|
||||
}
|
||||
static _handleAnimation() {
|
||||
if (!this.animRunning) {
|
||||
this.animRunning = true;
|
||||
Util.requestAnimFrame(this._animationLoop);
|
||||
}
|
||||
}
|
||||
}
|
||||
Animation.animations = [];
|
||||
Animation.animIdCounter = 0;
|
||||
Animation.animRunning = false;
|
||||
Reference in New Issue
Block a user