Skip to content

Commit

Permalink
Add requestAnimationFrame
Browse files Browse the repository at this point in the history
Issue: no way to cap frame rate
  • Loading branch information
jonom committed Mar 2, 2016
1 parent 7571357 commit 40cb148
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions js/jquery.focuspoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,29 @@
var resizeFrameRate = 15; // Throttle the frame rate - set to 0 to disable throttling) 15fps default

// Resize throttling https://developer.mozilla.org/en-US/docs/Web/Events/resize
var resizeTimeout;
function resizeThrottler() {
if ( !resizeTimeout ) {
resizeTimeout = setTimeout(function() {
resizeTimeout = null;
$resizeElements.focusPoint('adjustFocus');
}, 1000/resizeFrameRate);
var running = false;
function resize() {
if (!running) {
running = true;

if (window.requestAnimationFrame) {
window.requestAnimationFrame(doResize);
} else {
setTimeout(doResize, 1000/resizeFrameRate);
}
}
}
function doResize() {
$resizeElements.focusPoint('adjustFocus');
running = false;
}

// Single resize listener for all focus point instances
var updateResizeListener = function() {
$(window).off('resize.focuspoint');
if ($resizeElements.length) {
if (resizeFrameRate > 0) {
$(window).on('resize.focuspoint', resizeThrottler);
$(window).on('resize.focuspoint', resize);
}
else {
$(window).on('resize.focuspoint', function(){
Expand Down

0 comments on commit 40cb148

Please sign in to comment.