Created
May 22, 2012 14:53
-
-
Save jeffreytierney/2769555 to your computer and use it in GitHub Desktop.
throttle back canvas drawing on drag / mousemove events
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
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