Skip to content

Commit

Permalink
tween: add play/reverse state hatch
Browse files Browse the repository at this point in the history
  • Loading branch information
legomushroom committed Dec 22, 2015
1 parent 23d62a0 commit 5219bdb
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 25 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mojs",
"description": "motion graphics toolbelt for the web",
"version": "0.163.2",
"version": "0.163.3",
"license": "MIT",
"homepage": "https://github.com/legomushroom/mojs",
"authors": [
Expand Down
16 changes: 13 additions & 3 deletions build/mo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2415,7 +2415,7 @@
/***/ function(module, exports, __webpack_require__) {

var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;window.mojs = {
revision: '0.163.2',
revision: '0.163.3',
isDebug: true,
helpers: __webpack_require__(2),
Bit: __webpack_require__(3),
Expand Down Expand Up @@ -3583,6 +3583,9 @@
*/
value: function play() {
var shift = arguments[0] === undefined ? 0 : arguments[0];
if (this._state === "play" && this._isRunning) {
return false;
}
// if was playing reverse and paused or playing reverse right now,
// flip the time progress in repeatTime bounds
var isPausedReverse = this._state === "pause" && this._prevState === "reverse";
Expand All @@ -3607,8 +3610,15 @@
*/
value: function reverse() {
var shift = arguments[0] === undefined ? 0 : arguments[0];
this.o.isIt && console.log(this._isRunning);
if (this._state === "reverse" && this._isRunning) {
return false;
}
// flip time progress in repeatTime bounds
this._progressTime = this._props.repeatTime - this._progressTime;
var isPlayPaused = this._state === "pause" && this._prevState === "play";
if (isPlayPaused || this._state === "play") {
this._progressTime = this._props.repeatTime - this._progressTime;
}
// play reversed
this._props.isReversed = true;
this._subPlay(shift);
Expand Down Expand Up @@ -3910,7 +3920,7 @@
}

this._prevTime = time;
return this._isCompleted;
return time >= p.endTime || time <= startPoint;
},
writable: true,
enumerable: true,
Expand Down
8 changes: 4 additions & 4 deletions build/mo.min.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions js/mojs.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

window.mojs =
revision: '0.163.2', isDebug: true
revision: '0.163.3', isDebug: true
helpers : require './h'
Bit : require './shapes/bit'
bitsMap : require './shapes/bitsMap'
Expand Down Expand Up @@ -33,14 +33,15 @@ mojs.delta = mojs.h.delta
# pause = document.querySelector('#js-pause')
# stop = document.querySelector('#js-stop')

# tm = new mojs.Timeline isIt: 1
# tm = new mojs.Timeline
# tw = new mojs.Tween
# isIt2: 1
# # repeat: 3
# # yoyo: true
# # isIt: true
# # delay: 500
# # speed: .5
# duration: 10000
# duration: 1000
# # onStart:-> console.log "START"
# # onComplete:-> console.log "COMPLETE"
# # onRepeatStart:-> console.log "REPEAT START"
Expand Down
10 changes: 8 additions & 2 deletions js/tween/tween.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var Tween = class Tween {
@return {Object} Self.
*/
play (shift = 0) {
if ( this._state === 'play' && this._isRunning ) { return false; }
// if was playing reverse and paused or playing reverse right now,
// flip the time progress in repeatTime bounds
var isPausedReverse = this._state === 'pause' && this._prevState === 'reverse';
Expand All @@ -28,8 +29,13 @@ var Tween = class Tween {
@return {Object} Self.
*/
reverse (shift = 0) {
this.o.isIt && console.log(this._isRunning);
if ( this._state === 'reverse' && this._isRunning) { return false; }
// flip time progress in repeatTime bounds
this._progressTime = this._props.repeatTime - this._progressTime;
var isPlayPaused = this._state === 'pause' && this._prevState === 'play';
if ( isPlayPaused || this._state === 'play' ) {
this._progressTime = this._props.repeatTime - this._progressTime;
}
// play reversed
this._props.isReversed = true;
this._subPlay( shift );
Expand Down Expand Up @@ -289,7 +295,7 @@ var Tween = class Tween {
}

this._prevTime = time;
return this._isCompleted;
return (time >= p.endTime) || (time <= startPoint);
}
/*
Method to handle tween's progress in active area.
Expand Down
4 changes: 1 addition & 3 deletions js/tween/tweener.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ class Tweener {
this._startLoop();
}

removeAll() {
this.tweens.length = 0;
}
removeAll() { this.tweens.length = 0; }

remove(tween) {
var index = (typeof tween === 'number')
Expand Down
2 changes: 1 addition & 1 deletion lib/mojs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function() {
window.mojs = {
revision: '0.163.2',
revision: '0.163.3',
isDebug: true,
helpers: require('./h'),
Bit: require('./shapes/bit'),
Expand Down
14 changes: 12 additions & 2 deletions lib/tween/tween.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ var Tween = (function () {
value: function play() {
var shift = arguments.length <= 0 || arguments[0] === undefined ? 0 : arguments[0];

if (this._state === 'play' && this._isRunning) {
return false;
}
// if was playing reverse and paused or playing reverse right now,
// flip the time progress in repeatTime bounds
var isPausedReverse = this._state === 'pause' && this._prevState === 'reverse';
Expand All @@ -58,8 +61,15 @@ var Tween = (function () {
value: function reverse() {
var shift = arguments.length <= 0 || arguments[0] === undefined ? 0 : arguments[0];

this.o.isIt && console.log(this._isRunning);
if (this._state === 'reverse' && this._isRunning) {
return false;
}
// flip time progress in repeatTime bounds
this._progressTime = this._props.repeatTime - this._progressTime;
var isPlayPaused = this._state === 'pause' && this._prevState === 'play';
if (isPlayPaused || this._state === 'play') {
this._progressTime = this._props.repeatTime - this._progressTime;
}
// play reversed
this._props.isReversed = true;
this._subPlay(shift);
Expand Down Expand Up @@ -364,7 +374,7 @@ var Tween = (function () {
}

this._prevTime = time;
return this._isCompleted;
return time >= p.endTime || time <= startPoint;
}

/*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mo-js",
"description": "motion graphics toolbelt for the web",
"version": "0.163.2",
"version": "0.163.3",
"license": "MIT",
"private": false,
"scripts": {
Expand Down
43 changes: 42 additions & 1 deletion spec/tween/tween.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ describe 'Tween ->', ->
expect(t._isCompleted).toBe true
expect(t._isRepeatCompleted).toBe true
expect(returnValue).toBe true
it 'should return true on the start', ->
t = new Tween(duration: 1000, delay: 200, isIt: true, onUpdate:(p)-> console.log(p) )
t._setStartTime()
t._update t._props.startTime + t._props.duration/2
returnValue = t._update t._props.startTime - 1000
expect(t.progress).toBeCloseTo 0, 5
expect(returnValue).toBe true
it 'should not call update method if timeline isnt active "-"', ->
t = new Tween(duration: 1000, onUpdate:->)
t._setStartTime()
Expand Down Expand Up @@ -4490,6 +4497,23 @@ describe 'Tween ->', ->
.reverse()
.play()
expect(t._progressTime).toBe progress
it 'should return immediately if already playing',->
t = new Tween duration: 1000
t.play()
spyOn t, '_subPlay'
t.play()
expect(t._subPlay).not.toHaveBeenCalled()

it 'should run if already playing but ended', (dfr)->
duration = 50
t = new Tween duration: duration
t.play()
setTimeout ->
spyOn t, '_subPlay'
t.play()
expect(t._subPlay).toHaveBeenCalled()
dfr()
, 2*duration

describe 'reverse method ->', ->
it 'should set _state to "reverse"',->
Expand Down Expand Up @@ -4517,7 +4541,7 @@ describe 'Tween ->', ->
t.setProgress(.75)
progress = t._progressTime
t.reverse()
expect(t._progressTime).toBe t._props.repeatTime - progress
expect(t._progressTime).toBe progress
it 'should recalc _progressTime if previous state was "play"',->
duration = 1000
t = new Tween duration: duration
Expand All @@ -4526,6 +4550,23 @@ describe 'Tween ->', ->
t .play()
.reverse()
expect(t._progressTime).toBe t._props.repeatTime - progress
it 'should return immediately if already reversing',->
t = new Tween duration: 1000
t.reverse()
spyOn t, '_subPlay'
t.reverse()
expect(t._subPlay).not.toHaveBeenCalled()

it 'should run if already reversing but ended', (dfr)->
duration = 50
t = new Tween duration: duration
t.reverse()
setTimeout ->
spyOn t, '_subPlay'
t.reverse()
expect(t._subPlay).toHaveBeenCalled()
dfr()
, 2*duration

describe 'stop method', ->
it 'should call removeFromTweener method with self',->
Expand Down
70 changes: 67 additions & 3 deletions spec/tween/tween.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,22 @@
expect(t._isRepeatCompleted).toBe(true);
return expect(returnValue).toBe(true);
});
it('should return true on the start', function() {
var returnValue, t;
t = new Tween({
duration: 1000,
delay: 200,
isIt: true,
onUpdate: function(p) {
return console.log(p);
}
});
t._setStartTime();
t._update(t._props.startTime + t._props.duration / 2);
returnValue = t._update(t._props.startTime - 1000);
expect(t.progress).toBeCloseTo(0, 5);
return expect(returnValue).toBe(true);
});
it('should not call update method if timeline isnt active "-"', function() {
var t;
t = new Tween({
Expand Down Expand Up @@ -3943,7 +3959,7 @@
t.play().reverse().pause().play();
return expect(t._progressTime).toBe(progress);
});
return it('should recalc _progressTime if previous state was "reverse"', function() {
it('should recalc _progressTime if previous state was "reverse"', function() {
var duration, progress, t;
duration = 1000;
t = new Tween({
Expand All @@ -3954,6 +3970,30 @@
t.play().reverse().play();
return expect(t._progressTime).toBe(progress);
});
it('should return immediately if already playing', function() {
var t;
t = new Tween({
duration: 1000
});
t.play();
spyOn(t, '_subPlay');
t.play();
return expect(t._subPlay).not.toHaveBeenCalled();
});
return it('should run if already playing but ended', function(dfr) {
var duration, t;
duration = 50;
t = new Tween({
duration: duration
});
t.play();
return setTimeout(function() {
spyOn(t, '_subPlay');
t.play();
expect(t._subPlay).toHaveBeenCalled();
return dfr();
}, 2 * duration);
});
});
describe('reverse method ->', function() {
it('should set _state to "reverse"', function() {
Expand Down Expand Up @@ -3991,9 +4031,9 @@
t.setProgress(.75);
progress = t._progressTime;
t.reverse();
return expect(t._progressTime).toBe(t._props.repeatTime - progress);
return expect(t._progressTime).toBe(progress);
});
return it('should recalc _progressTime if previous state was "play"', function() {
it('should recalc _progressTime if previous state was "play"', function() {
var duration, progress, t;
duration = 1000;
t = new Tween({
Expand All @@ -4004,6 +4044,30 @@
t.play().reverse();
return expect(t._progressTime).toBe(t._props.repeatTime - progress);
});
it('should return immediately if already reversing', function() {
var t;
t = new Tween({
duration: 1000
});
t.reverse();
spyOn(t, '_subPlay');
t.reverse();
return expect(t._subPlay).not.toHaveBeenCalled();
});
return it('should run if already reversing but ended', function(dfr) {
var duration, t;
duration = 50;
t = new Tween({
duration: duration
});
t.reverse();
return setTimeout(function() {
spyOn(t, '_subPlay');
t.reverse();
expect(t._subPlay).toHaveBeenCalled();
return dfr();
}, 2 * duration);
});
});
describe('stop method', function() {
it('should call removeFromTweener method with self', function() {
Expand Down
1 change: 0 additions & 1 deletion todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
### TWEEN + TIMELINE:
- add total onUpdate
- nested speed tests
- play/pause and others should not run if already running

- fix coverage
- release
Expand Down

0 comments on commit 5219bdb

Please sign in to comment.