Skip to content

Commit

Permalink
runable: fix _transformHistoryRecord method algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
legomushroom committed Mar 17, 2016
1 parent b271918 commit 3c15ecc
Show file tree
Hide file tree
Showing 17 changed files with 474 additions and 230 deletions.
2 changes: 1 addition & 1 deletion bower.json
@@ -1,7 +1,7 @@
{
"name": "mojs",
"description": "motion graphics toolbelt for the web",
"version": "0.188.0",
"version": "0.189.0",
"license": "MIT",
"homepage": "https://github.com/legomushroom/mojs",
"authors": [
Expand Down
90 changes: 44 additions & 46 deletions build/mo.js
Expand Up @@ -3622,8 +3622,11 @@
key: '_transformHistoryFor',
value: function _transformHistoryFor(key, value) {
for (var i = 0; i < this._history.length; i++) {
if (this._transformHistoryRecord(i, key, value)) {
break; // break if no further history modifications needed
if (value = this._transformHistoryRecord(i, key, value)) {
// break if no further history modifications needed
if (value == null) {
break;
}
}
}
}
Expand All @@ -3638,39 +3641,31 @@

}, {
key: '_transformHistoryRecord',
value: function _transformHistoryRecord(index, key, value) {
value: function _transformHistoryRecord(index, key, newValue) {
var currRecord = this._history[index],
prevRecord = this._history[index - 1],
nextRecord = this._history[index + 1],
propertyValue = currRecord[key];

if (this._isDelta(value)) {
// if previous history record have been already overriden
// with the delta, copy only the end property to the start
if (prevRecord && prevRecord[key] === value) {
var prevEnd = _h2.default.getDeltaEnd(prevRecord[key]);
currRecord[key] = (0, _defineProperty3.default)({}, prevEnd, _h2.default.getDeltaEnd(propertyValue));
return true;
} // else go to very end of this function
// if new value is delta
oldValue = currRecord[key];

// if index is 0 - always save the newValue
// and return non-delta for subsequent modifications
if (index === 0) {
currRecord[key] = newValue;
return this._isDelta(newValue) ? _h2.default.getDeltaEnd(newValue) : newValue;
} else {
// if property value is delta - rewrite it's start
// and notify parent to stop hitory modifications
if (this._isDelta(propertyValue)) {
currRecord[key] = (0, _defineProperty3.default)({}, value, _h2.default.getDeltaEnd(propertyValue));
return true;
// both are not deltas and further in the chain
} else {
currRecord[key] = value;
// if next record isn't delta - we should always override it
// so do not notify parent
if (nextRecord && !this._isDelta(nextRecord[key])) {
// notify that no modifications needed in the next record
return nextRecord[key] !== propertyValue;
}
} // else go to very end of this function
// if was delta and came none-deltta - rewrite
// the start of the delta and stop
if (this._isDelta(oldValue)) {
currRecord[key] = (0, _defineProperty3.default)({}, newValue, _h2.default.getDeltaEnd(oldValue));
return null;
} else {
// if the old value is not delta and the new one is
currRecord[key] = newValue;
// if the next item has the same value - return the
// item for subsequent modifications or stop
return nextRecord && nextRecord[key] === oldValue ? newValue : null;
}
currRecord[key] = value;
}
}
/*
Method to tune new option on run.
Expand Down Expand Up @@ -7436,7 +7431,7 @@
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

window.mojs = {
revision: '0.188.0', isDebug: true, helpers: _h2.default,
revision: '0.189.0', isDebug: true, helpers: _h2.default,
Transit: _transit2.default, Swirl: _swirl2.default, Burst: _burst2.default, stagger: _stagger2.default, Spriter: _spriter2.default, MotionPath: _motionPath2.default,
Tween: _tween2.default, Timeline: _timeline2.default, Tweenable: _tweenable2.default, Thenable: _thenable2.default, Runable: _runable2.default, Module: _module2.default,
tweener: _tweener2.default, easing: _easing2.default, shapesMap: _shapesMap2.default
Expand Down Expand Up @@ -7485,23 +7480,26 @@
angle: 90
});

// speed: 1
// opacity: 0
console.log(tr._history[0].stroke);
console.log(tr._history[1].stroke);

var playEl = document.querySelector('#js-play'),
rangeSliderEl = document.querySelector('#js-range-slider');
playEl.addEventListener('click', function () {
tr.run({ stroke: 'red' });
console.log(tr._history[0].stroke);
console.log(tr._history[1].stroke);
});
// console.log(tr._history[0].stroke);
// console.log(tr._history[1].stroke);
// tr.run({ stroke: 'red' });
// console.log(tr._history[0].stroke);
// console.log(tr._history[1].stroke);

rangeSliderEl.addEventListener('input', function () {
tr.setProgress(rangeSliderEl.value / 1000);
});
// var playEl = document.querySelector('#js-play'),
// rangeSliderEl = document.querySelector('#js-range-slider');
// playEl.addEventListener('click', function () {
// tr.run({ stroke: 'red' });
// console.log(tr._history[0].stroke);
// console.log(tr._history[1].stroke);
// });

// rangeSliderEl.addEventListener('input', function () {
// tr.setProgress( rangeSliderEl.value/1000 );
// });

// speed: 1
// opacity: 0
mojs.h = mojs.helpers;
mojs.delta = mojs.h.delta;

Expand Down
10 changes: 5 additions & 5 deletions build/mo.min.js

Large diffs are not rendered by default.

29 changes: 16 additions & 13 deletions js/mojs.babel.js
Expand Up @@ -17,7 +17,7 @@ import tweener from './tween/tweener';
import easing from './easing/easing';

window.mojs = {
revision: '0.188.0', isDebug: true, helpers: h,
revision: '0.189.0', isDebug: true, helpers: h,
Transit, Swirl, Burst, stagger, Spriter, MotionPath,
Tween, Timeline, Tweenable, Thenable, Runable, Module,
tweener, easing, shapesMap
Expand Down Expand Up @@ -68,20 +68,23 @@ var tr = new mojs.Transit({
// opacity: 0
});

console.log(tr._history[0].stroke);
console.log(tr._history[1].stroke);
// console.log(tr._history[0].stroke);
// console.log(tr._history[1].stroke);
// tr.run({ stroke: 'red' });
// console.log(tr._history[0].stroke);
// console.log(tr._history[1].stroke);

var playEl = document.querySelector('#js-play'),
rangeSliderEl = document.querySelector('#js-range-slider');
playEl.addEventListener('click', function () {
tr.run({ stroke: 'red' });
console.log(tr._history[0].stroke);
console.log(tr._history[1].stroke);
});
// var playEl = document.querySelector('#js-play'),
// rangeSliderEl = document.querySelector('#js-range-slider');
// playEl.addEventListener('click', function () {
// tr.run({ stroke: 'red' });
// console.log(tr._history[0].stroke);
// console.log(tr._history[1].stroke);
// });

rangeSliderEl.addEventListener('input', function () {
tr.setProgress( rangeSliderEl.value/1000 );
});
// rangeSliderEl.addEventListener('input', function () {
// tr.setProgress( rangeSliderEl.value/1000 );
// });

mojs.h = mojs.helpers;
mojs.delta = mojs.h.delta;
Expand Down
53 changes: 24 additions & 29 deletions js/runable.babel.js
Expand Up @@ -45,8 +45,9 @@ class Runable extends Thenable {
*/
_transformHistoryFor ( key, value ) {
for (var i = 0; i < this._history.length; i++ ) {
if ( this._transformHistoryRecord( i, key, value ) ) {
break; // break if no further history modifications needed
if ( value = this._transformHistoryRecord( i, key, value ) ) {
// break if no further history modifications needed
if ( value == null ) { break; }
}
}
}
Expand All @@ -58,39 +59,33 @@ class Runable extends Thenable {
@returns {Boolean} Returns true if no further
history modifications is needed.
*/
_transformHistoryRecord ( index, key, value ) {
_transformHistoryRecord ( index, key, newValue ) {
var currRecord = this._history[index],
prevRecord = this._history[index-1],
nextRecord = this._history[index+1],
propertyValue = currRecord[key];

if ( this._isDelta(value) ) {
// if previous history record have been already overriden
// with the delta, copy only the end property to the start
if (prevRecord && prevRecord[key] === value) {
var prevEnd = h.getDeltaEnd(prevRecord[key]);
currRecord[key] = { [prevEnd]: h.getDeltaEnd(propertyValue) }
return true;
} // else go to very end of this function
// if new value is delta
oldValue = currRecord[key];

// if index is 0 - always save the newValue
// and return non-delta for subsequent modifications
if ( index === 0 ) {
currRecord[key] = newValue;
return ( this._isDelta(newValue) ) ? h.getDeltaEnd(newValue) : newValue;
} else {
// if property value is delta - rewrite it's start
// and notify parent to stop hitory modifications
if ( this._isDelta(propertyValue) ) {
currRecord[key] = { [value] : h.getDeltaEnd(propertyValue) };
return true;
// both are not deltas and further in the chain
// if was delta and came none-deltta - rewrite
// the start of the delta and stop
if ( this._isDelta( oldValue ) ) {
currRecord[key] = { [newValue] : h.getDeltaEnd(oldValue) };
return null;
} else {
currRecord[key] = value;
// if next record isn't delta - we should always override it
// so do not notify parent
if (nextRecord && !this._isDelta(nextRecord[key])) {
// notify that no modifications needed in the next record
return ( nextRecord[key] !== propertyValue );
}
}// else go to very end of this function
// if the old value is not delta and the new one is
currRecord[key] = newValue;
// if the next item has the same value - return the
// item for subsequent modifications or stop
return ( nextRecord && nextRecord[key] === oldValue )
? newValue : null;
}
}
currRecord[key] = value;

}
/*
Method to tune new option on run.
Expand Down
2 changes: 1 addition & 1 deletion lib/burst.js
Expand Up @@ -447,7 +447,7 @@ var Burst = (function (_Transit) {
// it will fallback to default 0 value
var option = this._getOption(len),
ref;
// !o.childOptions?.angle? && ( o.angleShift == null )

if (((ref = o.childOptions) != null ? ref.angle : void 0) == null && o.angleShift == null) {
option.angle = this.transits[len].o.angle;
}
Expand Down
30 changes: 25 additions & 5 deletions lib/module.js
Expand Up @@ -51,7 +51,7 @@ var Module = (function () {
value: function _vars() {
this._index = this._o.index || 0;
this._progress = 0;
this._strokeBuffer = [];
this._strokeDasharrayBuffer = [];
}

/*
Expand Down Expand Up @@ -233,6 +233,7 @@ var Module = (function () {

/*
Method to extend module defaults with passed options.
Saves the result to _props.
@param {Object} Optional object to extend defaults with.
*/
}, {
Expand Down Expand Up @@ -281,17 +282,16 @@ var Module = (function () {
}, {
key: '_calcCurrentProps',
value: function _calcCurrentProps(p) {
var stroke;
for (var key in this._deltas) {
var value = this._deltas[key];
if (value.type === 'array') {
stroke = [];
this._strokeDasharrayBuffer.length = 0;
for (var i = 0; i < value.delta.length; i++) {
var item = value.delta[i],
dash = value.start[i].value + p * item.value;
stroke.push({ value: dash, unit: item.unit });
this._strokeDasharrayBuffer.push({ value: dash, unit: item.unit });
}
this._props[key] = stroke;
this._props[key] = this._strokeDasharrayBuffer;
} else if (value.type === 'number') {
this._props[key] = value.start + value.delta * p;
} else if (value.type === 'unit') {
Expand All @@ -317,6 +317,26 @@ var Module = (function () {
this._progress = progress;
this._calcCurrentProps(progress);
}

/*
Method to override callback for controll pupropes.
@private
@param {String} Callback name.
@parma {Function} Method to call
*/
}, {
key: '_overrideCallback',
value: function _overrideCallback(name, fun) {
var callback = this._o[name],
isCallback = callback && typeof callback === 'function';

this._o[name] = function () {
// call overriden callback if it exists
isCallback && callback.apply(this, arguments);
// call the passed cleanup function
fun.apply(this, arguments);
};
}
}]);

return Module;
Expand Down

0 comments on commit 3c15ecc

Please sign in to comment.