Skip to content

Commit d2ed7a9

Browse files
committed
Add a requestAnimationFrame polyfill
1 parent f07a274 commit d2ed7a9

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ var d3 = require('d3');
1313

1414
var lib = module.exports = {};
1515

16+
require('./request_animation_frame');
17+
1618
lib.nestedProperty = require('./nested_property');
1719
lib.isPlainObject = require('./is_plain_object');
1820
lib.isArray = require('./is_array');

src/lib/request_animation_frame.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
10+
'use strict';
11+
12+
if(!Date.now) {
13+
Date.now = function() { return new Date().getTime(); };
14+
}
15+
16+
var vendors = ['webkit', 'moz'];
17+
for(var i = 0; i < vendors.length && !window.requestAnimationFrame; ++i) {
18+
var vp = vendors[i];
19+
window.requestAnimationFrame = window[vp + 'RequestAnimationFrame'];
20+
window.cancelAnimationFrame = (window[vp + 'CancelAnimationFrame'] ||
21+
window[vp + 'CancelRequestAnimationFrame']);
22+
}
23+
if(/iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent) || // iOS6 is buggy
24+
!window.requestAnimationFrame || !window.cancelAnimationFrame) {
25+
var lastTime = 0;
26+
window.requestAnimationFrame = function(callback) {
27+
var now = Date.now();
28+
var nextTime = Math.max(lastTime + 16, now);
29+
return setTimeout(function() { callback(lastTime = nextTime); },
30+
nextTime - now);
31+
};
32+
window.cancelAnimationFrame = clearTimeout;
33+
}

0 commit comments

Comments
 (0)