html2canvas/build/html2canvas.min.js
2011-07-18 00:59:36 +03:00

43 lines
13 KiB
JavaScript

/*
* html2canvas v0.20 <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)},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";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)};
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){var d=this.backgroundImageUrl(d),g=this.loadImage(d),a=this.getBackgroundPosition(a,b,g);if(g)switch(e){case "repeat-x":this.drawbackgroundRepeatX(c,g,a,b.left,b.top,b.width,b.height);break;case "repeat-y":this.drawbackgroundRepeatY(c,g,a,b.left,b.top,b.width,b.height);break;case "no-repeat":this.drawBackgroundRepeat(c,
g,a.left+b.left,a.top+b.top,Math.min(b.width,g.width),Math.min(b.height,g.height),b.left,b.top);break;default:var f;a.top-=Math.ceil(a.top/g.height)*g.height;for(d=b.top+a.top;d<b.height+b.top;)e=Math.min(g.height,b.height+b.top-d),e=Math.floor(d+g.height)>e+d?e+d-d:g.height,d<b.top?(f=b.top-d,d=b.top):f=0,this.drawbackgroundRepeatX(c,g,a,b.left,d,b.width,e),f>0&&(a.top+=f),d=Math.floor(d+g.height)-f}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 a=this.getCSS(a,"background-position").split(" "),d,e;a.length==1&&(d=a,a=[],a[0]=d,a[1]=d);a[0].toString().indexOf("%")!=-1?(e=parseFloat(a[0])/100,d=b.width*e-c.width*e):d=parseInt(a[0],10);a[1].toString().indexOf("%")!=-1?(e=parseFloat(a[1])/100,b=b.height*e-c.height*e):b=parseInt(a[1],10);return{top:b,left:d}};
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;h<f+e;)g=Math.floor(h+b.height)>f+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;h<g+d;)i=Math.floor(h+b.width)>g+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"),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 <img>:"+
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],
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,c){if(b<c.zIndex.length)b=c.zIndex.length});var c=0;this.each(a,function(d,e){for(var g=a.length.toString().length-c.toString().length;b>e.zIndex.length;)e.zIndex+="0";for(e.zIndex+=c;b+g+c.toString().length>e.zIndex.length;)e.zIndex+="0";c++});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,c){a.storage?a.storage.push({type:"variable",name:b,arguments:c}):a[b]=c};
html2canvas.prototype.newText=function(a,b,c){var d=this.getCSS(a,"font-family"),e=this.getCSS(a,"font-size"),g=this.getCSS(a,"color"),f=this.getCSS(a,"font-weight"),i=this.getCSS(a,"font-style"),h=this.getCSS(a,"font-variant"),k=this.getCSS(a,"text-decoration");b.nodeValue=this.textTransform(b.nodeValue,this.getCSS(a,"text-transform"));a=b.nodeValue;if(a.length>0){switch(f){case "401":f="bold"}if(k!="none")var l=this.fontMetrics(d,e);this.setContextVariable(c,"fillStyle",g);this.setContextVariable(c,
"font",h+" "+f+" "+i+" "+e+" "+d);for(d=0;d<a.length;d++){e=b.splitText(1);if(this.useRangeBounds)document.createRange?(f=document.createRange(),f.selectNode(b)):f=document.body.createTextRange(),f=f.getBoundingClientRect()?f.getBoundingClientRect():{};else{var i=b.parentNode,h=document.createElement("wrapper"),m=b.cloneNode(!0);h.appendChild(b.cloneNode(!0));i.replaceChild(h,b);f=this.getBounds(h);i.replaceChild(m,h)}this.printText(b.nodeValue,f.left,f.bottom,c);switch(k){case "underline":this.newRect(c,
f.left,Math.round(f.top+l.baseline+l.lineWidth),f.width,1,g);break;case "overline":this.newRect(c,f.left,f.top,f.width,1,g);break;case "line-through":this.newRect(c,f.left,Math.ceil(f.top+l.middle+l.lineWidth),f.width,1,g)}b=e}}};
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,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<a.length;c++)b(c,a[c])};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 c in a)b[c]=a[c];return b};html2canvas.prototype.leadingZero=function(a,b){var c="000000000"+a;return c.substr(c.length-b)};
html2canvas.prototype.formatZ=function(a,b,c,d){c||(c="0");if(b!="static"&&c.charAt(0)=="0")this.needReorder=!0,c="1"+c.slice(1);if(a=="auto")if(a=this.getCSS(d,"position"),a!="static"&&typeof a!="undefined")a=0;else return c;b=this.leadingZero(this.numDraws,9);a=this.leadingZero(a+1,9);return c+""+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)};