Skip to content

Commit

Permalink
browser: use window.requestAnimationFrame...
Browse files Browse the repository at this point in the history
inside L.Util.requestAnimFrame unconditionally as we have already assuming the
availability of window.requestAnimationFrame inside canvas-section container.

Signed-off-by: Dennis Francis <[email protected]>
Change-Id: Ifdf885452135346e1df5f3196d49f76b8d1f983a
  • Loading branch information
dennisfrancis committed Jan 24, 2025
1 parent 5786dc7 commit 7a06303
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 39 deletions.
23 changes: 23 additions & 0 deletions browser/src/core/CoolUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,28 @@ namespace cool {
}
return result;
}

export function requestAnimFrame(
fn: () => void,
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
context: any,
immediate?: boolean,
): number {
if (immediate) {
fn.call(context);
return 0;
}

return window.requestAnimationFrame(fn.bind(context));
}

export function cancelAnimFrame(id: number): void {
if (id) {
window.cancelAnimationFrame(id);
}
}

export const MAX_SAFE_INTEGER = Math.pow(2, 3) - 1;
export const MIN_SAFE_INTEGER = -cool.Util.MAX_SAFE_INTEGER;
}
}
45 changes: 6 additions & 39 deletions browser/src/core/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,49 +106,16 @@ L.Util = {
replaceCtrlAltInMac: cool.Util.replaceCtrlAltInMac,

randomString: cool.Util.randomString,
};

(function () {
// inspired by http://paulirish.com/2011/requestanimationframe-for-smart-animating/

function getPrefixed(name) {
return window['webkit' + name] || window['moz' + name] || window['ms' + name];
}

var lastTime = 0;

// fallback for IE 7-8
function timeoutDefer(fn) {
var time = +new Date(),
timeToCall = Math.max(0, 16 - (time - lastTime));

lastTime = time + timeToCall;
return window.setTimeout(fn, timeToCall);
}

var requestFn = window.requestAnimationFrame || getPrefixed('RequestAnimationFrame') || timeoutDefer,
cancelFn = window.cancelAnimationFrame || getPrefixed('CancelAnimationFrame') ||
getPrefixed('CancelRequestAnimationFrame') || function (id) { window.clearTimeout(id); };

requestAnimFrame: cool.Util.requestAnimFrame,
cancelAnimFrame: cool.Util.cancelAnimFrame,

L.Util.requestAnimFrame = function (fn, context, immediate) {
if (immediate && requestFn === timeoutDefer) {
fn.call(context);
} else {
return requestFn.call(window, L.bind(fn, context));
}
};

L.Util.cancelAnimFrame = function (id) {
if (id) {
cancelFn.call(window, id);
}
};

// on IE11 Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER are not supported
L.Util.MAX_SAFE_INTEGER = Math.pow(2, 53)-1;
L.Util.MIN_SAFE_INTEGER = -L.Util.MAX_SAFE_INTEGER;
})();
MAX_SAFE_INTEGER: cool.Util.MAX_SAFE_INTEGER,
MIN_SAFE_INTEGER: cool.Util.MIN_SAFE_INTEGER,

};

if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
Expand Down

0 comments on commit 7a06303

Please sign in to comment.