html2canvas/build/html2canvas.min.js

8 lines
37 KiB
JavaScript
Raw Normal View History

2013-09-07 22:24:41 +04:00
/*
2014-01-19 20:04:27 +04:00
html2canvas 0.5.0-rc1 <http://html2canvas.hertzen.com>
Copyright (c) 2014 Niklas von Hertzen
2013-09-07 22:24:41 +04:00
Released under MIT License
*/
(function(t,e,n){function r(t,e,n,r){return c(t,n,r,e).then(function(a){b("Document cloned");var c="["+ge+"='true']";t.querySelector(c).removeAttribute(ge);var h=a.contentWindow,u=h.document.querySelector(c),p=new pe(h.document),d=new g(e,p),l=C(u),f="view"===e.type?Math.min(l.width,n):o(),m="view"===e.type?Math.min(l.height,r):s(),y=new he(f,m,d),v=new O(u,y,p,d,e);return v.ready.then(function(){return b("Finished rendering"),e.removeContainer&&a.parentNode.removeChild(a),"view"===e.type||u!==h.document.body&&u!==h.document.documentElement?i(y.canvas,l):y.canvas})})}function i(t,n){var r=e.createElement("canvas"),i=r.width=Math.max(Math.min(0,n.left)+n.width,1),o=r.height=Math.max(Math.min(0,n.top)+n.height,1);return b("Cropping canvas at:","left:",n.left,"top:",n.top,"width:",n.width,"height:",n.height),b("Resulting crop with width",i,"and height",o),r.getContext("2d").drawImage(t,Math.max(0,n.left),Math.max(0,n.top),i,o,0,0,i,o),r}function o(){return Math.max(Math.max(e.body.scrollWidth,e.documentElement.scrollWidth),Math.max(e.body.offsetWidth,e.documentElement.offsetWidth),Math.max(e.body.clientWidth,e.documentElement.clientWidth))}function s(){return Math.max(Math.max(e.body.scrollHeight,e.documentElement.scrollHeight),Math.max(e.body.offsetHeight,e.documentElement.offsetHeight),Math.max(e.body.clientHeight,e.documentElement.clientHeight))}function a(){return"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"}function c(e,n,r,i){var o=e.documentElement.cloneNode(!0),s=e.createElement("iframe");return s.style.visibility="hidden",s.style.position="absolute",s.style.left=s.style.top="-10000px",s.width=n,s.height=r,s.scrolling="no",e.body.appendChild(s),new Promise(function(e){var n=s.contentWindow.document;n.open(),n.write("<!DOCTYPE html>"),n.close(),n.replaceChild(h(n.adoptNode(o)),n.documentElement),"view"===i.type&&s.contentWindow.scrollTo(t.pageXOffset,t.pageYOffset),e(s)})}function h(t){return[].slice.call(t.childNodes,0).filter(u).forEach(function(e){"SCRIPT"===e.tagName?t.removeChild(e):h(e)}),t}function u(t){return t.nodeType===Node.ELEMENT_NODE}function p(t,n){var r,i,o=e.createElement("div"),s=e.createElement("img"),c=e.createElement("span"),h="Hidden Text";o.style.visibility="hidden",o.style.fontFamily=t,o.style.fontSize=n,o.style.margin=0,o.style.padding=0,e.body.appendChild(o),s.src=a(),s.width=1,s.height=1,s.style.margin=0,s.style.padding=0,s.style.verticalAlign="baseline",c.style.fontFamily=t,c.style.fontSize=n,c.style.margin=0,c.style.padding=0,c.appendChild(e.createTextNode(h)),o.appendChild(c),o.appendChild(s),r=s.offsetTop-c.offsetTop+1,o.removeChild(c),o.appendChild(e.createTextNode(h)),o.style.lineHeight="normal",s.style.verticalAlign="super",i=s.offsetTop-o.offsetTop+1,e.body.removeChild(o),this.baseline=r,this.lineWidth=1,this.middle=i}function d(){this.data={}}function l(t){this.src=t.value,this.colorStops=[],this.type=null,this.x0=.5,this.y0=.5,this.x1=.5,this.y1=.5,this.promise=Promise.resolve(!0)}function f(t,e){this.src=t,this.image=new Image;var n=this.image;this.promise=new Promise(function(r,i){n.onload=r,n.onerror=i,e&&(n.crossOrigin="anonymous"),n.src=t,n.complete===!0&&r(n)})}function g(e,n){this.link=null,this.options=e,this.support=n,this.origin=t.location.protocol+t.location.host}function m(t){return"IMG"===t.node.nodeName}function y(t){return{args:[t.node.src],method:"url"}}function v(t){l.apply(this,arguments),this.type=this.TYPES.LINEAR,t.args[0].split(" ").forEach(function(t){switch(t){case"left":this.x0=0,this.x1=1;break;case"top":this.y0=0,this.y1=1;break;case"right":this.x0=1,this.x1=0;break;case"bottom":this.y0=1,this.y1=0}},this),t.args.slice(1).forEach(function(){},this)}function b(){t.html2canvas.logging&&t.console&&t.console.log&&Function.prototype.bind.call(t.console.log,t.console).apply(t.console,[Date.now()-t.html2canvas.start+"ms","html2canvas:"].concat([].slice.call(arguments,0)))}function w(t,e){this.node=t,this.parent=e,this.stack=null,this.bounds=null,this.offsetBounds=null,this.visible=null,this.computedStyles=null,this.styles={},
this.drawImage(r,0,0,r.width,r.height,e.left+i+c[3].width,e.top+o+c[0].width,e.width-(c[1].width+c[3].width+i+s),e.height-(c[0].width+c[2].width+o+a))},ce.prototype.renderBackground=function(t,e,n){e.height>0&&e.width>0&&(this.renderBackgroundColor(t,e),this.renderBackgroundImage(t,e,n))},ce.prototype.renderBackgroundColor=function(t,e){var n=t.css("backgroundColor");this.isTransparent(n)||this.rectangle(e.left,e.top,e.width,e.height,t.css("backgroundColor"))},ce.prototype.renderBorders=function(t){t.forEach(this.renderBorder,this)},ce.prototype.renderBorder=function(t){this.isTransparent(t.color)||null===t.args||this.drawShape(t.args,t.color)},ce.prototype.renderBackgroundImage=function(t,e,n){var r=t.parseBackgroundImages();r.reverse().forEach(function(r,i,o){switch(r.method){case"url":var s=this.images.get(r.args[0]);s?this.renderBackgroundRepeating(t,e,s,o.length-(i+1),n):b("Error loading background-image",r.args[0]);break;case"linear-gradient":case"gradient":var a=this.images.get(r.value);a?this.renderBackgroundGradient(a,e,n):b("Error loading background-image",r.args[0]);break;case"none":break;default:b("Unknown background-image type",r.args[0])}},this)},ce.prototype.renderBackgroundRepeating=function(t,e,n,r,i){var o=t.parseBackgroundSize(e,n.image,r),s=t.parseBackgroundPosition(e,n.image,r,o),a=t.parseBackgroundRepeat(r);switch(a){case"repeat-x":case"repeat no-repeat":this.backgroundRepeatShape(n,s,o,e,e.left+i[3],e.top+s.top+i[0],99999,n.image.height,i);break;case"repeat-y":case"no-repeat repeat":this.backgroundRepeatShape(n,s,o,e,e.left+s.left+i[3],e.top+i[0],n.image.width,99999,i);break;case"no-repeat":this.backgroundRepeatShape(n,s,o,e,e.left+s.left+i[3],e.top+s.top+i[0],n.image.width,n.image.height,i);break;default:this.renderBackgroundRepeat(n,s,o,{top:e.top,left:e.left},i[3],i[0])}},ce.prototype.isTransparent=function(t){return!t||"transparent"===t||"rgba(0, 0, 0, 0)"===t},he.prototype=Object.create(ce.prototype),he.prototype.setFillStyle=function(t){return this.ctx.fillStyle=t,this.ctx},he.prototype.rectangle=function(t,e,n,r,i){this.setFillStyle(i).fillRect(t,e,n,r)},he.prototype.drawShape=function(t,e){this.shape(t),this.setFillStyle(e).fill()},he.prototype.drawImage=function(t,e,n,r,i,o,s,a,c){this.ctx.drawImage(t,e,n,r,i,o,s,a,c)},he.prototype.clip=function(t,e,n){this.ctx.save(),this.shape(t).clip(),e.call(n),this.ctx.restore()},he.prototype.shape=function(t){return this.ctx.beginPath(),t.forEach(function(t,e){this.ctx[0===e?"moveTo":t[0]+"To"].apply(this.ctx,t.slice(1))},this),this.ctx.closePath(),this.ctx},he.prototype.font=function(t,e,n,r,i,o){this.setFillStyle(t).font=[e,n,r,i,o].join(" ")},he.prototype.fontShadow=function(t,e,n,r){this.setVariable("shadowColor",t).setVariable("shadowOffsetY",e).setVariable("shadowOffsetX",n).setVariable("shadowBlur",r)},he.prototype.clearShadow=function(){this.setVariable("shadowColor","rgba(0,0,0,0)")},he.prototype.setOpacity=function(t){this.ctx.globalAlpha=t},he.prototype.setTransform=function(t){this.ctx.translate(t.origin[0],t.origin[1]),this.ctx.transform.apply(this.ctx,t.matrix),this.ctx.translate(-t.origin[0],-t.origin[1])},he.prototype.setVariable=function(t,e){return this.variables[t]!==e&&(this.variables[t]=this.ctx[t]=e),this},he.prototype.text=function(t,e,n){this.ctx.fillText(t,e,n)},he.prototype.backgroundRepeatShape=function(t,e,n,r,i,o,s,a,c){var h=[["line",Math.round(i),Math.round(o)],["line",Math.round(i+s),Math.round(o)],["line",Math.round(i+s),Math.round(a+o)],["line",Math.round(i),Math.round(a+o)]];this.clip(h,function(){this.renderBackgroundRepeat(t,e,n,r,c[3],c[0])},this)},he.prototype.renderBackgroundRepeat=function(t,e,n,r,i,o){var s=Math.round(r.left+e.left+i),a=Math.round(r.top+e.top+o);this.setFillStyle(this.ctx.createPattern(this.resizeImage(t,n),"repeat")),this.ctx.translate(s,a),this.ctx.fill(),this.ctx.translate(-s,-a)},he.prototype.renderBackgroundGradient=function(t,e){t instanceof v&&this.ctx.createLinearGradient(e.left,e.top,e.right,e.bottom)},he.prototype.resizeImage=function(t,n){var r=t.image;if(r.width===n.width&&r.heig