From dcd2a03f79723b9256d173b24e4f34b3b0d3e44d Mon Sep 17 00:00:00 2001 From: Niklas von Hertzen Date: Thu, 11 Aug 2011 15:32:20 +0300 Subject: [PATCH] fixed fatal error with multiple background images --- build/html2canvas.js | 53 +++++++++++++++++++++------------------ build/html2canvas.min.js | 14 +++++------ src/Background.js | 10 +++++--- src/Images.js | 4 +-- tests/background.html | 1 + tests/image2.jpg | Bin 0 -> 3608 bytes 6 files changed, 43 insertions(+), 39 deletions(-) create mode 100644 tests/image2.jpg diff --git a/build/html2canvas.js b/build/html2canvas.js index 6b109ea..f0c0dc4 100644 --- a/build/html2canvas.js +++ b/build/html2canvas.js @@ -293,9 +293,9 @@ html2canvas.prototype.finish = function(){ html2canvas.prototype.drawBackground = function(el,bounds,ctx){ - - var background_image = this.getCSS(el,"background-image"); - var background_repeat = this.getCSS(el,"background-repeat"); + // TODO add support for multi background-images + var background_image = this.getCSS(el,"background-image").split(",")[0]; + var background_repeat = this.getCSS(el,"background-repeat").split(",")[0]; if (typeof background_image != "undefined" && /^(1|none)$/.test(background_image)==false && /^(-webkit|-moz|linear-gradient|-o-)/.test(background_image)==false){ @@ -457,7 +457,9 @@ html2canvas.prototype.backgroundImageUrl = function(src){ */ html2canvas.prototype.getBackgroundPosition = function(el,bounds,image){ - var bgpos = this.getCSS(el,"backgroundPosition") || "0 0"; + // TODO add support for multi image backgrounds + + var bgpos = this.getCSS(el,"backgroundPosition").split(",")[0] || "0 0"; // var bgpos = $(el).css("backgroundPosition") || "0 0"; var bgposition = bgpos.split(" "), topPos, @@ -955,8 +957,8 @@ html2canvas.prototype.getImages = function(el) { var background_image = this.getCSS(el,'background-image'); 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"){ - - var src = this.backgroundImageUrl(background_image); + // TODO add multi image background support + var src = this.backgroundImageUrl(background_image.split(",")[0]); this.preloadImage(src); } } @@ -1354,7 +1356,7 @@ html2canvas.prototype.canvasRenderer = function(queue){ * Sort elements based on z-index and position attributes */ - +/* html2canvas.prototype.sortQueue = function(queue){ if (!this.opts.reorderZ || !this.needReorder) return queue; @@ -1392,16 +1394,11 @@ html2canvas.prototype.sortQueue = function(queue){ }); - /* - console.log('after'); - this.each(queue,function(i,e){ - console.log(i+":"+e.zIndex); - // console.log(e.ctx.storage); - }); */ return queue; } +*/ html2canvas.prototype.setContextVariable = function(ctx,variable,value){ if (!ctx.storage){ @@ -1897,24 +1894,30 @@ html2canvas.prototype.sortZ = function(zStack){ subStacks.push(stackChild); stackValues.push(stackChild.zindex); }else{ - _.queue.push(stackChild); + + + _.queue.push(stackChild); + } }); - - - - stackValues.sort(function(a,b){return a - b}); + + + + + stackValues.sort(function(a,b){ + return a - b + }); this.each(stackValues, function(i,zValue){ - for (var s = 0;s<=subStacks.length;s++){ - if (subStacks[s].zindex == zValue){ - var stackChild = subStacks.splice(s,1); - _.sortZ(stackChild[0]); - break; + for (var s = 0;s<=subStacks.length;s++){ + if (subStacks[s].zindex == zValue){ + var stackChild = subStacks.splice(s,1); + _.sortZ(stackChild[0]); + break; - } - } + } + } }); diff --git a/build/html2canvas.min.js b/build/html2canvas.min.js index d15f5be..8bddca4 100644 --- a/build/html2canvas.min.js +++ b/build/html2canvas.min.js @@ -14,10 +14,10 @@ html2canvas.prototype.start=function(){if(this.images.length==0||this.imagesLoad html2canvas.prototype.stackingContext=function(a){this.canvas=document.createElement("canvas");this.canvas.width=a;this.canvas.height=a;if(this.canvas.getContext)this.ctx=this.canvas.getContext("2d");this.ctx.textBaseline="bottom";return this.ctx}; html2canvas.prototype.storageContext=function(a,b){this.storage=[];this.width=a;this.height=b;this.fillRect=function(a,b,e,f){this.storage.push({type:"function",name:"fillRect",arguments:[a,b,e,f]})};this.drawImage=function(a,b,e,f,g,i,h,j,l){this.storage.push({type:"function",name:"drawImage",arguments:[a,b,e,f,g,i,h,j,l]})};this.fillText=function(a,b,e){this.storage.push({type:"function",name:"fillText",arguments:[a,b,e]})};return this}; html2canvas.prototype.finish=function(){this.log("Finished rendering");document.getElementsByTagName("body")[0].style.overflow=this.bodyOverflow;this.opts.ready(this)}; -html2canvas.prototype.drawBackground=function(a,b,c){var d=this.getCSS(a,"background-image"),e=this.getCSS(a,"background-repeat");if(typeof d!="undefined"&&/^(1|none)$/.test(d)==!1&&/^(-webkit|-moz|linear-gradient|-o-)/.test(d)==!1){var d=this.backgroundImageUrl(d),f=this.loadImage(d),a=this.getBackgroundPosition(a,b,f);if(f)switch(e){case "repeat-x":this.drawbackgroundRepeatX(c,f,a,b.left,b.top,b.width,b.height);break;case "repeat-y":this.drawbackgroundRepeatY(c,f,a,b.left,b.top,b.width,b.height); -break;case "no-repeat":var d=b.width-a.left,e=b.height-a.top,g=a.left,i=a.top,h=a.left+b.left,j=a.top+b.top;g<0?(g=Math.abs(g),h+=g,d=Math.min(b.width,f.width-g)):(d=Math.min(d,f.width),g=0);i<0?(i=Math.abs(i),j+=i,e=Math.min(b.height,f.height-i)):(e=Math.min(e,f.height),i=0);if(e>0&&d>0){this.drawImage(c,f,g,i,d,e,h,j,d,e);break}default:a.top-=Math.ceil(a.top/f.height)*f.height;for(d=b.top+a.top;de+d?e+d-d:f.height,d< -b.top?(g=b.top-d,d=b.top):g=0,this.drawbackgroundRepeatX(c,f,a,b.left,d,b.width,e),g>0&&(a.top+=g),d=Math.floor(d+f.height)-g}else this.log("Error loading background:"+d)}};html2canvas.prototype.backgroundImageUrl=function(a){a.substr(0,5)=='url("'?(a=a.substr(5),a=a.substr(0,a.length-2)):(a=a.substr(4),a=a.substr(0,a.length-1));return a}; -html2canvas.prototype.getBackgroundPosition=function(a,b,c){var d=(this.getCSS(a,"backgroundPosition")||"0 0").split(" "),e;d.length==1&&(a=d,d=[],d[0]=a,d[1]=a);d[0].toString().indexOf("%")!=-1?(e=parseFloat(d[0])/100,a=b.width*e-c.width*e):a=parseInt(d[0],10);d[1].toString().indexOf("%")!=-1?(e=parseFloat(d[1])/100,b=b.height*e-c.height*e):b=parseInt(d[1],10);c={};c.top=b;c.left=a;return c}; +html2canvas.prototype.drawBackground=function(a,b,c){var d=this.getCSS(a,"background-image").split(",")[0],e=this.getCSS(a,"background-repeat").split(",")[0];if(typeof d!="undefined"&&/^(1|none)$/.test(d)==!1&&/^(-webkit|-moz|linear-gradient|-o-)/.test(d)==!1){var d=this.backgroundImageUrl(d),f=this.loadImage(d),a=this.getBackgroundPosition(a,b,f);if(f)switch(e){case "repeat-x":this.drawbackgroundRepeatX(c,f,a,b.left,b.top,b.width,b.height);break;case "repeat-y":this.drawbackgroundRepeatY(c,f,a,b.left, +b.top,b.width,b.height);break;case "no-repeat":var d=b.width-a.left,e=b.height-a.top,g=a.left,i=a.top,h=a.left+b.left,j=a.top+b.top;g<0?(g=Math.abs(g),h+=g,d=Math.min(b.width,f.width-g)):(d=Math.min(d,f.width),g=0);i<0?(i=Math.abs(i),j+=i,e=Math.min(b.height,f.height-i)):(e=Math.min(e,f.height),i=0);if(e>0&&d>0){this.drawImage(c,f,g,i,d,e,h,j,d,e);break}default:a.top-=Math.ceil(a.top/f.height)*f.height;for(d=b.top+a.top;d +e+d?e+d-d:f.height,d0&&(a.top+=g),d=Math.floor(d+f.height)-g}else this.log("Error loading background:"+d)}};html2canvas.prototype.backgroundImageUrl=function(a){a.substr(0,5)=='url("'?(a=a.substr(5),a=a.substr(0,a.length-2)):(a=a.substr(4),a=a.substr(0,a.length-1));return a}; +html2canvas.prototype.getBackgroundPosition=function(a,b,c){var d=(this.getCSS(a,"backgroundPosition").split(",")[0]||"0 0").split(" "),e;d.length==1&&(a=d,d=[],d[0]=a,d[1]=a);d[0].toString().indexOf("%")!=-1?(e=parseFloat(d[0])/100,a=b.width*e-c.width*e):a=parseInt(d[0],10);d[1].toString().indexOf("%")!=-1?(e=parseFloat(d[1])/100,b=b.height*e-c.height*e):b=parseInt(d[1],10);c={};c.top=b;c.left=a;return c}; html2canvas.prototype.drawbackgroundRepeatY=function(a,b,c,d,e,f,g){var i=Math.min(b.width,f),h;c.top-=Math.ceil(c.top/b.height)*b.height;for(h=e+c.top;hg+e?g+e-h:b.height,this.drawBackgroundRepeat(a,b,d+c.left,h,i,f,d,e),h=Math.floor(h+b.height)}; html2canvas.prototype.drawbackgroundRepeatX=function(a,b,c,d,e,f,g){var g=Math.min(b.height,g),i,h;c.left-=Math.ceil(c.left/b.width)*b.width;for(h=d+c.left;hf+d?f+d-h:b.width,this.drawBackgroundRepeat(a,b,h,e+c.top,i,g,d,e),h=Math.floor(h+b.width)};html2canvas.prototype.drawBackgroundRepeat=function(a,b,c,d,e,f,g,i){var h=0,j=0;g-c>0&&(h=g-c);i-d>0&&(j=i-d);this.drawImage(a,b,h,j,e-h,f-j,c+h,d+j,e-h,f-j)}; html2canvas.prototype.getBorderData=function(a){var b=[],c=this;this.each(["top","right","bottom","left"],function(d,e){b.push({width:parseInt(c.getCSS(a,"border-"+e+"-width"),10),color:c.getCSS(a,"border-"+e+"-color")})});return b}; @@ -29,15 +29,13 @@ html2canvas.prototype.newElement=function(a,b){var c=this.getBounds(a),d=c.left, html2canvas.prototype.newRect=function(a,b,c,d,e,f){f!="transparent"&&(this.setContextVariable(a,"fillStyle",f),a.fillRect(b,c,d,e),this.numDraws++)};html2canvas.prototype.drawImage=function(a,b,c,d,e,f,g,i,h,j){a.drawImage(b,c,d,e,f,g,i,h,j);this.numDraws++}; html2canvas.prototype.renderFormValue=function(a,b,c){var d=document.createElement("valuewrap"),e=this;this.each(["lineHeight","textAlign","fontFamily","color","fontSize","paddingLeft","paddingTop","width","height","border","borderLeftWidth","borderTopWidth"],function(b,c){d.style[c]=e.getCSS(a,c)});d.style.borderColor="black";d.style.borderStyle="solid";d.style.display="block";d.style.position="absolute";if(/^(submit|reset|button|text|password)$/.test(a.type)||a.nodeName=="SELECT")d.style.lineHeight= e.getCSS(a,"height");d.style.top=b.top+"px";d.style.left=b.left+"px";b=document.createTextNode(a.nodeName=="SELECT"?a.options[a.selectedIndex].text:a.value);d.appendChild(b);$("body").append(d);this.newText(a,b,c);$(d).remove()}; -html2canvas.prototype.getImages=function(a){var b=this;this.ignoreRe.test(a.nodeName)||this.each($(a).contents(),function(a,d){RegExp("("+this.ignoreElements+")").test(d.nodeName)||b.getImages(d)});if(a.nodeType==1||typeof a.nodeType=="undefined")(a=this.getCSS(a,"background-image"))&&a!="1"&&a!="none"&&a.substring(0,7)!="-webkit"&&a.substring(0,3)!="-o-"&&a.substring(0,4)!="-moz"&&this.preloadImage(this.backgroundImageUrl(a))}; +html2canvas.prototype.getImages=function(a){var b=this;this.ignoreRe.test(a.nodeName)||this.each($(a).contents(),function(a,d){RegExp("("+this.ignoreElements+")").test(d.nodeName)||b.getImages(d)});if(a.nodeType==1||typeof a.nodeType=="undefined")(a=this.getCSS(a,"background-image"))&&a!="1"&&a!="none"&&a.substring(0,7)!="-webkit"&&a.substring(0,3)!="-o-"&&a.substring(0,4)!="-moz"&&this.preloadImage(this.backgroundImageUrl(a.split(",")[0]))}; html2canvas.prototype.loadImage=function(a){a=this.getIndex(this.images,a);return a!=-1?this.images[a+1]:!1};html2canvas.prototype.preloadImage=function(a){if(this.getIndex(this.images,a)==-1)if(this.isSameOrigin(a)){this.images.push(a);var b=new Image,c=this;$(b).load(function(){c.imagesLoaded++;c.start()});b.onerror=function(){c.images.splice(c.images.indexOf(b.src),2);c.start()};b.src=a;this.images.push(b)}else this.opts.proxyUrl&&(this.images.push(a),b=new Image,this.proxyGetImage(a,b),this.images.push(b))}; html2canvas.prototype.proxyGetImage=function(a,b){var c=this,d=document.createElement("a");d.href=a;a=d.href;$.ajax({data:{xhr2:!1,url:a},url:this.opts.proxyUrl,dataType:"jsonp",success:function(d){d.substring(0,6)=="error:"?(c.images.splice(c.images.indexOf(a),2),c.start(),c.log("Proxy was unable to load "+a+" "+d)):(b.onload=function(){c.imagesLoaded++;c.start()},b.src=d)},error:function(){c.images.splice(c.images.indexOf(a),2);c.start()}})}; html2canvas.prototype.Renderer=function(a){var b=this;this.log("Renderer initiated");this.each(this.opts.renderOrder.split(" "),function(c,d){switch(d){case "canvas":b.canvas=document.createElement("canvas");if(b.canvas.getContext)return b.canvasRenderer(a),b.log("Using canvas renderer"),!1;break;case "html":return b.log("Using HTML renderer"),!1}});return this};html2canvas.prototype.throttler=function(){}; html2canvas.prototype.canvasRenderContext=function(a,b){b.textBaseline="bottom";var c=this;a.clip&&(b.save(),b.beginPath(),b.rect(a.clip.left,a.clip.top,a.clip.width,a.clip.height),b.clip());a.ctx.storage&&c.each(a.ctx.storage,function(a,e){switch(e.type){case "variable":b[e.name]=e.arguments;break;case "function":e.name=="fillRect"?b.fillRect(e.arguments[0],e.arguments[1],e.arguments[2],e.arguments[3]):e.name=="fillText"?b.fillText(e.arguments[0],e.arguments[1],e.arguments[2]):e.name=="drawImage"? e.arguments[8]>0&&e.arguments[7]&&b.drawImage(e.arguments[0],e.arguments[1],e.arguments[2],e.arguments[3],e.arguments[4],e.arguments[5],e.arguments[6],e.arguments[7],e.arguments[8]):c.log(e)}});a.clip&&b.restore()};html2canvas.prototype.canvasRenderStorage=function(a,b){for(;0e.zIndex.length;)e.zIndex+="0";for(e.zIndex+=c;b+f+c.toString().length>e.zIndex.length;)e.zIndex+="0";c++});return a=a.sort(function(a,b){return a.zIndexb.zIndex?1:0})}; -html2canvas.prototype.setContextVariable=function(a,b,c){a.storage?a.storage.push({type:"variable",name:b,arguments:c}):a[b]=c}; +html2canvas.prototype.canvasRenderer=function(a){this.sortZ(this.zStack);a=this.queue;this.canvas.width=Math.max($(document).width(),this.opts.canvasWidth);this.canvas.height=Math.max($(document).height(),this.opts.canvasHeight);this.ctx=this.canvas.getContext("2d");this.canvasRenderStorage(a,this.ctx)};html2canvas.prototype.setContextVariable=function(a,b,c){a.storage?a.storage.push({type:"variable",name:b,arguments:c}):a[b]=c}; html2canvas.prototype.newText=function(a,b,c){var c=c.ctx,d=this.getCSS(a,"font-family"),e=this.getCSS(a,"font-size"),f=this.getCSS(a,"color"),g=this.getCSS(a,"font-weight"),i=this.getCSS(a,"font-style"),h=this.getCSS(a,"font-variant"),j=this.getCSS(a,"text-decoration"),l=this.getCSS(a,"text-align"),m=this.getCSS(a,"letter-spacing");b.nodeValue=this.textTransform(b.nodeValue,this.getCSS(a,"text-transform"));if(this.trim(b.nodeValue).length>0){switch(g){case 401:g="bold";break;case 400:g="normal"}if(j!= "none")var k=this.fontMetrics(d,e);d=h+" "+g+" "+i+" "+e+" "+d;l=l.replace(["-webkit-auto"],["auto"]);a=this.opts.letterRendering==!1&&/^(left|right|justify|auto)$/.test(l)&&/^(normal|none)$/.test(m)?b.nodeValue.split(/(\b| )/):b.nodeValue.split("");this.setContextVariable(c,"fillStyle",f);this.setContextVariable(c,"font",d);for(l=0;l
+
diff --git a/tests/image2.jpg b/tests/image2.jpg new file mode 100644 index 0000000000000000000000000000000000000000..3cb8bd690cd7855dc5d8f879165f3d715056fb02 GIT binary patch literal 3608 zcmex=9G120;!6Zw7B>MnMK9K}Kdl#{Wkcq!}2P89{&n23XnHIart&ng1VQuoPfm zVq|7!VrFAuWnu>@U}R!uVHIRxQxq~}7k1qMMFnc z;XtLt!p4aU**1OvlPLi1VshpvRuVh3bp9bja`&|ID85^)9Nw)}&QK`^kjl%Rc_xT^04Z zX2BCqEnyJsD`{Ehn6>!*aj7RObYm({9xHh;Yuc-Kf|sw@q!xv|^lms=CV5tJ;~m{A zhD+@JI`d>L4CM`}VK88?<$ud_Wv9O8bBA4S-xo){R9F@qapJD(qk@2s^Zsu9ezi7F zNpli&O6ldFLDCs9DlMe5|KnlZxxq{tM58vcpA+3k+s@Ow6cv=VT)WTrM_~eM)QQzujW4@|vSi&07?K#+m+!yv z__^S-%k8^-9@(YwZ*a@EdsAMeFMU7X>~@p%(R2T{mi%?>)tSK8@#w^pKx9_h1WvA5F%ai;htxGlS=FCmk zo9#L^_LS_@MVl8F7q?B0iut_Z`j5{i^}M$2lDhHGK9`Pq`m+eR1fwbBW2bC$czx`6=@}HFAG$ zMy7J_+6(Uf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