mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
bug fixes
This commit is contained in:
parent
67c5af1822
commit
35fd0ae8bf
@ -213,6 +213,7 @@ _html2canvas.Util.Children = function(el) {
|
|||||||
var children;
|
var children;
|
||||||
try {
|
try {
|
||||||
children = $(el).contents();
|
children = $(el).contents();
|
||||||
|
// children = (el.nodeName && el.nodeName.toUpperCase() === "IFRAME") ? el.contentDocument || el.contentWindow.document : Array.prototype.push.call([], el.childNodes );
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
h2clog("html2canvas.Util.Children failed with exception: " + ex.message);
|
h2clog("html2canvas.Util.Children failed with exception: " + ex.message);
|
||||||
children = [];
|
children = [];
|
||||||
|
135
src/Preload.js
135
src/Preload.js
@ -7,7 +7,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
_html2canvas.Preload = function( options ) {
|
_html2canvas.Preload = function( options ) {
|
||||||
|
|
||||||
var images = {
|
var images = {
|
||||||
numLoaded: 0, // also failed are counted here
|
numLoaded: 0, // also failed are counted here
|
||||||
numFailed: 0,
|
numFailed: 0,
|
||||||
@ -31,30 +31,30 @@ _html2canvas.Preload = function( options ) {
|
|||||||
link.href = window.location.href;
|
link.href = window.location.href;
|
||||||
pageOrigin = link.protocol + link.host;
|
pageOrigin = link.protocol + link.host;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function isSameOrigin(url){
|
function isSameOrigin(url){
|
||||||
link.href = url;
|
link.href = url;
|
||||||
link.href = link.href; // YES, BELIEVE IT OR NOT, that is required for IE9 - http://jsfiddle.net/niklasvh/2e48b/
|
link.href = link.href; // YES, BELIEVE IT OR NOT, that is required for IE9 - http://jsfiddle.net/niklasvh/2e48b/
|
||||||
var origin = link.protocol + link.host;
|
var origin = link.protocol + link.host;
|
||||||
return (origin === pageOrigin);
|
return (origin === pageOrigin);
|
||||||
}
|
}
|
||||||
|
|
||||||
function start(){
|
function start(){
|
||||||
h2clog("html2canvas: start: images: " + images.numLoaded + " / " + images.numTotal + " (failed: " + images.numFailed + ")");
|
h2clog("html2canvas: start: images: " + images.numLoaded + " / " + images.numTotal + " (failed: " + images.numFailed + ")");
|
||||||
if (!images.firstRun && images.numLoaded >= images.numTotal){
|
if (!images.firstRun && images.numLoaded >= images.numTotal){
|
||||||
h2clog("Finished loading images: # " + images.numTotal + " (failed: " + images.numFailed + ")");
|
h2clog("Finished loading images: # " + images.numTotal + " (failed: " + images.numFailed + ")");
|
||||||
|
|
||||||
if (typeof options.complete === "function"){
|
if (typeof options.complete === "function"){
|
||||||
options.complete(images);
|
options.complete(images);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO modify proxy to serve images with CORS enabled, where available
|
// TODO modify proxy to serve images with CORS enabled, where available
|
||||||
function proxyGetImage(url, img, imageObj){
|
function proxyGetImage(url, img, imageObj){
|
||||||
var callback_name,
|
var callback_name,
|
||||||
@ -66,7 +66,7 @@ _html2canvas.Preload = function( options ) {
|
|||||||
|
|
||||||
callback_name = 'html2canvas_' + (count++);
|
callback_name = 'html2canvas_' + (count++);
|
||||||
imageObj.callbackname = callback_name;
|
imageObj.callbackname = callback_name;
|
||||||
|
|
||||||
if (scriptUrl.indexOf("?") > -1) {
|
if (scriptUrl.indexOf("?") > -1) {
|
||||||
scriptUrl += "&";
|
scriptUrl += "&";
|
||||||
} else {
|
} else {
|
||||||
@ -80,10 +80,10 @@ _html2canvas.Preload = function( options ) {
|
|||||||
imageObj.succeeded = false;
|
imageObj.succeeded = false;
|
||||||
images.numLoaded++;
|
images.numLoaded++;
|
||||||
images.numFailed++;
|
images.numFailed++;
|
||||||
start();
|
start();
|
||||||
} else {
|
} else {
|
||||||
setImageLoadHandlers(img, imageObj);
|
setImageLoadHandlers(img, imageObj);
|
||||||
img.src = a;
|
img.src = a;
|
||||||
}
|
}
|
||||||
window[callback_name] = undefined; // to work with IE<9 // NOTE: that the undefined callback property-name still exists on the window object (for IE<9)
|
window[callback_name] = undefined; // to work with IE<9 // NOTE: that the undefined callback property-name still exists on the window object (for IE<9)
|
||||||
try {
|
try {
|
||||||
@ -101,13 +101,13 @@ _html2canvas.Preload = function( options ) {
|
|||||||
window.document.body.appendChild(script);
|
window.document.body.appendChild(script);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getImages (el) {
|
function getImages (el) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// if (!this.ignoreRe.test(el.nodeName)){
|
// if (!this.ignoreRe.test(el.nodeName)){
|
||||||
//
|
//
|
||||||
|
|
||||||
var contents = _html2canvas.Util.Children(el),
|
var contents = _html2canvas.Util.Children(el),
|
||||||
i,
|
i,
|
||||||
@ -116,14 +116,14 @@ _html2canvas.Preload = function( options ) {
|
|||||||
src,
|
src,
|
||||||
img,
|
img,
|
||||||
elNodeType = false;
|
elNodeType = false;
|
||||||
|
|
||||||
for (i = 0; i < contentsLen; i+=1 ){
|
for (i = 0; i < contentsLen; i+=1 ){
|
||||||
// var ignRe = new RegExp("("+this.ignoreElements+")");
|
// var ignRe = new RegExp("("+this.ignoreElements+")");
|
||||||
// if (!ignRe.test(element.nodeName)){
|
// if (!ignRe.test(element.nodeName)){
|
||||||
getImages(contents[i]);
|
getImages(contents[i]);
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
// }
|
// }
|
||||||
try {
|
try {
|
||||||
elNodeType = el.nodeType;
|
elNodeType = el.nodeType;
|
||||||
@ -133,59 +133,59 @@ _html2canvas.Preload = function( options ) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (elNodeType === 1 || elNodeType === undefined){
|
if (elNodeType === 1 || elNodeType === undefined){
|
||||||
|
|
||||||
// opera throws exception on external-content.html
|
// opera throws exception on external-content.html
|
||||||
try {
|
try {
|
||||||
background_image = _html2canvas.Util.getCSS(el, 'backgroundImage');
|
background_image = _html2canvas.Util.getCSS(el, 'backgroundImage');
|
||||||
}catch(e) {
|
}catch(e) {
|
||||||
h2clog("html2canvas: failed to get background-image - Exception: " + e.message);
|
h2clog("html2canvas: failed to get background-image - Exception: " + e.message);
|
||||||
}
|
}
|
||||||
if ( background_image && background_image !== "1" && background_image !== "none" ) {
|
if ( background_image && background_image !== "1" && background_image !== "none" ) {
|
||||||
|
|
||||||
// TODO add multi image background support
|
// TODO add multi image background support
|
||||||
|
|
||||||
if (background_image.substring(0,7) === "-webkit" || background_image.substring(0,3) === "-o-" || background_image.substring(0,4) === "-moz") {
|
if (background_image.substring(0,7) === "-webkit" || background_image.substring(0,3) === "-o-" || background_image.substring(0,4) === "-moz") {
|
||||||
|
|
||||||
img = _html2canvas.Generate.Gradient( background_image, _html2canvas.Util.Bounds( el ) );
|
img = _html2canvas.Generate.Gradient( background_image, _html2canvas.Util.Bounds( el ) );
|
||||||
|
|
||||||
if ( img !== undefined ){
|
if ( img !== undefined ){
|
||||||
images[background_image] = {
|
images[background_image] = {
|
||||||
img: img,
|
img: img,
|
||||||
succeeded: true
|
succeeded: true
|
||||||
};
|
};
|
||||||
images.numTotal++;
|
images.numTotal++;
|
||||||
images.numLoaded++;
|
images.numLoaded++;
|
||||||
start();
|
start();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
src = _html2canvas.Util.backgroundImage(background_image.match(/data:image\/.*;base64,/i) ? background_image : background_image.split(",")[0]);
|
src = _html2canvas.Util.backgroundImage(background_image.match(/data:image\/.*;base64,/i) ? background_image : background_image.split(",")[0]);
|
||||||
methods.loadImage(src);
|
methods.loadImage(src);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
if (background_image && background_image !== "1" && background_image !== "none" && background_image.substring(0,7) !== "-webkit" && background_image.substring(0,3)!== "-o-" && background_image.substring(0,4) !== "-moz"){
|
if (background_image && background_image !== "1" && background_image !== "none" && background_image.substring(0,7) !== "-webkit" && background_image.substring(0,3)!== "-o-" && background_image.substring(0,4) !== "-moz"){
|
||||||
// TODO add multi image background support
|
// TODO add multi image background support
|
||||||
src = html2canvas.Util.backgroundImage(background_image.split(",")[0]);
|
src = html2canvas.Util.backgroundImage(background_image.split(",")[0]);
|
||||||
methods.loadImage(src); */
|
methods.loadImage(src); */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setImageLoadHandlers(img, imageObj) {
|
function setImageLoadHandlers(img, imageObj) {
|
||||||
img.onload = function() {
|
img.onload = function() {
|
||||||
if ( imageObj.timer !== undefined ) {
|
if ( imageObj.timer !== undefined ) {
|
||||||
// CORS succeeded
|
// CORS succeeded
|
||||||
window.clearTimeout( imageObj.timer );
|
window.clearTimeout( imageObj.timer );
|
||||||
}
|
}
|
||||||
|
|
||||||
images.numLoaded++;
|
images.numLoaded++;
|
||||||
imageObj.succeeded = true;
|
imageObj.succeeded = true;
|
||||||
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 );
|
||||||
@ -201,32 +201,33 @@ _html2canvas.Preload = function( options ) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
images.numLoaded++;
|
images.numLoaded++;
|
||||||
images.numFailed++;
|
images.numFailed++;
|
||||||
imageObj.succeeded = false;
|
imageObj.succeeded = false;
|
||||||
start();
|
start();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO Opera has no load/error event for SVG images
|
// TODO Opera has no load/error event for SVG images
|
||||||
|
|
||||||
// Opera ninja onload's cached images
|
// Opera ninja onload's cached images
|
||||||
|
/*
|
||||||
window.setTimeout(function(){
|
window.setTimeout(function(){
|
||||||
if ( img.width !== 0 && imageObj.succeeded === undefined ) {
|
if ( img.width !== 0 && imageObj.succeeded === undefined ) {
|
||||||
img.onload();
|
img.onload();
|
||||||
}
|
}
|
||||||
}, 100); // needs a reflow for base64 encoded images? interestingly timeout of 0 doesn't work but 1 does.
|
}, 100); // needs a reflow for base64 encoded images? interestingly timeout of 0 doesn't work but 1 does.
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
methods = {
|
methods = {
|
||||||
loadImage: function( src ) {
|
loadImage: function( src ) {
|
||||||
var img, imageObj;
|
var img, imageObj;
|
||||||
if ( src && images[src] === undefined ) {
|
if ( src && images[src] === undefined ) {
|
||||||
img = new Image();
|
img = new Image();
|
||||||
if ( src.match(/data:image\/.*;base64,/i) ) {
|
if ( src.match(/data:image\/.*;base64,/i) ) {
|
||||||
img.src = src.replace(/url\(['"]{0,}|['"]{0,}\)$/ig, '');
|
img.src = src.replace(/url\(['"]{0,}|['"]{0,}\)$/ig, '');
|
||||||
imageObj = images[src] = {
|
imageObj = images[src] = {
|
||||||
@ -234,7 +235,7 @@ _html2canvas.Preload = function( options ) {
|
|||||||
};
|
};
|
||||||
images.numTotal++;
|
images.numTotal++;
|
||||||
setImageLoadHandlers(img, imageObj);
|
setImageLoadHandlers(img, imageObj);
|
||||||
} else if ( isSameOrigin( src ) || options.allowTaint === true ) {
|
} else if ( isSameOrigin( src ) || options.allowTaint === true ) {
|
||||||
imageObj = images[src] = {
|
imageObj = images[src] = {
|
||||||
img: img
|
img: img
|
||||||
};
|
};
|
||||||
@ -243,25 +244,25 @@ _html2canvas.Preload = function( options ) {
|
|||||||
img.src = src;
|
img.src = src;
|
||||||
} else if ( supportCORS && !options.allowTaint && options.useCORS ) {
|
} else if ( supportCORS && !options.allowTaint && options.useCORS ) {
|
||||||
// attempt to load with CORS
|
// attempt to load with CORS
|
||||||
|
|
||||||
img.crossOrigin = "anonymous";
|
img.crossOrigin = "anonymous";
|
||||||
imageObj = images[src] = {
|
imageObj = images[src] = {
|
||||||
img: img
|
img: img
|
||||||
};
|
};
|
||||||
images.numTotal++;
|
images.numTotal++;
|
||||||
setImageLoadHandlers(img, imageObj);
|
setImageLoadHandlers(img, imageObj);
|
||||||
img.src = src;
|
img.src = src;
|
||||||
|
|
||||||
// work around for https://bugs.webkit.org/show_bug.cgi?id=80028
|
// work around for https://bugs.webkit.org/show_bug.cgi?id=80028
|
||||||
img.customComplete = function () {
|
img.customComplete = function () {
|
||||||
if (!this.img.complete) {
|
if (!this.img.complete) {
|
||||||
this.timer = window.setTimeout(this.img.customComplete, 100);
|
this.timer = window.setTimeout(this.img.customComplete, 100);
|
||||||
} else {
|
} else {
|
||||||
this.img.onerror();
|
this.img.onerror();
|
||||||
}
|
}
|
||||||
}.bind(imageObj);
|
}.bind(imageObj);
|
||||||
img.customComplete();
|
img.customComplete();
|
||||||
|
|
||||||
} else if ( options.proxy ) {
|
} else if ( options.proxy ) {
|
||||||
imageObj = images[src] = {
|
imageObj = images[src] = {
|
||||||
img: img
|
img: img
|
||||||
@ -269,8 +270,8 @@ _html2canvas.Preload = function( options ) {
|
|||||||
images.numTotal++;
|
images.numTotal++;
|
||||||
proxyGetImage( src, img, imageObj );
|
proxyGetImage( src, img, imageObj );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
cleanupDOM: function(cause) {
|
cleanupDOM: function(cause) {
|
||||||
var img, src;
|
var img, src;
|
||||||
@ -321,7 +322,7 @@ _html2canvas.Preload = function( options ) {
|
|||||||
window.clearTimeout(timeoutTimer);
|
window.clearTimeout(timeoutTimer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (options.timeout > 0) {
|
if (options.timeout > 0) {
|
||||||
@ -331,21 +332,21 @@ _html2canvas.Preload = function( options ) {
|
|||||||
images.firstRun = true;
|
images.firstRun = true;
|
||||||
|
|
||||||
getImages( element );
|
getImages( element );
|
||||||
|
|
||||||
h2clog('html2canvas: Preload: Finding images');
|
h2clog('html2canvas: Preload: Finding images');
|
||||||
// load <img> images
|
// load <img> images
|
||||||
for (i = 0; i < imgLen; i+=1){
|
for (i = 0; i < imgLen; i+=1){
|
||||||
methods.loadImage( domImages[i].getAttribute( "src" ) );
|
methods.loadImage( domImages[i].getAttribute( "src" ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
images.firstRun = false;
|
images.firstRun = false;
|
||||||
h2clog('html2canvas: Preload: Done.');
|
h2clog('html2canvas: Preload: Done.');
|
||||||
if ( images.numTotal === images.numLoaded ) {
|
if ( images.numTotal === images.numLoaded ) {
|
||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
|
|
||||||
return methods;
|
return methods;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ _html2canvas.Renderer = function(parseQueue, options){
|
|||||||
|
|
||||||
sortZ(parseQueue.zIndex);
|
sortZ(parseQueue.zIndex);
|
||||||
if ( typeof options.renderer._create !== "function" ) {
|
if ( typeof options.renderer._create !== "function" ) {
|
||||||
throw Error("Invalid renderer defined");
|
throw new Error("Invalid renderer defined");
|
||||||
}
|
}
|
||||||
return options.renderer._create( parseQueue, options, document, queue, _html2canvas );
|
return options.renderer._create( parseQueue, options, document, queue, _html2canvas );
|
||||||
|
|
||||||
|
@ -219,7 +219,7 @@ html2canvas.Renderer.Canvas = function( options ) {
|
|||||||
|
|
||||||
return canvas;
|
return canvas;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
return methods;
|
return methods;
|
||||||
|
|
||||||
|
@ -3,7 +3,10 @@
|
|||||||
<head>
|
<head>
|
||||||
<script type="text/javascript" src="test.js"></script>
|
<script type="text/javascript" src="test.js"></script>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
h2cSelector = '#bar';
|
function setUp(){
|
||||||
|
h2cSelector = '#bar';
|
||||||
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
<title>
|
<title>
|
||||||
display/box/float/clear test
|
display/box/float/clear test
|
||||||
|
@ -14,10 +14,12 @@ var h2cSelector, h2cOptions;
|
|||||||
document.write(scrStart + '../src/' + html2canvas[i] + '.js' + scrEnd);
|
document.write(scrStart + '../src/' + html2canvas[i] + '.js' + scrEnd);
|
||||||
}
|
}
|
||||||
window.onload = function() {
|
window.onload = function() {
|
||||||
|
h2cSelector = [document.body];
|
||||||
|
|
||||||
if (window.setUp) {
|
if (window.setUp) {
|
||||||
window.setUp();
|
window.setUp();
|
||||||
}
|
}
|
||||||
h2cSelector = [document.body];
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
|
|
||||||
$(h2cSelector).html2canvas($.extend({
|
$(h2cSelector).html2canvas($.extend({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user