Skip to content

Commit

Permalink
Merge pull request #1574 from IcetCode/fix/type-improve
Browse files Browse the repository at this point in the history
fix: type improve
  • Loading branch information
lavrton committed Jun 9, 2023
2 parents 8e620e1 + 217fd86 commit a8959e1
Showing 1 changed file with 30 additions and 50 deletions.
80 changes: 30 additions & 50 deletions src/Animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Layer } from './Layer';
import { IFrame, AnimationFn } from './types';
import { Util } from './Util';

var now = (function (): () => number {
const now = (function (): () => number {
if (glob.performance && glob.performance.now) {
return function () {
return glob.performance.now();
Expand Down Expand Up @@ -61,21 +61,12 @@ export class Animation {
* @param {Konva.Layer|Array} [layers] layer(s) to be redrawn. Can be a layer, an array of layers, or null. Not specifying a node will result in no redraw.
* @return {Konva.Animation} this
*/
setLayers(layers) {
var lays = [];
setLayers(layers:null | Layer | Layer[]) {
let lays: Layer[] = [];
// if passing in no layers
if (!layers) {
lays = [];
} else if (layers.length > 0) {
// if passing in an array of Layers
// NOTE: layers could be an array. for simplicity, I'm just inspecting
// the length property to check for both cases
lays = layers;
} else {
// if passing in a Layer
lays = [layers];
if (layers) {
lays = Array.isArray(layers)? layers : [layers]
}

this.layers = lays;
return this;
}
Expand All @@ -95,13 +86,12 @@ export class Animation {
* @param {Konva.Layer} layer to add
* @return {Bool} true if layer is added to animation, otherwise false
*/
addLayer(layer) {
var layers = this.layers,
len = layers.length,
n;
addLayer(layer: Layer) {
const layers = this.layers
const len = layers.length

// don't add the layer if it already exists
for (n = 0; n < len; n++) {
for (let n = 0; n < len; n++) {
if (layers[n]._id === layer._id) {
return false;
}
Expand All @@ -117,12 +107,11 @@ export class Animation {
* @return {Bool} is animation running?
*/
isRunning() {
var a = Animation,
animations = a.animations,
len = animations.length,
n;
const a = Animation
const animations = a.animations
const len = animations.length

for (n = 0; n < len; n++) {
for (let n = 0; n < len; n++) {
if (animations[n].id === this.id) {
return true;
}
Expand Down Expand Up @@ -152,7 +141,7 @@ export class Animation {
Animation._removeAnimation(this);
return this;
}
_updateFrameObject(time) {
_updateFrameObject(time: number) {
this.frame.timeDiff = time - this.frame.lastTime;
this.frame.lastTime = time;
this.frame.time += this.frame.timeDiff;
Expand All @@ -168,12 +157,11 @@ export class Animation {
this._handleAnimation();
}
static _removeAnimation(anim) {
var id = anim.id,
animations = this.animations,
len = animations.length,
n;
const id = anim.id
const animations = this.animations
const len = animations.length

for (n = 0; n < len; n++) {
for (let n = 0; n < len; n++) {
if (animations[n].id === id) {
this.animations.splice(n, 1);
break;
Expand All @@ -182,17 +170,8 @@ export class Animation {
}

static _runFrames() {
var layerHash = {},
animations = this.animations,
anim,
layers,
func,
n,
i,
layersLen,
layer,
key,
needRedraw;
const layerHash = {}
const animations = this.animations
/*
* loop through all animations and execute animation
* function. if the animation object has specified node,
Expand All @@ -205,15 +184,16 @@ export class Animation {
* the for loop is running, causing a JS error
*/

for (n = 0; n < animations.length; n++) {
anim = animations[n];
layers = anim.layers;
func = anim.func;
for (let n = 0; n < animations.length; n++) {
const anim = animations[n];
const layers = anim.layers;
const func = anim.func;

anim._updateFrameObject(now());
layersLen = layers.length;
const layersLen = layers.length;

// if animation object has a function, execute it
let needRedraw
if (func) {
// allow anim bypassing drawing
needRedraw = func.call(anim, anim.frame) !== false;
Expand All @@ -223,24 +203,24 @@ export class Animation {
if (!needRedraw) {
continue;
}
for (i = 0; i < layersLen; i++) {
layer = layers[i];
for (let i = 0; i < layersLen; i++) {
const layer = layers[i];

if (layer._id !== undefined) {
layerHash[layer._id] = layer;
}
}
}

for (key in layerHash) {
for (let key in layerHash) {
if (!layerHash.hasOwnProperty(key)) {
continue;
}
layerHash[key].batchDraw();
}
}
static _animationLoop() {
var Anim = Animation;
const Anim = Animation;
if (Anim.animations.length) {
Anim._runFrames();
Util.requestAnimFrame(Anim._animationLoop);
Expand Down

0 comments on commit a8959e1

Please sign in to comment.