Skip to content

Commit abd4e7c

Browse files
author
daniel-lundin
committed
Added update and start callbacks
1 parent 6b1687d commit abd4e7c

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

docs/snabbt.min.js

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

src/animation.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function createAnimation(startState, endState, options) {
1313
currentState.transformOrigin = options.transformOrigin;
1414
currentState.perspective = options.perspective;
1515

16-
var startTime = 0;
16+
var startTime = -1;
1717
var currentTime = 0;
1818
var stopped = false;
1919
var started = false;
@@ -62,7 +62,7 @@ function createAnimation(startState, endState, options) {
6262

6363
restart() {
6464
// Restart timer
65-
startTime = undefined;
65+
startTime = -1;
6666
easer.resetFrom(0);
6767
},
6868

@@ -76,17 +76,23 @@ function createAnimation(startState, endState, options) {
7676
}
7777

7878
// If first tick, set startTime
79-
if (!startTime) {
79+
if (startTime === -1) {
8080
startTime = time;
8181
}
8282

8383
if (time - startTime >= delay) {
84+
if (!started && options.start) {
85+
options.start();
86+
}
8487
started = true;
8588
currentTime = time - delay;
8689

8790
var curr = Math.min(Math.max(0.0, currentTime - startTime), duration);
8891
easer.tick(curr / duration);
8992
this.updateCurrentTransform();
93+
if (options.update) {
94+
options.update(curr / duration);
95+
}
9096
if (this.completed() && manualCallback) {
9197
manualCallback();
9298
}

src/properties.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ function preprocessOptions(options, index, len) {
5151
return options.easing(i, index, len);
5252
};
5353
}
54+
if (utils.isFunction(options.start)) {
55+
clone.start = function() {
56+
return options.start(index, len);
57+
};
58+
}
59+
if (utils.isFunction(options.update)) {
60+
clone.update = function(i) {
61+
return options.update(i, index, len);
62+
};
63+
}
5464

5565
var properties = Object.keys(tweenableProperties).concat(['perspective', 'transformOrigin', 'duration', 'delay']);
5666

src/tests/animationTests.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
'use strict';
22

3-
var sinon = require('sinon');
4-
var expect = require('chai').expect;
5-
var createAnimation = require('../animation.js').createAnimation;
6-
var createState = require('../state.js').createState;
3+
const sinon = require('sinon');
4+
const expect = require('chai').expect;
5+
const createAnimation = require('../animation.js').createAnimation;
6+
const createState = require('../state.js').createState;
77

88

99
describe('animations', () => {
1010

1111
describe('tick', () => {
12-
var animation;
12+
let animation;
13+
let startedCallback;
14+
let updateCallback;
15+
const startState = createState({});
16+
const endState = createState({});
17+
1318
beforeEach(() => {
14-
var startState = createState({});
15-
var endState = createState({});
16-
animation = createAnimation(startState, endState, {});
17-
sinon.spy(animation, 'updateCurrentTransform');
19+
startedCallback = sinon.stub();
20+
updateCallback = sinon.stub();
21+
animation = createAnimation(startState, endState, {
22+
start: startedCallback,
23+
update: updateCallback
24+
});
25+
sinon.stub(animation, 'updateCurrentTransform');
1826
});
1927

2028
afterEach(() => {
@@ -29,6 +37,26 @@ describe('animations', () => {
2937
expect(animation.isStarted()).to.be.ok;
3038
sinon.assert.calledOnce(animation.updateCurrentTransform);
3139
});
40+
41+
it('should call started callback for first tick', () => {
42+
43+
animation.tick(1);
44+
animation.tick(2);
45+
46+
sinon.assert.calledOnce(startedCallback);
47+
});
48+
49+
it('should call update callback for every tick', () => {
50+
51+
animation.tick(0);
52+
animation.tick(250);
53+
animation.tick(500);
54+
55+
sinon.assert.calledThrice(updateCallback);
56+
expect(updateCallback.firstCall.args[0]).to.equal(0);
57+
expect(updateCallback.secondCall.args[0]).to.equal(0.5);
58+
expect(updateCallback.thirdCall.args[0]).to.equal(1);
59+
});
3260
});
3361

3462

0 commit comments

Comments
 (0)