Skip to content

Commit 57611e9

Browse files
committed
Add a requestAnimationFrame polyfill
1 parent f07a274 commit 57611e9

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
// This code adapted from:
10+
//
11+
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
12+
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
13+
14+
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
15+
16+
// MIT license
17+
18+
19+
'use strict';
20+
21+
if(!Date.now) {
22+
Date.now = function() { return new Date().getTime(); };
23+
}
24+
25+
var vendors = ['webkit', 'moz'];
26+
for(var i = 0; i < vendors.length && !window.requestAnimationFrame; ++i) {
27+
var vp = vendors[i];
28+
window.requestAnimationFrame = window[vp + 'RequestAnimationFrame'];
29+
window.cancelAnimationFrame = (window[vp + 'CancelAnimationFrame'] ||
30+
window[vp + 'CancelRequestAnimationFrame']);
31+
}
32+
if(/iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent) || // iOS6 is buggy
33+
!window.requestAnimationFrame || !window.cancelAnimationFrame) {
34+
var lastTime = 0;
35+
window.requestAnimationFrame = function(callback) {
36+
var now = Date.now();
37+
var nextTime = Math.max(lastTime + 16, now);
38+
return setTimeout(function() { callback(lastTime = nextTime); },
39+
nextTime - now);
40+
};
41+
window.cancelAnimationFrame = clearTimeout;
42+
}

src/components/rangeslider/create_slider.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,9 @@ module.exports = function createSlider(gd) {
221221
}
222222

223223
function setDataRange(dataMin, dataMax) {
224-
225-
if(window.requestAnimationFrame) {
226-
window.requestAnimationFrame(function() {
227-
Plotly.relayout(gd, 'xaxis.range', [dataMin, dataMax]);
228-
});
229-
} else {
230-
setTimeout(function() {
231-
Plotly.relayout(gd, 'xaxis.range', [dataMin, dataMax]);
232-
}, 16);
233-
}
224+
window.requestAnimationFrame(function() {
225+
Plotly.relayout(gd, 'xaxis.range', [dataMin, dataMax]);
226+
});
234227
}
235228

236229

tasks/stats.js

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function getInfoContent() {
5252
'',
5353
'```html',
5454
'<script>if(typeof window.Int16Array !== \'function\')document.write("<scri"+"pt src=\'extras/typedarray.min.js\'></scr"+"ipt>");</script>',
55+
'<script>document.write("<scri"+"pt src=\'extras/request_animation_frame.js\'></scr"+"ipt>");</script>',
5556
'```',
5657
'',
5758
'before the plotly.js script tag.',

0 commit comments

Comments
 (0)