html2canvas/build/html2canvas.min.js

50 lines
16 KiB
JavaScript

/*
* html2canvas v0.25 <http://html2canvas.hertzen.com>
* Copyright (c) 2011 Niklas von Hertzen. All rights reserved.
* http://www.twitter.com/niklasvh
*
* Released under MIT License
*/
function html2canvas(a,b){this.opts=this.extendObj(b||{},{logging:!1,ready:function(a){document.body.appendChild(a.canvas)},storageReady:function(a){a.Renderer(a.contextStacks)},iframeDefault:"default",flashCanvasPath:"http://html2canvas.hertzen.com/external/flashcanvas/flashcanvas.js",renderViewport:!1,reorderZ:!0,throttle:!0,letterRendering:!1,proxyUrl:null,logger:function(a){window.console&&window.console.log?window.console.log(a):alert(a)},canvasWidth:0,canvasHeight:0,renderOrder:"canvas flash html"});
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.pageOrigin=window.location.protocol+window.location.hostname;this.ignoreRe=RegExp("("+this.ignoreElements+")");this.support={rangeBounds:!1};if(document.createRange){var d=document.createRange();if(d.getBoundingClientRect){var c=document.createElement("boundtest");c.style.height="123px";c.style.display=
"block";document.getElementsByTagName("body")[0].appendChild(c);d.selectNode(c);if(d.getBoundingClientRect().height==123)this.support.rangeBounds=!0;document.getElementsByTagName("body")[0].removeChild(c)}}this.init();return this}
html2canvas.prototype.init=function(){var a=this;this.log("Finding background-images");this.images.push("start");this.getImages(this.element);this.log("Finding images");this.each(this.element.ownerDocument.images,function(b,d){a.preloadImage(a.getAttr(d,"src"))});this.images.splice(0,1);this.images.length==0&&this.start()};
html2canvas.prototype.start=function(){if(this.images.length==0||this.imagesLoaded==this.images.length/2){this.log("Finished loading "+this.imagesLoaded+" images, Started parsing");this.bodyOverflow=document.getElementsByTagName("body")[0].style.overflow;document.getElementsByTagName("body")[0].style.overflow="hidden";var a=new this.storageContext($(document).width(),$(document).height());a.opacity=this.getCSS(this.element,"opacity");this.parseElement(this.element,this.newElement(this.element,a))}};
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,j,h,k,i){this.storage.push({type:"function",name:"drawImage",arguments:[a,b,e,f,g,j,h,k,i]})};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,d){var c=this.getCSS(a,"background-image"),e=this.getCSS(a,"background-repeat");if(typeof c!="undefined"&&/^(1|none)$/.test(c)==!1&&/^(-webkit|-moz|linear-gradient|-o-)/.test(c)==!1){var c=this.backgroundImageUrl(c),f=this.loadImage(c),a=this.getBackgroundPosition(a,b,f);if(f)switch(e){case "repeat-x":this.drawbackgroundRepeatX(d,f,a,b.left,b.top,b.width,b.height);break;case "repeat-y":this.drawbackgroundRepeatY(d,f,a,b.left,b.top,b.width,b.height);
break;case "no-repeat":var c=b.width-a.left,e=b.height-a.top,g=a.left,j=a.top,h=a.left+b.left,k=a.top+b.top;g<0?(g=Math.abs(g),h+=g,c=Math.min(b.width,f.width-g)):(c=Math.min(c,f.width),g=0);j<0?(j=Math.abs(j),k+=j,e=Math.min(b.height,f.height-j)):(e=Math.min(e,f.height),j=0);if(e>0&&c>0){this.drawImage(d,f,g,j,c,e,h,k,c,e);break}default:a.top-=Math.ceil(a.top/f.height)*f.height;for(c=b.top+a.top;c<b.height+b.top;)e=Math.min(f.height,b.height+b.top-c),e=Math.floor(c+f.height)>e+c?e+c-c:f.height,c<
b.top?(g=b.top-c,c=b.top):g=0,this.drawbackgroundRepeatX(d,f,a,b.left,c,b.width,e),g>0&&(a.top+=g),c=Math.floor(c+f.height)-g}else this.log("Error loading background:"+c)}};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,d){var c=(this.getCSS(a,"backgroundPosition")||"0 0").split(" "),e;c.length==1&&(a=c,c=[],c[0]=a,c[1]=a);c[0].toString().indexOf("%")!=-1?(e=parseFloat(c[0])/100,a=b.width*e-d.width*e):a=parseInt(c[0],10);c[1].toString().indexOf("%")!=-1?(e=parseFloat(c[1])/100,b=b.height*e-d.height*e):b=parseInt(c[1],10);d={};d.top=b;d.left=a;return d};
html2canvas.prototype.drawbackgroundRepeatY=function(a,b,d,c,e,f,g){var j=Math.min(b.width,f),h;d.top-=Math.ceil(d.top/b.height)*b.height;for(h=e+d.top;h<g+e;)f=Math.floor(h+b.height)>g+e?g+e-h:b.height,this.drawBackgroundRepeat(a,b,c+d.left,h,j,f,c,e),h=Math.floor(h+b.height)};
html2canvas.prototype.drawbackgroundRepeatX=function(a,b,d,c,e,f,g){var g=Math.min(b.height,g),j,h;d.left-=Math.ceil(d.left/b.width)*b.width;for(h=c+d.left;h<f+c;)j=Math.floor(h+b.width)>f+c?f+c-h:b.width,this.drawBackgroundRepeat(a,b,h,e+d.top,j,g,c,e),h=Math.floor(h+b.width)};html2canvas.prototype.drawBackgroundRepeat=function(a,b,d,c,e,f,g,j){var h=0,k=0;g-d>0&&(h=g-d);j-c>0&&(k=j-c);this.drawImage(a,b,h,k,e-h,f-k,d+h,c+k,e-h,f-k)};
html2canvas.prototype.getBorderData=function(a){var b=[],d=this;this.each(["top","right","bottom","left"],function(c,e){b.push({width:parseInt(d.getCSS(a,"border-"+e+"-width"),10),color:d.getCSS(a,"border-"+e+"-color")})});return b};
html2canvas.prototype.drawBorders=function(a,b,d,c,e,f){var g=this.getBorderData(a),j=this;this.each(g,function(a,k){if(k.width>0){var i=d,l=c,m=e,n=f-g[2].width;switch(a){case 0:n=g[0].width;break;case 1:i=d+e-g[1].width;m=g[1].width;break;case 2:l=l+f-g[2].width;n=g[2].width;break;case 3:m=g[3].width}j.newRect(b,i,l,m,n,k.color)}});return g};
html2canvas.prototype.newElement=function(a,b){var d=this.getBounds(a),c=d.left,e=d.top,f=d.width,g=d.height,j=this.getCSS(a,"background-color"),b=b||{},h=this.formatZ(this.getCSS(a,"zIndex"),this.getCSS(a,"position"),b.zIndex,a.parentNode),k=this.getCSS(a,"opacity"),i={ctx:new this.storageContext,zIndex:h,opacity:k*b.opacity},h=this.contextStacks.push(i),k=this.contextStacks[h-1].ctx;this.setContextVariable(k,"globalAlpha",i.opacity);i=this.drawBorders(a,k,d.left,d.top,d.width,d.height);this.ignoreRe.test(a.nodeName)&&
this.opts.iframeDefault!="transparent"&&(j=this.opts.iframeDefault=="default"?"#efefef":this.opts.iframeDefault);this.newRect(k,c+i[3].width,e+i[0].width,f-(i[1].width+i[3].width),g-(i[0].width+i[2].width),j);this.drawBackground(a,{left:c+i[3].width,top:e+i[0].width,width:f-(i[1].width+i[3].width),height:g-(i[0].width+i[2].width)},k);a.nodeName=="IMG"&&((f=this.loadImage(this.getAttr(a,"src")))?this.drawImage(k,f,0,0,f.width,f.height,c+parseInt(this.getCSS(a,"padding-left"),10)+i[3].width,e+parseInt(this.getCSS(a,
"padding-top"),10)+i[0].width,d.width-(i[1].width+i[3].width+parseInt(this.getCSS(a,"padding-left"),10)+parseInt(this.getCSS(a,"padding-right"),10)),d.height-(i[0].width+i[2].width+parseInt(this.getCSS(a,"padding-top"),10)+parseInt(this.getCSS(a,"padding-bottom"),10))):this.log("Error loading <img>:"+this.getAttr(a,"src")));return this.contextStacks[h-1]};html2canvas.prototype.printText=function(a,b,d,c){this.trim(a).length>0&&(c.fillText(a,b,d),this.numDraws++)};
html2canvas.prototype.newRect=function(a,b,d,c,e,f){f!="transparent"&&(this.setContextVariable(a,"fillStyle",f),a.fillRect(b,d,c,e),this.numDraws++)};html2canvas.prototype.drawImage=function(a,b,d,c,e,f,g,j,h,k){a.drawImage(b,d,c,e,f,g,j,h,k);this.numDraws++};
html2canvas.prototype.getImages=function(a){var b=this;this.ignoreRe.test(a.nodeName)||this.each($(a).contents(),function(a,c){RegExp("("+this.ignoreElements+")").test(c.nodeName)||b.getImages(c)});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.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,d=this;$(b).load(function(){d.imagesLoaded++;d.start()});b.onerror=function(){d.images.splice(d.images.indexOf(b.src),2);d.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 d=this,c=document.createElement("a");c.href=a;a=c.href;$.ajax({data:{xhr2:!1,url:a},url:this.opts.proxyUrl,dataType:"jsonp",success:function(c){c.substring(0,6)=="error:"?(d.images.splice(d.images.indexOf(a),2),d.start(),d.log("Proxy was unable to load "+a+" "+c)):(b.onload=function(){d.imagesLoaded++;d.start()},b.src=c)},error:function(){d.images.splice(d.images.indexOf(a),2);d.start()}})};
html2canvas.prototype.Renderer=function(a){var b=this;this.log("Renderer initiated");this.each(this.opts.renderOrder.split(" "),function(d,c){switch(c){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.canvasRenderer=function(a){var b=this,a=this.sortQueue(a);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.ctx.textBaseline="bottom";this.each(a,function(a,c){c.ctx.storage&&b.each(c.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"?c.arguments[8]>0&&c.arguments[7]&&b.ctx.drawImage(c.arguments[0],c.arguments[1],c.arguments[2],c.arguments[3],c.arguments[4],c.arguments[5],c.arguments[6],c.arguments[7],c.arguments[8]):this.log(c)}})})};
html2canvas.prototype.sortQueue=function(a){if(!this.opts.reorderZ||!this.needReorder)return a;var b=0;this.each(a,function(a,d){if(b<d.zIndex.length)b=d.zIndex.length});var d=0;this.each(a,function(c,e){for(var f=a.length.toString().length-d.toString().length;b>e.zIndex.length;)e.zIndex+="0";for(e.zIndex+=d;b+f+d.toString().length>e.zIndex.length;)e.zIndex+="0";d++});return a=a.sort(function(a,b){return a.zIndex<b.zIndex?-1:a.zIndex>b.zIndex?1:0})};
html2canvas.prototype.setContextVariable=function(a,b,d){a.storage?a.storage.push({type:"variable",name:b,arguments:d}):a[b]=d};
html2canvas.prototype.newText=function(a,b,d){var c=this.getCSS(a,"font-family"),e=this.getCSS(a,"font-size"),f=this.getCSS(a,"color"),g=this.getCSS(a,"font-weight"),j=this.getCSS(a,"font-style"),h=this.getCSS(a,"font-variant"),k=this.getCSS(a,"text-decoration"),i=this.getCSS(a,"text-align"),l=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(k!="none")var m=
this.fontMetrics(c,e);a=h+" "+g+" "+j+" "+e+" "+c;i=i.replace(["-webkit-auto"],["auto"]);i=this.opts.letterRendering==!1&&/^(left|right|justify|auto)$/.test(i)&&/^(normal|none)$/.test(l)?b.nodeValue.split(/(\b| )/):b.nodeValue.split("");this.setContextVariable(d,"fillStyle",f);this.setContextVariable(d,"font",a);for(l=0;l<i.length;l++){a=b.splitText(i[l].length);this.support.rangeBounds?(document.createRange?(c=document.createRange(),c.selectNode(b)):c=document.body.createTextRange(),c=c.getBoundingClientRect()?
c.getBoundingClientRect():{}):(e=b.parentNode,g=document.createElement("wrapper"),j=b.cloneNode(!0),g.appendChild(b.cloneNode(!0)),e.replaceChild(g,b),c=this.getBounds(g),e.replaceChild(j,g));this.printText(b.nodeValue,c.left,c.bottom,d);switch(k){case "underline":this.newRect(d,c.left,Math.round(c.top+m.baseline+m.lineWidth),c.width,1,f);break;case "overline":this.newRect(d,c.left,c.top,c.width,1,f);break;case "line-through":this.newRect(d,c.left,Math.ceil(c.top+m.middle+m.lineWidth),c.width,1,f)}b=
a}}};
html2canvas.prototype.fontMetrics=function(a,b){var d=this.fontData.indexOf(a+"-"+b);if(d>-1)return this.fontData[d+1];d=document.createElement("div");document.getElementsByTagName("body")[0].appendChild(d);$(d).css({visibility:"hidden",fontFamily:a,fontSize:b,margin:0,padding:0});var c=document.createElement("img");c.src="http://html2canvas.hertzen.com/images/8.jpg";c.width=1;c.height=1;$(c).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"));d.appendChild(e);d.appendChild(c);var f=c.offsetTop-e.offsetTop+1;d.removeChild(e);d.appendChild(document.createTextNode("Hidden Text"));$(d).css("line-height","normal");$(c).css("vertical-align","super");c={baseline:f,lineWidth:1,middle:c.offsetTop-d.offsetTop+1};this.fontData.push(a+"-"+b);this.fontData.push(c);$(d).remove();return c};
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 d=this;this.each(a.children,function(a,e){d.parsing(e,b)});this.log("Render queue stored");this.opts.storageReady(this);this.finish()};
html2canvas.prototype.parsing=function(a,b){if(this.getCSS(a,"display")!="none"&&this.getCSS(a,"visibility")!="hidden"){var d=this,b=this.newElement(a,b)||b,c=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,g){g.nodeType==1?d.parsing(g,b):g.nodeType==3&&d.newText(a,g,c)})}}};html2canvas.prototype.log=function(a){this.opts.logging&&this.opts.logger(a)};
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(){},d=0;d<a.length;d++)if(b(d,a[d])===!1)break};html2canvas.prototype.contentsInZ=function(a){return $(a).contents()};
html2canvas.prototype.getAttr=function(a,b){return a.getAttribute(b)};html2canvas.prototype.extendObj=function(a,b){for(var d in a)b[d]=a[d];return b};html2canvas.prototype.leadingZero=function(a,b){var d="000000000"+a;return d.substr(d.length-b)};
html2canvas.prototype.formatZ=function(a,b,d,c){d||(d="0");if(b!="static"&&d.charAt(0)=="0")this.needReorder=!0,d="1"+d.slice(1);if(a=="auto")if(a=this.getCSS(c,"position"),a!="static"&&typeof a!="undefined")a=0;else return d;b=this.leadingZero(this.numDraws,9);a=this.leadingZero(a+1,9);return d+""+a+""+b};html2canvas.prototype.getContents=function(a){return a.nodeName=="iframe"?a.contentDocument||a.contentWindow.document:a.childNodes};html2canvas.prototype.getCSS=function(a,b){return $(a).css(b)};
html2canvas.prototype.getIndex=function(a,b){if(a.indexOf)return a.indexOf(b);else{for(var d=0;d<a.length;d++)if(this[d]==b)return d;return-1}};html2canvas.prototype.isSameOrigin=function(a){var b=document.createElement("a");b.href=a;return b.protocol+b.hostname==this.pageOrigin};