html2canvas/html2canvas.min.js
Niklas von Hertzen 578cd17223 initial commit
2011-07-16 04:13:38 +03:00

21 lines
5.9 KiB
JavaScript

/*
* html2canvas v0.11 <http://html2canvas.hertzen.com>
*
* Copyright 2011, Niklas von Hertzen <http://hertzen.com>
* http://www.twitter.com/niklasvh
*
* Released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
*/
function html2canvas(s,I){function t(){if(j.length==0||u==j.length/2)m("Started parsing"),x(s),J(s)}function m(a){q.logging&&(typeof console!="undefined"&&console.log?console.log(a):alert(a))}function y(a){if(j.indexOf(a)==-1){j.push(a);var b=new Image;$(b).load(function(){u++;t()});b.onerror=function(){j.splice(j.indexOf(b.src),2);u++;t()};b.src=a;j.push(b)}}function z(a){n($(a).contents(),function(a,e){RegExp("("+A+")").test(e.nodeName)||z(e)});if(a.nodeType==1||typeof a.nodeType=="undefined")if((a=
i(a,"background-image"))&&a!="1"&&a!="none"&&a.substring(0,7)!="-webkit")a=B(a),y(a)}function v(a){if(!RegExp("("+A+")").test(a.nodeName)){x(a);var b=$(a).contents();b.length==1?b[0].nodeType==1?v(b[0]):b[0].nodeType==3&&C(a,b[0]):n(b,function(b,g){g.nodeType==1?v(g):g.nodeType==3&&C(a,g)})}}function J(a){n(a.children,function(a,e){v(e)});K()}function B(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}function D(a){a=j.indexOf(a);
return a!=-1?j[a+1]:!1}function E(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(i(a,"border-left-width"),10),top:b.top+parseInt(i(a,"border-top-width"),10),width:$(a).innerWidth(),height:$(a).innerHeight()}}}function x(a){var b=E(a),e=b.left,g=b.top,f=b.width,d=b.height,b=i(a,"background-color"),c=L(a);n(c,function(a,b){if(b.width>0){var h=e,i=g,j=f,k=d-c[2].width;switch(a){case 0:k=
c[0].width;break;case 1:h=e+f-c[1].width;j=c[1].width;break;case 2:i=i+d-c[2].width;k=c[2].width;break;case 3:j=c[3].width}r(h,i,j,k,b.color)}});r(e+c[3].width,g+c[0].width,f-(c[1].width+c[3].width),d-(c[0].width+c[2].width),b);M(a,{left:e+c[3].width,top:g+c[0].width,width:f-(c[1].width+c[3].width),height:d-(c[0].width+c[2].width)});a.nodeName=="IMG"&&((F=D(a.getAttribute("src")))?k.drawImage(F,e+parseInt(i(a,"padding-left"),10),g+parseInt(i(a,"padding-top"),10)):m("Error loading <img>:"+a.getAttribute("src")))}
function M(a,b){var e=i(a,"background-image"),g=i(a,"background-repeat");if(typeof e!="undefined"&&/^(1|none)$/.test(e)==!1){var e=B(e),f=D(e),d,c=i(a,"background-position").split(" "),o;c[0].indexOf("%")!=-1?(o=parseFloat(c[0])/100,d=b.width*o-f.width*o):d=parseInt(c[0],10);c[1].indexOf("%")!=-1?(o=parseFloat(c[1])/100,c=b.height*o-f.height*o):c=parseInt(c[1],10);d={top:c,left:d};if(f)switch(g){case "repeat-x":G(f,d,b.left,b.top,b.width,b.height);break;case "repeat-y":var e=b.left,g=b.top,c=b.height,
j=Math.min(f.width,b.width);d.top-=Math.ceil(d.top/f.height)*f.height;for(h=g+d.top;h<=c+g;)o=Math.floor(h+f.height)>c+g?c+g-h:f.height,w(f,e+d.left,h,j,o,e,g),h=Math.floor(h+f.height);break;case "no-repeat":w(f,d.left+b.left,d.top+b.top,Math.min(b.width,f.width),Math.min(b.height,f.height),b.left,b.top);break;default:d.top-=Math.ceil(d.top/f.height)*f.height;for(h=b.top+d.top;h<=b.height+b.top;)e=Math.min(f.height,b.height+b.top-h),e=Math.floor(h+f.height)>e+h?e+h-h:f.height,h<b.top?(g=b.top-h,h=
b.top):g=0,G(f,d,b.left,h,b.width,e),g>0&&(d.top+=g),h=Math.floor(h+f.height)-g}else m("Error loading background:"+e)}}function G(a,b,e,g,f,d){var d=Math.min(a.height,d),c;b.left-=Math.ceil(b.left/a.width)*a.width;for(l=e+b.left;l<=f+e;)c=Math.floor(l+a.width)>f+e?f+e-l:a.width,w(a,l,g+b.top,c,d,e,g),l=Math.floor(l+a.width)}function w(a,b,e,g,f,d,c){var h=0,i=0;d-b>0&&(h=d-b);c-e>0&&(i=c-e);k.drawImage(a,h,i,g-h,f-i,b+h,e+i,g-h,f-i)}function L(a){var b=[];n(["top","right","bottom","left"],function(e,
g){b.push({width:parseInt(i(a,"border-"+g+"-width"),10),color:i(a,"border-"+g+"-color")})});return b}function r(a,b,e,g,f){if(f!="transparent")k.fillStyle=f,k.fillRect(a,b,e,g)}function i(a,b){return $(a).css(b)}function C(a,b){var e=i(a,"font-family"),g=i(a,"font-size"),f=i(a,"color"),d=i(a,"font-weight"),c=i(a,"font-style"),h=i(a,"text-decoration");b.nodeValue=N(b.nodeValue,i(a,"text-transform"));var j=b.nodeValue;if(j.length>0){switch(d){case "401":d="bold"}k.font=d+" "+c+" "+g+" "+e;k.fillStyle=
f;c=b;for(e=0;e<j.length;e++){g=c.splitText(1);if(H)document.createRange?(d=document.createRange(),d.selectNode(c)):d=document.body.createTextRange(),d=d.getBoundingClientRect()?d.getBoundingClientRect():{};else{var m=c.parentNode,l=document.createElement("wrapper"),n=c.cloneNode(!0);l.appendChild(c.cloneNode(!0));m.replaceChild(l,c);d=E(l);m.replaceChild(n,l)}c=c.nodeValue;m=d.left;l=d.bottom;c.replace(/^\s*/,"").replace(/\s*$/,"").length>0&&k.fillText(c,m,l);switch(h){case "underline":r(d.left,
Math.round(d.bottom-d.height/7),d.width,1,f);break;case "overline":r(d.left,d.top,d.width,1,f);break;case "line-through":r(d.left,Math.round(d.top+d.height/2),d.width,1,f)}c=g}}}function N(a,b){switch(b){case "lowercase":return a.toLowerCase();case "capitalize":return a.replace(/(^|\s|:|-|\(|\))([a-z])/g,function(a,b,f){return b+f.toUpperCase()});case "uppercase":return a.toUpperCase();default:return a}}function K(){m("Finished rendering");if(q.renderViewport){var a=document.createElement("canvas");
a.getContext("2d");a.width=window.innerWidth;a.height=window.innerHeight}q.ready(p)}function n(a,b){for(var b=b||function(){},e=0;e<a.length;e++)b(e,a[e])}var q={logging:!1,ready:function(a){document.body.appendChild(a)},renderViewport:!0},q=function(a,b){for(var e in a)b[e]=a[e];return b}(I||{},q),u=0,j=[],p,k,l,h,F,A="IFRAME|OBJECT|PARAM",H=!1;if(document.createRange)var O=document.createRange(),H=new Boolean(O.getBoundingClientRect);(function(){p=document.createElement("canvas");p.width=$(document).width();
p.height=$(document).height()+10;p.getContext&&(k=p.getContext("2d"));k?(k.textBaseline="bottom",m("Finding background images"),z(s),m("Finding images"),n(document.images,function(a,b){y(b.getAttribute("src"))}),j.length==0&&t()):m("Canvas not available")})()};