Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- Added a frameRate parameter to animate() to control CPU usage

- Added an animateWithOptions() method as an alternate syntax which takes a single options object instead of individual arguments
  • Loading branch information...
commit 70ab80f2af83d57fbbd7b15ba97eeb37c41da249 1 parent dcd2739
@attaboy authored
Showing with 49 additions and 9 deletions.
  1. +49 −9 raphael.js
View
58 raphael.js
@@ -2938,8 +2938,13 @@ Raphael = (function () {
prev = e.prev || 0,
that = e.el,
callback = e.callback,
+ frameRate = e.frameRate,
+ timeoutDuration,
set = {},
now;
+ if (isFinite(frameRate) && frameRate > 0) { // Set amount of time to wait between animation calls if frameRate provided
+ timeoutDuration = Math.round(1000 / frameRate);
+ }
if (time < ms) {
var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
for (var attr in from) if (from[has](attr)) {
@@ -3022,7 +3027,7 @@ Raphael = (function () {
e.prev = time;
}
R.svg && that && that.paper && that.paper.safari();
- animationElements[length] && win.setTimeout(animation);
+ animationElements[length] && win.setTimeout(animation, timeoutDuration);
},
upto255 = function (color) {
return mmax(mmin(color, 255), 0);
@@ -3052,9 +3057,9 @@ Raphael = (function () {
}
return this;
};
- Element[proto].animateWith = function (element, params, ms, easing, callback) {
+ Element[proto].animateWith = function (element, params, ms, easing, callback, frameRate) {
animationElements[element.id] && (params.start = animationElements[element.id].start);
- return this.animate(params, ms, easing, callback);
+ return this.animate(params, ms, easing, callback, frameRate);
};
Element[proto].animateAlong = along();
Element[proto].animateAlongBack = along(1);
@@ -3071,8 +3076,29 @@ Raphael = (function () {
this._run = f || 0;
return this;
};
- Element[proto].animate = function (params, ms, easing, callback) {
- if (R.is(easing, "function") || !easing) {
+ Element[proto].animateWithOptions = function(optionsProvided) {
+ var options = {
+ parameters: {},
+ length: 0,
+ easing: "",
+ callback: function() {},
+ frameRate: null
+ };
+ for (var o in optionsProvided) {
+ options[o] = optionsProvided[o];
+ }
+ this.animate(options.parameters, options.length, options.easing, options.callback, options.frameRate);
+ };
+ Element[proto].animate = function (params, ms, easing, callback, frameRate) {
+ if (R.is(easing, "number")) {
+ frameRate = easing;
+ callback = null;
+ easing = null;
+ } else if (R.is(callback, "number")) {
+ frameRate = callback || null;
+ callback = null;
+ }
+ if (R.is(easing, "function")) {
callback = easing || null;
}
var from = {},
@@ -3163,6 +3189,7 @@ Raphael = (function () {
to: to,
el: this,
callback: callback,
+ frameRate: frameRate,
t: {x: 0, y: 0}
};
++animationElements[length] == 1 && animation();
@@ -3234,8 +3261,21 @@ Raphael = (function () {
}
return this;
};
- Set[proto].animate = function (params, ms, easing, callback) {
- (R.is(easing, "function") || !easing) && (callback = easing || null);
+ Set[proto].animateWithOptions = function(optionsProvided) {
+ Element[proto].animateWithOptions.call(this, optionsProvided);
+ };
+ Set[proto].animate = function (params, ms, easing, callback, frameRate) {
+ if (R.is(easing, "number")) {
+ frameRate = easing;
+ callback = null;
+ easing = null;
+ } else if (R.is(callback, "number")) {
+ frameRate = callback || null;
+ callback = null;
+ }
+ if (R.is(easing, "function")) {
+ callback = easing || null;
+ }
var len = this.items[length],
i = len,
item,
@@ -3245,9 +3285,9 @@ Raphael = (function () {
!--len && callback.call(set);
});
easing = R.is(easing, string) ? easing : collector;
- item = this.items[--i].animate(params, ms, easing, collector);
+ item = this.items[--i].animate(params, ms, easing, collector, frameRate);
while (i--) {
- this.items[i].animateWith(item, params, ms, easing, collector);
+ this.items[i].animateWith(item, params, ms, easing, collector, frameRate);
}
return this;
};
Please sign in to comment.
Something went wrong with that request. Please try again.