From 630b4357674c8b3918740a0ed7d9e3bf6bf76254 Mon Sep 17 00:00:00 2001 From: Niklas von Hertzen Date: Mon, 18 Jul 2011 00:59:36 +0300 Subject: [PATCH] support for opacity --- build/html2canvas.js | 74 ++++++++++++++++++---------------------- build/html2canvas.min.js | 12 +++---- src/Core.js | 12 ++++--- src/Draw.js | 36 +++++++------------ src/Traversing.js | 6 ++-- 5 files changed, 63 insertions(+), 77 deletions(-) diff --git a/build/html2canvas.js b/build/html2canvas.js index 77c560c..498f245 100644 --- a/build/html2canvas.js +++ b/build/html2canvas.js @@ -40,8 +40,8 @@ function html2canvas(el, userOptions) { this.opts = this.extendObj(options, { logging: false, - ready: function (canvas) { - document.body.appendChild(canvas); + ready: function (stack) { + document.body.appendChild(stack.canvas); }, iframeDefault: "default", flashCanvasPath: "http://html2canvas.hertzen.com/external/flashcanvas/flashcanvas.js", @@ -130,11 +130,15 @@ html2canvas.prototype.start = function(){ this.bodyOverflow = document.getElementsByTagName('body')[0].style.overflow; document.getElementsByTagName('body')[0].style.overflow = "hidden"; - var ctx = this.newElement(this.element, this.ctx) || this.ctx; + var stack = this.newElement(this.element,{ + ctx:this.ctx, + opacity:this.getCSS(this.element,"opacity") + + }) || this.ctx; + } - this.parseElement(this.element,ctx); - - } + this.parseElement(this.element,stack); + } @@ -475,7 +479,7 @@ html2canvas.prototype.getBorderData = function(el){ html2canvas.prototype.newElement = function(el,parentStack){ - + var bounds = this.getBounds(el); var x = bounds.left; @@ -491,29 +495,19 @@ html2canvas.prototype.newElement = function(el,parentStack){ //console.log(el.nodeName+":"+zindex+":"+this.getCSS(el,"position")+":"+this.numDraws+":"+this.getCSS(el,"z-index")) var opacity = this.getCSS(el,"opacity"); - - //if (this.getCSS(el,"position")!="static"){ - - /* - this.contextStacks.push(ctx); + + + var stack = { + ctx: new this.storageContext(), + zIndex: zindex, + opacity: opacity*parentStack.opacity + }; + + var stackLength = this.contextStacks.push(stack); - ctx = new this.storageContext(); - */ - - - - - var stack = { - ctx: new this.storageContext(), - zIndex: zindex, - opacity: opacity - }; - - var stackLength = this.contextStacks.push(stack); - - var ctx = this.contextStacks[stackLength-1].ctx; - //} - + var ctx = this.contextStacks[stackLength-1].ctx; + + this.setContextVariable(ctx,"globalAlpha",stack.opacity); /* * TODO add support for different border-style's than solid @@ -610,7 +604,7 @@ html2canvas.prototype.newElement = function(el,parentStack){ - return this.contextStacks[stackLength-1]; + return this.contextStacks[stackLength-1]; @@ -1067,12 +1061,10 @@ html2canvas.prototype.trim = function(text) { } -html2canvas.prototype.parseElement = function(element,ctx){ +html2canvas.prototype.parseElement = function(element,stack){ var _ = this; - this.each(element.children,function(index,el){ - _.parsing(el,{ - ctx:ctx - }); + this.each(element.children,function(index,el){ + _.parsing(el,stack); }); this.canvasRenderer(this.contextStacks); @@ -1082,10 +1074,12 @@ html2canvas.prototype.parseElement = function(element,ctx){ html2canvas.prototype.parsing = function(el,stack){ + + if (this.getCSS(el,'display') != "none" && this.getCSS(el,'visibility')!="hidden"){ + + var _ = this; - var _ = this; - - //if (!this.blockElements.test(el.nodeName)){ + //if (!this.blockElements.test(el.nodeName)){ stack = this.newElement(el,stack) || stack; @@ -1128,8 +1122,8 @@ html2canvas.prototype.parsing = function(el,stack){ } } - - // } + } +// } } diff --git a/build/html2canvas.min.js b/build/html2canvas.min.js index 79003ef..ed49619 100644 --- a/build/html2canvas.min.js +++ b/build/html2canvas.min.js @@ -8,7 +8,7 @@ function html2canvas(a,b){this.opts=this.extendObj(b||{},{logging:!1,ready:function(a){document.body.appendChild(a.canvas)},iframeDefault:"default",flashCanvasPath:"http://html2canvas.hertzen.com/external/flashcanvas/flashcanvas.js",renderViewport:!1,reorderZ:!0});this.element=a;this.imagesLoaded=0;this.images=[];this.fontData=[];this.numDraws=0;this.contextStacks=[];this.ignoreElements="IFRAME|OBJECT|PARAM";this.needReorder=!1;this.blockElements=/(BR|PARAM)/;this.ignoreRe=RegExp("("+this.ignoreElements+ ")");this.useRangeBounds=!1;this.init();return this}html2canvas.prototype.init=function(){var a=this;(this.ctx=new this.stackingContext($(document).width(),$(document).height()))?(this.canvas=this.ctx.canvas,this.log("Finding background images"),this.getImages(this.element),this.log("Finding images"),this.each(document.images,function(b,c){a.preloadImage(a.getAttr(c,"src"))}),this.images.length==0&&this.start()):this.log("Canvas not available")}; -html2canvas.prototype.start=function(){if(this.images.length==0||this.imagesLoaded==this.images.length/2)this.log("Started parsing"),this.bodyOverflow=document.getElementsByTagName("body")[0].style.overflow,document.getElementsByTagName("body")[0].style.overflow="hidden",this.parseElement(this.element,this.newElement(this.element,this.ctx)||this.ctx)}; +html2canvas.prototype.start=function(){if(this.images.length==0||this.imagesLoaded==this.images.length/2){this.log("Started parsing");this.bodyOverflow=document.getElementsByTagName("body")[0].style.overflow;document.getElementsByTagName("body")[0].style.overflow="hidden";var a=this.newElement(this.element,{ctx:this.ctx,opacity:this.getCSS(this.element,"opacity")})||this.ctx}this.parseElement(this.element,a)}; html2canvas.prototype.stackingContext=function(){this.canvas=document.createElement("canvas");this.canvas.width=$(document).width();this.canvas.height=$(document).height();if(this.canvas.getContext)this.ctx=this.canvas.getContext("2d");this.ctx.textBaseline="bottom";return this.ctx}; html2canvas.prototype.storageContext=function(){this.storage=[];this.fillRect=function(a,b,c,d){this.storage.push({type:"function",name:"fillRect",arguments:[a,b,c,d]})};this.drawImage=function(a,b,c,d,e,g,f,i,h){this.storage.push({type:"function",name:"drawImage",arguments:[a,b,c,d,e,g,f,i,h]})};this.fillText=function(a,b,c){this.storage.push({type:"function",name:"fillText",arguments:[a,b,c]})};return this}; html2canvas.prototype.finish=function(){this.log("Finished rendering");document.getElementsByTagName("body")[0].style.overflow=this.bodyOverflow;if(this.opts.renderViewport){var a=document.createElement("canvas");a.getContext("2d");a.width=window.innerWidth;a.height=window.innerHeight}this.opts.ready(this)}; @@ -19,10 +19,10 @@ html2canvas.prototype.getBackgroundPosition=function(a,b,c){var a=this.getCSS(a, html2canvas.prototype.drawbackgroundRepeatY=function(a,b,c,d,e,g,f){var i=Math.min(b.width,g),h;c.top-=Math.ceil(c.top/b.height)*b.height;for(h=e+c.top;hf+e?f+e-h:b.height,this.drawBackgroundRepeat(a,b,d+c.left,h,i,g,d,e),h=Math.floor(h+b.height)}; html2canvas.prototype.drawbackgroundRepeatX=function(a,b,c,d,e,g,f){var f=Math.min(b.height,f),i,h;c.left-=Math.ceil(c.left/b.width)*b.width;for(h=d+c.left;hg+d?g+d-h:b.width,this.drawBackgroundRepeat(a,b,h,e+c.top,i,f,d,e),h=Math.floor(h+b.width)};html2canvas.prototype.drawBackgroundRepeat=function(a,b,c,d,e,g,f,i){var h=0,k=0;f-c>0&&(h=f-c);i-d>0&&(k=i-d);this.drawImage(a,b,h,k,e-h,g-k,c+h,d+k,e-h,g-k)}; 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}; -html2canvas.prototype.newElement=function(a,b){var c=this.getBounds(a),d=c.left,e=c.top,g=c.width,f=c.height,i=this,h;h=this.getCSS(a,"background-color");var k=this.formatZ(this.getCSS(a,"z-index"),this.getCSS(a,"position"),b.zIndex,a.parentNode),l=this.getCSS(a,"opacity"),k=this.contextStacks.push({ctx:new this.storageContext,zIndex:k,opacity:l}),m=this.contextStacks[k-1].ctx,j=this.getBorderData(a);this.each(j,function(a,b){if(b.width>0){var c=d,h=e,k=g,l=f-j[2].width;switch(a){case 0:l=j[0].width; -break;case 1:c=d+g-j[1].width;k=j[1].width;break;case 2:h=h+f-j[2].width;l=j[2].width;break;case 3:k=j[3].width}i.newRect(m,c,h,k,l,b.color)}});this.ignoreRe.test(a.nodeName)&&this.opts.iframeDefault!="transparent"&&(h=this.opts.iframeDefault=="default"?"#efefef":this.opts.iframeDefault);this.newRect(m,d+j[3].width,e+j[0].width,g-(j[1].width+j[3].width),f-(j[0].width+j[2].width),h);this.drawBackground(a,{left:d+j[3].width,top:e+j[0].width,width:g-(j[1].width+j[3].width),height:f-(j[0].width+j[2].width)}, -m);a.nodeName=="IMG"&&((h=i.loadImage(i.getAttr(a,"src")))?this.drawImage(m,h,0,0,h.width,h.height,d+parseInt(i.getCSS(a,"padding-left"),10)+j[3].width,e+parseInt(i.getCSS(a,"padding-top"),10)+j[0].width,c.width-(j[1].width+j[3].width+parseInt(i.getCSS(a,"padding-left"),10)+parseInt(i.getCSS(a,"padding-right"),10)),c.height-(j[0].width+j[2].width+parseInt(i.getCSS(a,"padding-top"),10)+parseInt(i.getCSS(a,"padding-bottom"),10))):this.log("Error loading :"+i.getAttr(a,"src")));return this.contextStacks[k- -1]};html2canvas.prototype.printText=function(a,b,c,d){this.trim(a).length>0&&(d.fillText(a,b,c),this.numDraws++)};html2canvas.prototype.newRect=function(a,b,c,d,e,g){g!="transparent"&&(this.setContextVariable(a,"fillStyle",g),a.fillRect(b,c,d,e),this.numDraws++)};html2canvas.prototype.drawImage=function(a,b,c,d,e,g,f,i,h,k){a.drawImage(b,c,d,e,g,f,i,h,k);this.numDraws++}; +html2canvas.prototype.newElement=function(a,b){var c=this.getBounds(a),d=c.left,e=c.top,g=c.width,f=c.height,i=this,h;h=this.getCSS(a,"background-color");var k=this.formatZ(this.getCSS(a,"z-index"),this.getCSS(a,"position"),b.zIndex,a.parentNode),l=this.getCSS(a,"opacity"),l={ctx:new this.storageContext,zIndex:k,opacity:l*b.opacity},k=this.contextStacks.push(l),m=this.contextStacks[k-1].ctx;this.setContextVariable(m,"globalAlpha",l.opacity);var j=this.getBorderData(a);this.each(j,function(a,b){if(b.width> +0){var c=d,h=e,k=g,l=f-j[2].width;switch(a){case 0:l=j[0].width;break;case 1:c=d+g-j[1].width;k=j[1].width;break;case 2:h=h+f-j[2].width;l=j[2].width;break;case 3:k=j[3].width}i.newRect(m,c,h,k,l,b.color)}});this.ignoreRe.test(a.nodeName)&&this.opts.iframeDefault!="transparent"&&(h=this.opts.iframeDefault=="default"?"#efefef":this.opts.iframeDefault);this.newRect(m,d+j[3].width,e+j[0].width,g-(j[1].width+j[3].width),f-(j[0].width+j[2].width),h);this.drawBackground(a,{left:d+j[3].width,top:e+j[0].width, +width:g-(j[1].width+j[3].width),height:f-(j[0].width+j[2].width)},m);a.nodeName=="IMG"&&((h=i.loadImage(i.getAttr(a,"src")))?this.drawImage(m,h,0,0,h.width,h.height,d+parseInt(i.getCSS(a,"padding-left"),10)+j[3].width,e+parseInt(i.getCSS(a,"padding-top"),10)+j[0].width,c.width-(j[1].width+j[3].width+parseInt(i.getCSS(a,"padding-left"),10)+parseInt(i.getCSS(a,"padding-right"),10)),c.height-(j[0].width+j[2].width+parseInt(i.getCSS(a,"padding-top"),10)+parseInt(i.getCSS(a,"padding-bottom"),10))):this.log("Error loading :"+ +i.getAttr(a,"src")));return this.contextStacks[k-1]};html2canvas.prototype.printText=function(a,b,c,d){this.trim(a).length>0&&(d.fillText(a,b,c),this.numDraws++)};html2canvas.prototype.newRect=function(a,b,c,d,e,g){g!="transparent"&&(this.setContextVariable(a,"fillStyle",g),a.fillRect(b,c,d,e),this.numDraws++)};html2canvas.prototype.drawImage=function(a,b,c,d,e,g,f,i,h,k){a.drawImage(b,c,d,e,g,f,i,h,k);this.numDraws++}; 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,4)!="-moz"&&this.preloadImage(this.backgroundImageUrl(a))}; html2canvas.prototype.loadImage=function(a){a=this.images.indexOf(a);return a!=-1?this.images[a+1]:!1};html2canvas.prototype.preloadImage=function(a){if(this.images.indexOf(a)==-1){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.imagesLoaded++;c.start()};b.src=a;this.images.push(b)}}; html2canvas.prototype.canvasRenderer=function(a){var b=this,a=this.sortQueue(a);this.each(a,function(a,d){d.ctx.storage&&b.each(d.ctx.storage,function(a,c){switch(c.type){case "variable":b.ctx[c.name]=c.arguments;break;case "function":c.name=="fillRect"?b.ctx.fillRect(c.arguments[0],c.arguments[1],c.arguments[2],c.arguments[3]):c.name=="fillText"?b.ctx.fillText(c.arguments[0],c.arguments[1],c.arguments[2]):c.name=="drawImage"?b.ctx.drawImage(c.arguments[0],c.arguments[1],c.arguments[2],c.arguments[3], @@ -35,7 +35,7 @@ f.left,Math.round(f.top+l.baseline+l.lineWidth),f.width,1,g);break;case "overlin html2canvas.prototype.fontMetrics=function(a,b){var c=this.fontData.indexOf(a+"-"+b);if(c>-1)return this.fontData[c+1];c=document.createElement("div");document.getElementsByTagName("body")[0].appendChild(c);$(c).css({visibility:"hidden",fontFamily:a,fontSize:b,margin:0,padding:0});var d=document.createElement("img");d.src="http://html2canvas.hertzen.com/images/8.jpg";d.width=1;d.height=1;$(d).css({margin:0,padding:0});var e=document.createElement("span");$(e).css({fontFamily:a,fontSize:b,margin:0, padding:0});e.appendChild(document.createTextNode("Hidden Text"));c.appendChild(e);c.appendChild(d);var g=d.offsetTop-e.offsetTop+1;c.removeChild(e);c.appendChild(document.createTextNode("Hidden Text"));$(c).css("line-height","normal");$(d).css("vertical-align","super");d={baseline:g,lineWidth:1,middle:d.offsetTop-c.offsetTop+1};this.fontData.push(a+"-"+b);this.fontData.push(d);$(c).remove();return d}; html2canvas.prototype.textTransform=function(a,b){switch(b){case "lowercase":return a.toLowerCase();case "capitalize":return a.replace(/(^|\s|:|-|\(|\))([a-z])/g,function(a,b,e){return b+e.toUpperCase()});case "uppercase":return a.toUpperCase();default:return a}};html2canvas.prototype.trim=function(a){return a.replace(/^\s*/,"").replace(/\s*$/,"")}; -html2canvas.prototype.parseElement=function(a,b){var c=this;this.each(a.children,function(a,e){c.parsing(e,{ctx:b})});this.canvasRenderer(this.contextStacks);this.finish()}; +html2canvas.prototype.parseElement=function(a,b){var c=this;this.each(a.children,function(a,e){c.parsing(e,b)});this.canvasRenderer(this.contextStacks);this.finish()}; html2canvas.prototype.parsing=function(a,b){if(this.getCSS(a,"display")!="none"&&this.getCSS(a,"visibility")!="hidden"){var c=this,b=this.newElement(a,b)||b,d=b.ctx;if(!this.ignoreRe.test(a.nodeName)){var e=this.contentsInZ(a);e.length==1?e[0].nodeType==1?this.parsing(e[0],b):e[0].nodeType==3&&this.newText(a,e[0],b.ctx):this.each(e,function(e,f){f.nodeType==1?c.parsing(f,b):f.nodeType==3&&c.newText(a,f,d)})}}};html2canvas.prototype.log=function(){}; html2canvas.prototype.getBounds=function(a){window.scroll(0,0);if(a.getBoundingClientRect)return a=a.getBoundingClientRect(),a.top=a.top,a.left=a.left,a;else{var b=$(a).offset();return{left:b.left+parseInt(this.getCSS(a,"border-left-width"),10),top:b.top+parseInt(this.getCSS(a,"border-top-width"),10),width:$(a).innerWidth(),height:$(a).innerHeight()}}};html2canvas.prototype.each=function(a,b){for(var b=b||function(){},c=0;c