Skip to content

Commit 5aa1c6a

Browse files
author
daniel-lundin
committed
Fixed bug with complete callback for animations with duration 0
1 parent a76f82c commit 5aa1c6a

File tree

8 files changed

+42
-53
lines changed

8 files changed

+42
-53
lines changed

docs/snabbt.min.js

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"snabbt.min.js"
1313
],
1414
"scripts": {
15-
"watch": "watchify -v -t babelify src/main.js -o snabbt.js",
1615
"test": "mocha src/**/*Tests.js --harmony",
1716
"tdd": "mocha src/**/*Tests.js --harmony --watch",
1817
"build": "browserify -s snabbt -t babelify src/main.js -o snabbt.js && uglifyjs --compress --mangle -- snabbt.js > snabbt.min.js",

snabbt.js

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* snabbt.js Version: 0.6.1 Build date: 2015-12-12 (c) 2015 Daniel Lundin @license MIT */
21
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.snabbt = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
32
'use strict';
43
var utils = require('./utils.js');
@@ -17,7 +16,6 @@ function createAnimation(startState, _endState, options, transformProperty) {
1716

1817
var startTime = -1;
1918
var currentTime = 0;
20-
var stopped = false;
2119
var started = false;
2220

2321
// Manual related
@@ -37,18 +35,9 @@ function createAnimation(startState, _endState, options, transformProperty) {
3735
// Public api
3836
return {
3937
options: options,
40-
stop: function stop() {
41-
stopped = true;
42-
},
4338
endState: function endState() {
4439
return _endState;
4540
},
46-
isStopped: function isStopped() {
47-
return stopped;
48-
},
49-
isStarted: function isStarted() {
50-
return started;
51-
},
5241

5342
finish: function finish(callback) {
5443
manual = false;
@@ -74,8 +63,6 @@ function createAnimation(startState, _endState, options, transformProperty) {
7463
},
7564

7665
tick: function tick(time) {
77-
if (stopped) return;
78-
7966
if (manual) {
8067
currentTime = time;
8168
return this.updateCurrentTransform();
@@ -94,7 +81,7 @@ function createAnimation(startState, _endState, options, transformProperty) {
9481
currentTime = time - delay;
9582

9683
var curr = Math.min(Math.max(0.0, currentTime - startTime), duration);
97-
easer.tick(curr / duration);
84+
easer.tick(duration === 0 ? 1 : curr / duration);
9885
this.updateCurrentTransform();
9986
if (options.update) {
10087
options.update(curr / duration);
@@ -129,10 +116,7 @@ function createAnimation(startState, _endState, options, transformProperty) {
129116
},
130117

131118
completed: function completed() {
132-
if (stopped) return true;
133-
if (startTime === 0) {
134-
return false;
135-
}
119+
if (startTime === 0) return false;
136120
return easer.completed();
137121
},
138122

@@ -155,7 +139,6 @@ function createAttentionAnimation(_options) {
155139
_options.initialVelocity = 0.1;
156140
_options.equilibriumPosition = 0;
157141
var spring = easing.createSpringEasing(_options);
158-
var stopped = false;
159142
var tweenPosition = movement.position;
160143
var tweenRotation = movement.rotation;
161144
var tweenRotationPost = movement.rotationPost;
@@ -175,15 +158,8 @@ function createAttentionAnimation(_options) {
175158
options: function options() {
176159
return _options;
177160
},
178-
stop: function stop() {
179-
stopped = true;
180-
},
181-
isStopped: function isStopped() {
182-
return stopped;
183-
},
184161

185162
tick: function tick() {
186-
if (stopped) return;
187163
if (spring.equilibrium) return;
188164
spring.tick();
189165

@@ -228,7 +204,7 @@ function createAttentionAnimation(_options) {
228204
},
229205

230206
completed: function completed() {
231-
return spring.completed() || stopped;
207+
return spring.completed();
232208
},
233209

234210
restart: function restart() {
@@ -404,12 +380,10 @@ var Engine = {
404380

405381
this.archiveCompletedAnimations();
406382

407-
this.scheduleNextFrame();
383+
if (this.runningAnimations.length > 0) this.scheduleNextFrame();
408384
},
409385

410386
stepAnimation: function stepAnimation(element, animation, time) {
411-
if (animation.isStopped()) return;
412-
413387
animation.tick(time);
414388
animation.updateElement(element);
415389
},
@@ -499,15 +473,29 @@ var Engine = {
499473
return animation;
500474
},
501475

476+
stopAnimation: function stopAnimation(element) {
477+
var stoppedAnimation = this.runningAnimations.filter(function (animation) {
478+
return animation[0] === element;
479+
});
480+
this.runningAnimations = this.runningAnimations.filter(function (animation) {
481+
return animation[0] !== element;
482+
});
483+
Array.prototype.push.apply(this.completedAnimations, stoppedAnimation);
484+
},
485+
502486
initializeAnimation: function initializeAnimation(element, arg2, arg3) {
503-
var animation;
487+
var animation = undefined;
504488
if (arg2 === 'attention') {
505489
animation = this.createAttentionAnimation(element, arg3);
490+
} else if (arg2 === 'stop') {
491+
return this.stopAnimation(element);
506492
} else {
507493
animation = this.createAnimation(element, arg2);
508494
}
509495
var chainer = this.createChainer();
510496

497+
animation.updateElement(element, true);
498+
511499
this.runningAnimations.push([element, animation, chainer]);
512500
this.scheduleNextFrame();
513501

@@ -519,14 +507,13 @@ var Engine = {
519507
return element === animation[0];
520508
});
521509
if (match) {
522-
match[1].stop();
523510
return match[1].getCurrentState();
524511
}
525512
match = this.completedAnimations.find(function (animation) {
526513
return element === animation[0];
527514
});
528515
if (match) {
529-
return match[1].endState();
516+
return match[1].getCurrentState();
530517
}
531518
},
532519

snabbt.min.js

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/animation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function createAnimation(startState, endState, options, transformProperty) {
8181
currentTime = time - delay;
8282

8383
var curr = Math.min(Math.max(0.0, currentTime - startTime), duration);
84-
easer.tick(curr / duration);
84+
easer.tick(duration === 0 ? 1 : curr / duration);
8585
this.updateCurrentTransform();
8686
if (options.update) {
8787
options.update(curr / duration);

src/engine.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const Engine = {
4242

4343
this.archiveCompletedAnimations();
4444

45-
this.scheduleNextFrame();
45+
if (this.runningAnimations.length > 0)
46+
this.scheduleNextFrame();
4647
},
4748

4849
stepAnimation(element, animation, time) {
@@ -147,6 +148,8 @@ const Engine = {
147148
}
148149
const chainer = this.createChainer();
149150

151+
animation.updateElement(element, true);
152+
150153
this.runningAnimations.push([element, animation, chainer]);
151154
this.scheduleNextFrame();
152155

@@ -160,7 +163,7 @@ const Engine = {
160163
}
161164
match = this.completedAnimations.find((animation) => element === animation[0]);
162165
if (match) {
163-
return match[1].endState();
166+
return match[1].getCurrentState();
164167
}
165168
},
166169

src/tests/engineTests.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('Engine', () => {
2020

2121
describe('createAnimation', () => {
2222
const previousState = createState({ position: [1, 2, 3] });
23-
const element = {};
23+
const element = { style: {} };
2424

2525
beforeEach(() => {
2626
sinon.stub(Animation, 'createAnimation');
@@ -61,7 +61,7 @@ describe('Engine', () => {
6161
tick: sinon.stub(),
6262
updateElement: sinon.stub()
6363
};
64-
const element = {};
64+
const element = { style: {} };
6565
const time = 42;
6666
Engine.stepAnimation(element, animation, time);
6767

@@ -182,7 +182,7 @@ describe('Engine', () => {
182182

183183

184184
it('should call createAnimation with states and options', () => {
185-
const element = {};
185+
const element = { style: {} };
186186
const options = {
187187
fromPosition: [-1, -1, -1],
188188
position: [1, 1, 1]
@@ -196,7 +196,7 @@ describe('Engine', () => {
196196
it('should append to runningAnimations', () => {
197197
expect(Engine.runningAnimations.length).to.eql(0);
198198

199-
const element = {};
199+
const element = { style: {} };
200200
const options = {
201201
fromPosition: [-1, -1, -1],
202202
position: [1, 1, 1]
@@ -215,7 +215,7 @@ describe('Engine', () => {
215215
return previousState;
216216
}
217217
};
218-
const element = {};
218+
const element = { style: {} };
219219
Engine.runningAnimations = [[element, previousAnimation, {}]];
220220

221221
const options = {
@@ -228,7 +228,7 @@ describe('Engine', () => {
228228
});
229229

230230
it('should cancel running animations on the same element', () => {
231-
const element = {};
231+
const element = { style: {} };
232232
const animation = {
233233
stop() {},
234234
getCurrentState() {}
@@ -241,7 +241,7 @@ describe('Engine', () => {
241241
});
242242

243243
it('should stop animation with \'stop\' command', () => {
244-
const element = {};
244+
const element = { style: {} };
245245
Engine.completedAnimations = [];
246246
Engine.runningAnimations = [[element, {}, {}]];
247247

@@ -251,17 +251,19 @@ describe('Engine', () => {
251251
expect(Engine.completedAnimations).to.have.length(1);
252252
});
253253

254-
describe('manual animations', () => {
254+
describe('attention animations', () => {
255255
beforeEach(() => {
256-
sinon.stub(Animation, 'createAttentionAnimation');
256+
sinon.stub(Animation, 'createAttentionAnimation').returns({
257+
updateElement() {}
258+
});
257259
});
258260

259261
afterEach(() => {
260262
Animation.createAttentionAnimation.restore();
261263
});
262264

263265
it('should create attention animation', () => {
264-
const element = {};
266+
const element = { style: {} };
265267
const options = {};
266268
Engine.initializeAnimation(element, 'attention', options);
267269

src/tests/mainTests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('main', () => {
5353
};
5454
Engine.createChainer.returns(chainerStub);
5555

56-
var elements = [{}, {}];
56+
var elements = [{ style: {} }, { style: {} }];
5757
var options = {};
5858
snabbt(elements, options)
5959
.snabbt(options);
@@ -68,7 +68,7 @@ describe('main', () => {
6868
callback();
6969
}
7070
});
71-
var elements = [{}, {}];
71+
var elements = [{ style: {} }, { style: {} }];
7272
var options = {};
7373

7474
var callback = sinon.stub();

0 commit comments

Comments
 (0)