-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
13 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,28 @@ | ||
import { getEnv } from '../../env'; | ||
|
||
const _requestAnimFrame: AnimationFrameProvider['requestAnimationFrame'] = | ||
getEnv().window.requestAnimationFrame || | ||
function (callback: FrameRequestCallback) { | ||
return getEnv().window.setTimeout(callback, 1000 / 60); | ||
}; | ||
|
||
const _cancelAnimFrame: AnimationFrameProvider['cancelAnimationFrame'] = | ||
getEnv().window.cancelAnimationFrame || getEnv().window.clearTimeout; | ||
let _requestAnimFrame: AnimationFrameProvider['requestAnimationFrame']; | ||
let _cancelAnimFrame: AnimationFrameProvider['cancelAnimationFrame']; | ||
|
||
/** | ||
* requestAnimationFrame polyfill based on http://paulirish.com/2011/requestanimationframe-for-smart-animating/ | ||
* In order to get a precise start time, `requestAnimFrame` should be called as an entry into the method | ||
* @param {Function} callback Callback to invoke | ||
*/ | ||
export function requestAnimFrame(callback: FrameRequestCallback): number { | ||
if (!_requestAnimFrame) { | ||
_requestAnimFrame = | ||
getEnv().window.requestAnimationFrame || | ||
function requestAnimationFramePolyfill(callback: FrameRequestCallback) { | ||
return getEnv().window.setTimeout(callback, 1000 / 60); | ||
}; | ||
} | ||
return _requestAnimFrame.call(getEnv().window, callback); | ||
} | ||
|
||
export function cancelAnimFrame(handle: number): void { | ||
if (!_cancelAnimFrame) { | ||
_cancelAnimFrame = | ||
getEnv().window.cancelAnimationFrame || getEnv().window.clearTimeout; | ||
} | ||
return _cancelAnimFrame.call(getEnv().window, handle); | ||
} |