UNPKG

konva

Version:

<p align="center"> <img src="https://raw.githubusercontent.com/konvajs/konvajs.github.io/master/apple-touch-icon-180x180.png" alt="Konva logo" height="180" /> </p>

145 lines (144 loc) 4.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Global_1 = require("./Global"); 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(); }; })(); var Animation = (function () { function Animation(func, layers) { this.id = Animation.animIdCounter++; this.frame = { time: 0, timeDiff: 0, lastTime: now(), frameRate: 0 }; this.func = func; this.setLayers(layers); } Animation.prototype.setLayers = function (layers) { var lays = []; if (!layers) { lays = []; } else if (layers.length > 0) { lays = layers; } else { lays = [layers]; } this.layers = lays; return this; }; Animation.prototype.getLayers = function () { return this.layers; }; Animation.prototype.addLayer = function (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; }; Animation.prototype.isRunning = function () { 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; }; Animation.prototype.start = function () { this.stop(); this.frame.timeDiff = 0; this.frame.lastTime = now(); Animation._addAnimation(this); return this; }; Animation.prototype.stop = function () { Animation._removeAnimation(this); return this; }; Animation.prototype._updateFrameObject = function (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; }; Animation._addAnimation = function (anim) { this.animations.push(anim); this._handleAnimation(); }; Animation._removeAnimation = function (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; } } }; Animation._runFrames = function () { 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].draw(); } }; Animation._animationLoop = function () { var Anim = Animation; if (Anim.animations.length) { Anim._runFrames(); requestAnimationFrame(Anim._animationLoop); } else { Anim.animRunning = false; } }; Animation._handleAnimation = function () { if (!this.animRunning) { this.animRunning = true; requestAnimationFrame(this._animationLoop); } }; Animation.animations = []; Animation.animIdCounter = 0; Animation.animRunning = false; return Animation; }()); exports.Animation = Animation;