Skipping visibility:hidden and display:none elements

This commit is contained in:
Niklas von Hertzen 2011-07-18 00:39:56 +03:00
parent a27290caae
commit 3c4581b5c4
5 changed files with 53 additions and 12 deletions

View File

@ -6,7 +6,7 @@
* Released under MIT License
*/
function html2canvas(a,b){this.opts=this.extendObj(b||{},{logging:!1,ready:function(a){document.body.appendChild(a)},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+
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",this.parseElement(this.element,this.newElement(this.element,this.ctx)||this.ctx)};
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};
@ -36,7 +36,7 @@ html2canvas.prototype.fontMetrics=function(a,b){var c=this.fontData.indexOf(a+"-
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,{ctx:b})});this.canvasRenderer(this.contextStacks);this.finish()};
html2canvas.prototype.parsing=function(a,b){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.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)};

View File

@ -10,8 +10,8 @@ function html2canvas(el, userOptions) {
this.opts = this.extendObj(options, {
logging: false,
ready: function (canvas) {
document.body.appendChild(canvas);
ready: function (stack) {
document.body.appendChild(stack.canvas);
},
iframeDefault: "default",
flashCanvasPath: "http://html2canvas.hertzen.com/external/flashcanvas/flashcanvas.js",

View File

@ -3,7 +3,7 @@
html2canvas.prototype.newElement = function(el,parentStack){
var bounds = this.getBounds(el);
var x = bounds.left;

View File

@ -2,10 +2,10 @@
html2canvas.prototype.parseElement = function(element,ctx){
var _ = this;
this.each(element.children,function(index,el){
this.each(element.children,function(index,el){
_.parsing(el,{
ctx:ctx
});
});
});
this.canvasRenderer(this.contextStacks);
@ -15,10 +15,12 @@ html2canvas.prototype.parseElement = function(element,ctx){
html2canvas.prototype.parsing = function(el,stack){
if (this.getCSS(el,'display') != "none" && this.getCSS(el,'visibility')!="hidden"){
var _ = this;
var _ = this;
//if (!this.blockElements.test(el.nodeName)){
//if (!this.blockElements.test(el.nodeName)){
stack = this.newElement(el,stack) || stack;
@ -61,7 +63,7 @@ html2canvas.prototype.parsing = function(el,stack){
}
}
// }
}
// }
}

View File

@ -0,0 +1,39 @@
<!--
* @author Niklas von Hertzen <niklas at hertzen.com>
* @created 16.7.2011
* @website http://hertzen.com
-->
<!DOCTYPE html>
<html>
<head>
<title>Visible elements tests</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="#" type="text/css" rel="stylesheet">
<script type="text/javascript" src="../external/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../build/html2canvas.js"></script>
<script type="text/javascript" src="../build/jquery.plugin.html2canvas.js"></script>
<script type="text/javascript">
$(window).ready(function() {
new html2canvas($('body').get(0));
});
</script>
<style>
div{
border:2px solid black;
}
</style>
</head>
<body>
<h1>Display:none and visible:hidden tests</h1>
<div>This should be visible </div>
<div style="display:none">display:none, This should be <b>hidden</b></div>
<div style="visibility:hidden">visibility:hidden, This should be <b>hidden</b></div>
<hr />
<div style="display:none">display:none, This should be <b>hidden</b></div>
<div style="visibility:hidden">visibility:hidden, This should be <b>hidden</b></div>
</body>
</html>