Refactored preload

This commit is contained in:
Niklas von Hertzen 2013-01-11 23:01:29 +02:00
parent 67850f2cee
commit 1b37c5d1ea

View File

@ -117,19 +117,14 @@ _html2canvas.Preload = function( options ) {
} }
} }
function loadBackgroundImages(background_image, el) { function invalidBackgrounds(background_image) {
var background_images, bounds; return (background_image && background_image.method && background_image.args && background_image.args.length > 0 );
background_images = _html2canvas.Util.parseBackgroundImage(background_image);
for(var imageIndex = background_images.length; imageIndex-- > 0;) {
background_image = background_images[imageIndex];
if(!background_image ||
!background_image.method ||
!background_image.args ||
background_image.args.length === 0 ) {
continue;
} }
function loadBackgroundImages(background_image, el) {
var bounds;
_html2canvas.Util.parseBackgroundImage(background_image).filter(invalidBackgrounds).forEach(function(background_image) {
if (background_image.method === 'url') { if (background_image.method === 'url') {
methods.loadImage(background_image.args[0]); methods.loadImage(background_image.args[0]);
} else if(background_image.method.match(/\-?gradient$/)) { } else if(background_image.method.match(/\-?gradient$/)) {
@ -138,20 +133,17 @@ _html2canvas.Preload = function( options ) {
} }
loadGradientImage(background_image.value, bounds); loadGradientImage(background_image.value, bounds);
} }
} });
} }
function getImages (el) { function getImages (el) {
var contents = _html2canvas.Util.Children(el), var elNodeType = false;
i,
elNodeType = false;
// Firefox fails with permission denied on pages with iframes // Firefox fails with permission denied on pages with iframes
try { try {
var contentsLen = contents.length; _html2canvas.Util.Children(el).forEach(function(img) {
for (i = 0; i < contentsLen; i+=1 ){ getImages(img);
getImages(contents[i]); });
}
} }
catch( e ) {} catch( e ) {}
@ -186,7 +178,6 @@ _html2canvas.Preload = function( options ) {
start(); start();
}; };
img.onerror = function() { img.onerror = function() {
if (img.crossOrigin === "anonymous") { if (img.crossOrigin === "anonymous") {
// CORS failed // CORS failed
window.clearTimeout( imageObj.timer ); window.clearTimeout( imageObj.timer );
@ -203,25 +194,12 @@ _html2canvas.Preload = function( options ) {
} }
} }
images.numLoaded++; images.numLoaded++;
images.numFailed++; images.numFailed++;
imageObj.succeeded = false; imageObj.succeeded = false;
img.onerror = img.onload = null; img.onerror = img.onload = null;
start(); start();
}; };
// TODO Opera has no load/error event for SVG images
// Opera ninja onload's cached images
/*
window.setTimeout(function(){
if ( img.width !== 0 && imageObj.succeeded === undefined ) {
img.onload();
}
}, 100); // needs a reflow for base64 encoded images? interestingly timeout of 0 doesn't work but 1 does.
*/
} }
methods = { methods = {