Skip to content

Commit

Permalink
Enhance withImageSize utility function so it queues up requests for t…
Browse files Browse the repository at this point in the history
…he same image's dimensions if the image has not yet been loaded; this prevents creating a new Image object for each of those requests.
  • Loading branch information
Jason Johnston committed Mar 6, 2011
1 parent ad97e7a commit 58d2df7
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions sources/Util.js
Expand Up @@ -67,14 +67,24 @@
* @param {Object} ctx A context object which will be used as the 'this' value within the executed callback function
*/
withImageSize: function( src, func, ctx ) {
var size = imageSizes[ src ], img;
var size = imageSizes[ src ], img, queue;
if( size ) {
func.call( ctx, size );
// If we have a queue, add to it
if( Object.prototype.toString.call( size ) === '[object Array]' ) {
size.push( [ func, ctx ] );
}
// Already have the size cached, call func right away
else {
func.call( ctx, size );
}
} else {
queue = imageSizes[ src ] = [ [ func, ctx ] ]; //create queue
img = new Image();
img.onload = function() {
size = imageSizes[ src ] = { w: img.width, h: img.height };
func.call( ctx, size );
for( var i = 0, len = queue.length; i < len; i++ ) {
queue[ i ][ 0 ].call( queue[ i ][ 1 ], size );
}
img.onload = null;
};
img.src = src;
Expand Down

0 comments on commit 58d2df7

Please sign in to comment.