Skip to content

Commit bc5fa45

Browse files
committed
Optimize the execution of the Queue Action.
1 parent b2f602f commit bc5fa45

File tree

3 files changed

+59
-33
lines changed

3 files changed

+59
-33
lines changed

ChangeLog.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## v2.1.0
2+
_`2021.8.18 UTC+8 10:58`_
3+
4+
* Add `smooth` function and set it as default ease.
5+
* Optimize the execution of the `Queue Action`.
6+
* Improve the code comments.
7+
8+
19
## v2.0.3
210
_`2020.2.2 UTC+8 11:34`_
311

MojoJS.animation.js

+49-31
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* Since : 2010-05-16
1111
* Update : 2021-8-17
12-
* Version: 2.1
12+
* Version: 2.1.0
1313
*/
1414

1515
(function(window) {
@@ -32,8 +32,8 @@
3232

3333
// queue configs
3434
this.queConfigs = [];
35-
// current running queue config
36-
this.queConfig = null;
35+
// whether to dequeue the queue action to play
36+
this.isDequeue = true;
3737
// current running actions include shift queue and nonqueued
3838
this.curActions = [];
3939
// whether animation is playing
@@ -58,7 +58,7 @@
5858
/**
5959
* Add animation with config to update array.
6060
*
61-
* @param {Object} animation
61+
* @param {Object} anim
6262
* @param {Object} config
6363
*/
6464
addAnim: function(anim, config) {
@@ -117,16 +117,22 @@
117117
}
118118
}
119119

120+
if (config.isQueue) {
121+
// into queue waiting to play
122+
anim.queConfigs.push(config);
123+
124+
if (anim.isDequeue) {
125+
this.dequeueAction(anim);
126+
}
127+
} else {
128+
// into current to play
129+
anim.curActions.push(this.createAction(anim, config));
130+
}
131+
120132
if (anim.isPlaying === false) {
121133
this.anims.push(anim);
122134
anim.isPlaying = true;
123-
}
124-
125-
config.isQueue ?
126-
// into queue waiting to run
127-
anim.queConfigs.push(config) :
128-
// add action into curActions
129-
anim.curActions.push(this.createAction(anim, config));
135+
}
130136
},
131137

132138
/**
@@ -239,9 +245,8 @@
239245
*
240246
* one action to one config.
241247
*
242-
* @param {Object} animation
248+
* @param {Object} anim
243249
* @param {Object} config
244-
* @return {Object} action
245250
*/
246251
createAction: function(anim, config) {
247252
var
@@ -264,7 +269,7 @@
264269
/**
265270
* Create acton steps by config steps and els current style.
266271
*
267-
* @param {Object} animation
272+
* @param {Object} anim
268273
* @param {Object} action
269274
*/
270275
createSteps: function(anim, action) {
@@ -387,20 +392,12 @@
387392
*/
388393
update: function(deltaTime) {
389394
var
390-
anim, queConfigs, curActions, len, i;
395+
anim, curActions, len, i;
391396

392397
for (i = 0, len = this.anims.length; i < len; ++i) {
393398
anim = this.anims[i];
394-
queConfigs = anim.queConfigs;
395399
curActions = anim.curActions;
396400

397-
if (anim.queConfig === null && queConfigs.length !== 0) {
398-
// get one config from queue
399-
anim.queConfig = queConfigs.shift();
400-
// add queue action into current runnings
401-
curActions.push(this.createAction(anim, anim.queConfig));
402-
}
403-
404401
if (curActions.length > 0) {
405402
this.doActions(anim, curActions, deltaTime);
406403
} else {
@@ -416,11 +413,28 @@
416413

417414
return true;
418415
},
416+
417+
/**
418+
* Dequeue one action to play.
419+
*
420+
* @param {Object} anim
421+
*/
422+
dequeueAction(anim) {
423+
var
424+
queConfigs = anim.queConfigs;
425+
426+
if (queConfigs.length !== 0) {
427+
// get one config from queue into curActions
428+
anim.curActions.push(this.createAction(anim, queConfigs.shift()));
429+
}
430+
431+
anim.isDequeue = false;
432+
},
419433

420434
/**
421435
* Do animation current actions.
422436
*
423-
* @param {Object} animation
437+
* @param {Object} anim
424438
* @param {Array} actions
425439
* @param {Number} deltaTime
426440
*/
@@ -451,12 +465,6 @@
451465
time = 1.0;
452466
actions.splice(j--, 1);
453467
--aLen;
454-
455-
// check whether the action is in queue
456-
if (anim.queConfig === action.config) {
457-
anim.queConfig = null;
458-
}
459-
460468
// action is complete
461469
completes.push(action.config);
462470
}
@@ -491,6 +499,11 @@
491499
for (k = 0, cLen = completes.length; k < cLen; ++k) {
492500
config = completes[k];
493501

502+
if (config.isQueue) {
503+
// need to dequeue action when queue action is complete
504+
anim.isDequeue = true;
505+
}
506+
494507
if (config.complete !== null) {
495508
// do action callback
496509
config.complete.apply(anim, config.args);
@@ -511,6 +524,11 @@
511524
}
512525
}
513526
}
527+
528+
if (anim.isDequeue)
529+
{
530+
this.dequeueAction(anim);
531+
}
514532
},
515533

516534
/**
@@ -610,7 +628,7 @@
610628
* Animate with config.
611629
*
612630
* @param {Object} animStyle
613-
* @return {Object} animation
631+
* @return {Object} anim
614632
*/
615633
animate: function(animStyle) {
616634
var

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## MojoJS-Animation v2.0.3
1+
## MojoJS-Animation v2.1.0
22

33
MojoJS-Animation is a very **lightweight** and **powerful** javascript **Animation** engine.
44

@@ -23,7 +23,7 @@ MojoJS-Animation is licensed under the [MIT License](https://github.com/scottcgi
2323
* Support configurable chained complete callbacks.
2424
* Support fully compatible standard easing effect and more.
2525
```js
26-
linear
26+
linear, smooth,
2727
quadraticIn, quadraticOut, quadraticInOut
2828
cubicIn, cubicOut, cubicInOut
2929
quarticIn, quarticOut, quarticInOut

0 commit comments

Comments
 (0)