Merge pull request #803 from ahocevar/tween-framerate

Tween: skip frames when minimum frame rate is not reached. r=@bartvde
This commit is contained in:
ahocevar
2012-12-24 06:11:11 -08:00
2 changed files with 57 additions and 2 deletions
+24 -2
View File
@@ -52,6 +52,22 @@ OpenLayers.Tween = OpenLayers.Class({
*/
time: null,
/**
* APIProperty: minFrameRate
* {Number} The minimum framerate for animations in frames per second. After
* each step, the time spent in the animation is compared to the calculated
* time at this frame rate. If the animation runs longer than the calculated
* time, the next step is skipped. Default is 30.
*/
minFrameRate: null,
/**
* Property: startTime
* {Number} The timestamp of the first execution step. Used for skipping
* frames
*/
startTime: null,
/**
* Property: animationId
* {int} Loop id returned by OpenLayers.Animation.start
@@ -83,7 +99,8 @@ OpenLayers.Tween = OpenLayers.Class({
* begin - {Object} values to start the animation with
* finish - {Object} values to finish the animation with
* duration - {int} duration of the tween (number of steps)
* options - {Object} hash of options (for example callbacks (start, eachStep, done))
* options - {Object} hash of options (callbacks (start, eachStep, done),
* minFrameRate)
*/
start: function(begin, finish, duration, options) {
this.playing = true;
@@ -91,7 +108,9 @@ OpenLayers.Tween = OpenLayers.Class({
this.finish = finish;
this.duration = duration;
this.callbacks = options.callbacks;
this.minFrameRate = options.minFrameRate || 30;
this.time = 0;
this.startTime = new Date().getTime();
OpenLayers.Animation.stop(this.animationId);
this.animationId = null;
if (this.callbacks && this.callbacks.start) {
@@ -139,7 +158,10 @@ OpenLayers.Tween = OpenLayers.Class({
this.time++;
if (this.callbacks && this.callbacks.eachStep) {
this.callbacks.eachStep.call(this, value);
// skip frames if frame rate drops below threshold
if ((new Date().getTime() - this.startTime) / this.time <= 1000 / this.minFrameRate) {
this.callbacks.eachStep.call(this, value);
}
}
if (this.time > this.duration) {