Skip to content

Commit dd42469

Browse files
committed
Bounce.js 0.8.2
1 parent 8a57c4e commit dd42469

3 files changed

Lines changed: 167 additions & 29 deletions

File tree

bounce.js

Lines changed: 163 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Bounce.js 0.8.0
2+
* Bounce.js 0.8.2
33
* MIT license
44
*/
55
!function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Bounce=e()}}(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);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.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(_dereq_,module,exports){
@@ -82,7 +82,7 @@ Component = (function() {
8282
module.exports = Component;
8383

8484

85-
},{"../easing/bounce":6,"../easing/hardbounce":7,"../easing/hardsway":8,"../easing/sway":10,"../math/matrix4d":12}],2:[function(_dereq_,module,exports){
85+
},{"../easing/bounce":6,"../easing/hardbounce":7,"../easing/hardsway":8,"../easing/sway":10,"../math/matrix4d":13}],2:[function(_dereq_,module,exports){
8686
var Component, Matrix4D, Rotate, Vector2D,
8787
__hasProp = {}.hasOwnProperty,
8888
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
@@ -127,7 +127,7 @@ Rotate = (function(_super) {
127127
module.exports = Rotate;
128128

129129

130-
},{"../math/matrix4d":12,"../math/vector2d":13,"./index":1}],3:[function(_dereq_,module,exports){
130+
},{"../math/matrix4d":13,"../math/vector2d":14,"./index":1}],3:[function(_dereq_,module,exports){
131131
var Component, Matrix4D, Scale, Vector2D,
132132
__hasProp = {}.hasOwnProperty,
133133
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
@@ -178,7 +178,7 @@ Scale = (function(_super) {
178178
module.exports = Scale;
179179

180180

181-
},{"../math/matrix4d":12,"../math/vector2d":13,"./index":1}],4:[function(_dereq_,module,exports){
181+
},{"../math/matrix4d":13,"../math/vector2d":14,"./index":1}],4:[function(_dereq_,module,exports){
182182
var Component, Matrix4D, Skew, Vector2D,
183183
__hasProp = {}.hasOwnProperty,
184184
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
@@ -232,7 +232,7 @@ Skew = (function(_super) {
232232
module.exports = Skew;
233233

234234

235-
},{"../math/matrix4d":12,"../math/vector2d":13,"./index":1}],5:[function(_dereq_,module,exports){
235+
},{"../math/matrix4d":13,"../math/vector2d":14,"./index":1}],5:[function(_dereq_,module,exports){
236236
var Component, Matrix4D, Translate, Vector2D,
237237
__hasProp = {}.hasOwnProperty,
238238
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
@@ -283,7 +283,7 @@ Translate = (function(_super) {
283283
module.exports = Translate;
284284

285285

286-
},{"../math/matrix4d":12,"../math/vector2d":13,"./index":1}],6:[function(_dereq_,module,exports){
286+
},{"../math/matrix4d":13,"../math/vector2d":14,"./index":1}],6:[function(_dereq_,module,exports){
287287
var BounceEasing, Easing,
288288
__hasProp = {}.hasOwnProperty,
289289
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
@@ -312,7 +312,7 @@ BounceEasing = (function(_super) {
312312
this.alpha = this.stiffness / 100;
313313
threshold = 0.005 / Math.pow(10, this.stiffness);
314314
this.limit = Math.floor(Math.log(threshold) / -this.alpha);
315-
this.omega = this.bounces * Math.PI / this.limit;
315+
this.omega = this.calculateOmega(this.bounces, this.limit);
316316
}
317317

318318
BounceEasing.prototype.calculate = function(ratio) {
@@ -324,6 +324,10 @@ BounceEasing = (function(_super) {
324324
return 1 - this.exponent(t) * this.oscillation(t);
325325
};
326326

327+
BounceEasing.prototype.calculateOmega = function(bounces, limit) {
328+
return (this.bounces + 0.5) * Math.PI / this.limit;
329+
};
330+
327331
BounceEasing.prototype.exponent = function(t) {
328332
return Math.pow(Math.E, -this.alpha * t);
329333
};
@@ -397,7 +401,9 @@ module.exports = HardSwayEasing;
397401

398402

399403
},{"./sway":10}],9:[function(_dereq_,module,exports){
400-
var Easing;
404+
var Easing, MathHelpers;
405+
406+
MathHelpers = _dereq_("../math/helpers");
401407

402408
Easing = (function() {
403409
function Easing() {}
@@ -410,14 +416,61 @@ Easing = (function() {
410416
return {};
411417
};
412418

419+
Easing.prototype.findOptimalKeyPoints = function(threshold, resolution) {
420+
var area, halfway, i, keyPoint, keyPoints, loops, result, values;
421+
if (threshold == null) {
422+
threshold = 1.0;
423+
}
424+
if (resolution == null) {
425+
resolution = 1000;
426+
}
427+
keyPoints = [0];
428+
values = (function() {
429+
var _i, _results;
430+
_results = [];
431+
for (i = _i = 0; 0 <= resolution ? _i < resolution : _i > resolution; i = 0 <= resolution ? ++_i : --_i) {
432+
_results.push(this.calculate(i / resolution));
433+
}
434+
return _results;
435+
}).call(this);
436+
keyPoints = keyPoints.concat(MathHelpers.findTurningPoints(values));
437+
keyPoints.push(resolution - 1);
438+
i = 0;
439+
loops = 1000;
440+
while (loops--) {
441+
if (i === keyPoints.length - 1) {
442+
break;
443+
}
444+
area = MathHelpers.areaBetweenLineAndCurve(values, keyPoints[i], keyPoints[i + 1]);
445+
if (area <= threshold) {
446+
i++;
447+
} else {
448+
halfway = Math.round(keyPoints[i] + (keyPoints[i + 1] - keyPoints[i]) / 2);
449+
keyPoints.splice(i + 1, 0, halfway);
450+
}
451+
}
452+
if (loops === 0) {
453+
return [];
454+
}
455+
return result = (function() {
456+
var _i, _len, _results;
457+
_results = [];
458+
for (_i = 0, _len = keyPoints.length; _i < _len; _i++) {
459+
keyPoint = keyPoints[_i];
460+
_results.push(keyPoint / (resolution - 1));
461+
}
462+
return _results;
463+
})();
464+
};
465+
413466
return Easing;
414467

415468
})();
416469

417470
module.exports = Easing;
418471

419472

420-
},{}],10:[function(_dereq_,module,exports){
473+
},{"../math/helpers":12}],10:[function(_dereq_,module,exports){
421474
var BounceEasing, SwayEasing,
422475
__hasProp = {}.hasOwnProperty,
423476
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
@@ -440,6 +493,10 @@ SwayEasing = (function(_super) {
440493
return this.exponent(t) * this.oscillation(t);
441494
};
442495

496+
SwayEasing.prototype.calculateOmega = function(bounces, limit) {
497+
return this.bounces * Math.PI / this.limit;
498+
};
499+
443500
SwayEasing.prototype.oscillation = function(t) {
444501
return Math.sin(this.omega * t);
445502
};
@@ -583,7 +640,14 @@ Bounce = (function() {
583640

584641
Bounce.prototype.remove = function() {
585642
var _ref;
586-
return (_ref = this.styleElement) != null ? _ref.remove() : void 0;
643+
if (!this.styleElement) {
644+
return;
645+
}
646+
if (this.styleElement.remove) {
647+
return this.styleElement.remove();
648+
} else {
649+
return (_ref = this.styleElement.parentNode) != null ? _ref.removeChild(this.styleElement) : void 0;
650+
}
587651
};
588652

589653
Bounce.prototype.getPrefixes = function(force) {
@@ -616,7 +680,7 @@ Bounce = (function() {
616680
prefixes = this.getPrefixes(options.forcePrefix);
617681
}
618682
keyframeList = [];
619-
keyframes = this.getKeyframes();
683+
keyframes = this.getKeyframes(options);
620684
_ref = this.keys;
621685
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
622686
key = _ref[_i];
@@ -628,7 +692,7 @@ Bounce = (function() {
628692
prefix = _ref1[_j];
629693
transforms.push("" + prefix + "transform: " + transformString + ";");
630694
}
631-
keyframeList.push("" + (Math.round(key * 100 * 1e6) / 1e6) + "% { " + (transforms.join(" ")) + " }");
695+
keyframeList.push("" + (Math.round(key * 100 * 100) / 100) + "% { " + (transforms.join(" ")) + " }");
632696
}
633697
animations = [];
634698
_ref2 = prefixes.animation;
@@ -639,29 +703,55 @@ Bounce = (function() {
639703
return animations.join("\n\n");
640704
};
641705

642-
Bounce.prototype.getKeyframes = function() {
643-
var component, currentTime, frames, i, key, keyframes, matrix, ratio, _i, _j, _k, _len, _len1, _ref, _ref1;
644-
frames = Math.round((this.duration / 1000) * Bounce.FPS);
645-
this.keys = [];
646-
for (i = _i = 0; 0 <= frames ? _i <= frames : _i >= frames; i = 0 <= frames ? ++_i : --_i) {
647-
this.keys.push(i / frames);
706+
Bounce.prototype.getKeyframes = function(options) {
707+
var component, componentKeys, currentTime, frames, i, key, keyframes, keys, matrix, ratio, _i, _j, _k, _l, _len, _len1, _len2, _ref, _ref1;
708+
if (options == null) {
709+
options = {};
648710
}
711+
keys = [0, 1];
712+
if (options.optimized) {
713+
_ref = this.components;
714+
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
715+
component = _ref[_i];
716+
componentKeys = component.easingObject.findOptimalKeyPoints().map((function(_this) {
717+
return function(key) {
718+
return (key * component.duration / _this.duration) + (component.delay / _this.duration);
719+
};
720+
})(this));
721+
if (component.delay) {
722+
componentKeys.push((component.delay / this.duration) - 0.001);
723+
}
724+
keys = keys.concat(componentKeys);
725+
}
726+
} else {
727+
frames = Math.round((this.duration / 1000) * Bounce.FPS);
728+
for (i = _j = 0; 0 <= frames ? _j <= frames : _j >= frames; i = 0 <= frames ? ++_j : --_j) {
729+
keys.push(i / frames);
730+
}
731+
}
732+
keys = keys.sort(function(a, b) {
733+
return a - b;
734+
});
735+
this.keys = [];
649736
keyframes = {};
650-
_ref = this.keys;
651-
for (_j = 0, _len = _ref.length; _j < _len; _j++) {
652-
key = _ref[_j];
737+
for (_k = 0, _len1 = keys.length; _k < _len1; _k++) {
738+
key = keys[_k];
739+
if (keyframes[key]) {
740+
continue;
741+
}
653742
matrix = new Matrix4D().identity();
654743
_ref1 = this.components;
655-
for (_k = 0, _len1 = _ref1.length; _k < _len1; _k++) {
656-
component = _ref1[_k];
744+
for (_l = 0, _len2 = _ref1.length; _l < _len2; _l++) {
745+
component = _ref1[_l];
657746
currentTime = key * this.duration;
658747
if ((component.delay - currentTime) > 1e-8) {
659748
continue;
660749
}
661750
ratio = (key - component.delay / this.duration) / (component.duration / this.duration);
662751
matrix.multiply(component.getEasedMatrix(ratio));
663752
}
664-
keyframes[key] = matrix.transpose().toFixed(5);
753+
this.keys.push(key);
754+
keyframes[key] = matrix.transpose().toFixed(3);
665755
}
666756
return keyframes;
667757
};
@@ -695,7 +785,54 @@ Bounce = (function() {
695785
module.exports = Bounce;
696786

697787

698-
},{"./components/rotate":2,"./components/scale":3,"./components/skew":4,"./components/translate":5,"./math/matrix4d":12}],12:[function(_dereq_,module,exports){
788+
},{"./components/rotate":2,"./components/scale":3,"./components/skew":4,"./components/translate":5,"./math/matrix4d":13}],12:[function(_dereq_,module,exports){
789+
var MathHelpers;
790+
791+
MathHelpers = (function() {
792+
function MathHelpers() {}
793+
794+
MathHelpers.prototype.sign = function(value) {
795+
if (value < 0) {
796+
return -1;
797+
}
798+
return 1;
799+
};
800+
801+
MathHelpers.prototype.findTurningPoints = function(values) {
802+
var i, signA, signB, turningPoints, _i, _ref;
803+
turningPoints = [];
804+
for (i = _i = 1, _ref = values.length - 1; 1 <= _ref ? _i < _ref : _i > _ref; i = 1 <= _ref ? ++_i : --_i) {
805+
signA = this.sign(values[i] - values[i - 1]);
806+
signB = this.sign(values[i + 1] - values[i]);
807+
if (signA !== signB) {
808+
turningPoints.push(i);
809+
}
810+
}
811+
return turningPoints;
812+
};
813+
814+
MathHelpers.prototype.areaBetweenLineAndCurve = function(values, start, end) {
815+
var area, curveValue, i, length, lineValue, yEnd, yStart, _i;
816+
length = end - start;
817+
yStart = values[start];
818+
yEnd = values[end];
819+
area = 0;
820+
for (i = _i = 0; 0 <= length ? _i <= length : _i >= length; i = 0 <= length ? ++_i : --_i) {
821+
curveValue = values[start + i];
822+
lineValue = yStart + (i / length) * (yEnd - yStart);
823+
area += Math.abs(lineValue - curveValue);
824+
}
825+
return area;
826+
};
827+
828+
return MathHelpers;
829+
830+
})();
831+
832+
module.exports = new MathHelpers;
833+
834+
835+
},{}],13:[function(_dereq_,module,exports){
699836
var Matrix4D;
700837

701838
Matrix4D = (function() {
@@ -787,7 +924,7 @@ Matrix4D = (function() {
787924
module.exports = Matrix4D;
788925

789926

790-
},{}],13:[function(_dereq_,module,exports){
927+
},{}],14:[function(_dereq_,module,exports){
791928
var Vector2D;
792929

793930
Vector2D = (function() {

0 commit comments

Comments
 (0)