html2canvas/build/html2canvas.min.js

8 lines
38 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
*/
2014-05-18 20:14:22 +04:00
(function(t,e,n){function r(t,e,n,r){return c(t,n,r,e).then(function(a){b("Document cloned");var c="["+me+"='true']";t.querySelector(c).removeAttribute(me);var h=a.contentWindow,u=h.document.querySelector(c),p=new le(h.document),l=new m(e,p),d=S(u),f="view"===e.type?Math.min(d.width,n):i(),g="view"===e.type?Math.min(d.height,r):s(),y=new ue(f,g,l),v=new B(u,y,p,l,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?o(y.canvas,d):y.canvas})})}function o(t,n){var r=e.createElement("canvas"),o=Math.min(t.width-1,Math.max(0,n.left)),i=Math.min(t.width,Math.max(1,n.left+n.width)),s=Math.min(t.height-1,Math.max(0,n.top)),a=Math.min(t.height,Math.max(1,n.top+n.height)),c=r.width=i-o,h=r.height=a-s;return b("Cropping canvas at:","left:",n.left,"top:",n.top,"width:",n.width,"height:",n.height),b("Resulting crop with width",c,"and height",h," with x",o,"and y",s),r.getContext("2d").drawImage(t,o,s,c,h,0,0,c,h),r}function i(){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,o){var i=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;s.contentWindow.onload=s.onload=function(){e(s)},n.open(),n.write("<!DOCTYPE html>"),n.close(),n.replaceChild(h(n.adoptNode(i)),n.documentElement),"view"===o.type&&s.contentWindow.scrollTo(t.pageXOffset,t.pageYOffset)})}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){if(this.src=t,b("DummyImageContainer for",t),!this.promise||!this.image){b("Initiating DummyImageContainer"),p.prototype.image=new Image;var e=this.image;p.prototype.promise=new Promise(function(t,n){e.onload=t,e.onerror=n,e.src=a(),e.complete===!0&&t(e)})}}function l(t,n){var r,o,i=e.createElement("div"),s=e.createElement("img"),c=e.createElement("span"),h="Hidden Text";i.style.visibility="hidden",i.style.fontFamily=t,i.style.fontSize=n,i.style.margin=0,i.style.padding=0,e.body.appendChild(i),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)),i.appendChild(c),i.appendChild(s),r=s.offsetTop-c.offsetTop+1,i.removeChild(c),i.appendChild(e.createTextNode(h)),i.style.lineHeight="normal",s.style.verticalAlign="super",o=s.offsetTop-i.offsetTop+1,e.body.removeChild(i),this.baseline=r,this.lineWidth=1,this.middle=o}function d(){this.data={}}function f(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 g(t,e){this.src=t,this.image=new Image;var n=this;this.promise=new Promise(function(r,o){n.image.onload=r,n.image.onerror=o,e&&(n.image.crossOrigin="anonymous"),n.image.src=t,n.image.complete===!0&&r(n.image)})["catch"](function(){var e=new p(t);return e.promise.then(function(t){n.image=t})})}function m(e,n){this.link=null,this.options=e,this.support=n,this.origin=t.location.protocol+t.location.hostname+t.location.port}function y(t){return"IMG"===t.node.nodeName}function v(t){return{args:[t.node.src],method:"url"}}function w(t){f.apply(this,arguments),this.type=this.TYPES.LINEAR,t.args[0].split(" ").forEach(function(t){switch(t){case"left":this.x0=0,this.x
break;case 2:a=a+e.height-r[2].width,h=r[2].width,t.args=D({c1:[s+c,a+h],c2:[s,a+h],c3:[s+r[3].width,a],c4:[s+c-r[3].width,a]},n[2],n[3],o.bottomRightOuter,o.bottomRightInner,o.bottomLeftOuter,o.bottomLeftInner);break;case 3:c=r[3].width,t.args=D({c1:[s,a+h+r[2].width],c2:[s,a],c3:[s+c,a+r[0].width],c4:[s+c,a+h]},n[3],n[0],o.bottomLeftOuter,o.bottomLeftInner,o.topLeftOuter,o.topLeftInner)}}return t})}},B.prototype.parseBackgroundClip=function(t,e,n,r,o){var i=t.css("backgroundClip"),s=[];switch(i){case"content-box":case"padding-box":_(s,r[0],r[1],e.topLeftInner,e.topRightInner,o.left+n[3].width,o.top+n[0].width),_(s,r[1],r[2],e.topRightInner,e.bottomRightInner,o.left+o.width-n[1].width,o.top+n[0].width),_(s,r[2],r[3],e.bottomRightInner,e.bottomLeftInner,o.left+o.width-n[1].width,o.top+o.height-n[2].width),_(s,r[3],r[0],e.bottomLeftInner,e.topLeftInner,o.left+n[3].width,o.top+o.height-n[2].width);break;default:_(s,r[0],r[1],e.topLeftOuter,e.topRightOuter,o.left,o.top),_(s,r[1],r[2],e.topRightOuter,e.bottomRightOuter,o.left+o.width,o.top),_(s,r[2],r[3],e.bottomRightOuter,e.bottomLeftOuter,o.left+o.width,o.top+o.height),_(s,r[3],r[0],e.bottomLeftOuter,e.topLeftOuter,o.left,o.top+o.height)}return s},B.prototype.pseudoHideClass="___html2canvas___pseudoelement";var ye=0;he.prototype.renderImage=function(t,e,n,r){var o=t.cssInt("paddingLeft"),i=t.cssInt("paddingTop"),s=t.cssInt("paddingRight"),a=t.cssInt("paddingBottom"),c=n.borders;this.drawImage(r,0,0,r.width,r.height,e.left+o+c[3].width,e.top+i+c[0].width,e.width-(c[1].width+c[3].width+o+s),e.height-(c[0].width+c[2].width+i+a))},he.prototype.renderBackground=function(t,e,n){e.height>0&&e.width>0&&(this.renderBackgroundColor(t,e),this.renderBackgroundImage(t,e,n))},he.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"))},he.prototype.renderBorders=function(t){t.forEach(this.renderBorder,this)},he.prototype.renderBorder=function(t){this.isTransparent(t.color)||null===t.args||this.drawShape(t.args,t.color)},he.prototype.renderBackgroundImage=function(t,e,n){var r=t.parseBackgroundImages();r.reverse().forEach(function(r,o,i){switch(r.method){case"url":var s=this.images.get(r.args[0]);s?this.renderBackgroundRepeating(t,e,s,i.length-(o+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)},he.prototype.renderBackgroundRepeating=function(t,e,n,r,o){var i=t.parseBackgroundSize(e,n.image,r),s=t.parseBackgroundPosition(e,n.image,r,i),a=t.parseBackgroundRepeat(r);switch(a){case"repeat-x":case"repeat no-repeat":this.backgroundRepeatShape(n,s,i,e,e.left+o[3],e.top+s.top+o[0],99999,n.image.height,o);break;case"repeat-y":case"no-repeat repeat":this.backgroundRepeatShape(n,s,i,e,e.left+s.left+o[3],e.top+o[0],n.image.width,99999,o);break;case"no-repeat":this.backgroundRepeatShape(n,s,i,e,e.left+s.left+o[3],e.top+s.top+o[0],n.image.width,n.image.height,o);break;default:this.renderBackgroundRepeat(n,s,i,{top:e.top,left:e.left},o[3],o[0])}},he.prototype.isTransparent=function(t){return!t||"transparent"===t||"rgba(0, 0, 0, 0)"===t},ue.prototype=Object.create(he.prototype),ue.prototype.setFillStyle=function(t){return this.ctx.fillStyle=t,this.ctx},ue.prototype.rectangle=function(t,e,n,r,o){this.setFillStyle(o).fillRect(t,e,n,r)},ue.prototype.drawShape=function(t,e){this.shape(t),this.setFillStyle(e).fill()},ue.prototype.drawImage=function(t,e,n,r,o,i,s,a,c){this.ctx.drawImage(t,e,n,r,o,i,s,a,c)},ue.prototype.clip=function(t,e,n){this.ctx.save(),this.shape(t).clip(),e.call(n),this.ctx.restore()},ue.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},ue.prototype.font=function(t,e,n,r,o,i){this.setFillStyle(t).font=[e,n