Skip to content

Instantly share code, notes, and snippets.

@jeffreytierney
Created May 22, 2012 14:53
Show Gist options
  • Save jeffreytierney/2769555 to your computer and use it in GitHub Desktop.
Save jeffreytierney/2769555 to your computer and use it in GitHub Desktop.
throttle back canvas drawing on drag / mousemove events
var last_draw;
function draw(e) {
var this_draw = +(new Date());
if(!last_draw || this_draw - last_draw > 20) {
last_draw = this_draw;
// window.reqAnimFrame is defined elsewhere to provide cross-browser
// requestAnimationFrame support with setTimeout fallback
window.reqAnimFrame(function() {
// draw logic
});
}
}
// binding (using jquery)
// in this case the canvas is the size of the entire window
// and animation is intended to fire / update
// while dragging an element anywhere over the whole thing
$(window).bind("dragover", draw);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment