Added timer/statistics for rendering

This commit is contained in:
Niklas von Hertzen 2011-07-17 02:42:45 +03:00
parent 3193e1a20b
commit 45381454b4
7 changed files with 61 additions and 21 deletions

View File

@ -59,6 +59,7 @@ function html2canvas(el, userOptions) {
this.imagesLoaded = 0;
this.images = [];
this.fontData = [];
this.numDraws = 0;
this.ignoreElements = "IFRAME|OBJECT|PARAM";
this.ignoreRe = new RegExp("("+this.ignoreElements+")");
@ -75,7 +76,9 @@ function html2canvas(el, userOptions) {
}*/
// Start script
this.init();
this.init();
return this;
}
@ -174,7 +177,7 @@ html2canvas.prototype.finish = function(){
newCanvas.height = window.innerHeight;
}
this.opts.ready(this.canvas);
this.opts.ready(this);
}
@ -396,6 +399,7 @@ html2canvas.prototype.drawBackgroundRepeat = function(image,x,y,width,height,elx
width-sourceX, // destination width
height-sourceY // destination height
);
this.numDraws++;
}
/*
* Function to provide border details for an element
@ -506,6 +510,7 @@ html2canvas.prototype.newElement = function(el){
if (el.nodeName=="IMG"){
image = _.loadImage(_.getAttr(el,'src'));
if (image){
this.ctx.drawImage(
image,
0, //sx
@ -518,6 +523,7 @@ html2canvas.prototype.newElement = function(el){
bounds.height - (borders[0].width + borders[2].width + parseInt(_.getCSS(el,'padding-top'),10) + parseInt(_.getCSS(el,'padding-bottom'),10)) //dh
);
this.numDraws++;
}else{
this.log("Error loading <img>:" + _.getAttr(el,'src'));
}
@ -539,6 +545,7 @@ html2canvas.prototype.newElement = function(el){
html2canvas.prototype.printText = function(currentText,x,y){
if (this.trim(currentText).length>0){
this.ctx.fillText(currentText,x,y);
this.numDraws++;
}
}
@ -549,6 +556,7 @@ html2canvas.prototype.newRect = function(x,y,w,h,bgcolor){
if (bgcolor!="transparent"){
this.ctx.fillStyle = bgcolor;
this.ctx.fillRect (x, y, w, h);
this.numDraws++;
}
}
/*

View File

@ -6,20 +6,20 @@
* 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});this.element=a;this.imagesLoaded=0;this.images=[];this.fontData=[];this.ignoreElements="IFRAME|OBJECT|PARAM";this.ignoreRe=RegExp("("+this.ignoreElements+")");this.useRangeBounds=!1;this.init()}
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});this.element=a;this.imagesLoaded=0;this.images=[];this.fontData=[];this.numDraws=0;this.ignoreElements="IFRAME|OBJECT|PARAM";this.ignoreRe=RegExp("("+this.ignoreElements+")");this.useRangeBounds=!1;this.init();return this}
html2canvas.prototype.init=function(){var a=this;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?(this.ctx.textBaseline="bottom",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.newElement(this.element),this.parseElement(this.element)};
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.canvas)};
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){var c=this.getCSS(a,"background-image"),e=this.getCSS(a,"background-repeat");if(typeof c!="undefined"&&/^(1|none)$/.test(c)==!1){var c=this.backgroundImageUrl(c),d=this.loadImage(c),i=this.getBackgroundPosition(a,b,d);if(d)switch(e){case "repeat-x":this.drawbackgroundRepeatX(d,i,b.left,b.top,b.width,b.height);break;case "repeat-y":this.drawbackgroundRepeatY(d,i,b.left,b.top,b.width,b.height);break;case "no-repeat":this.drawBackgroundRepeat(d,i.left+
b.left,i.top+b.top,Math.min(b.width,d.width),Math.min(b.height,d.height),b.left,b.top);break;default:var f;i.top-=Math.ceil(i.top/d.height)*d.height;for(c=b.top+i.top;c<b.height+b.top;)e=Math.min(d.height,b.height+b.top-c),e=Math.floor(c+d.height)>e+c?e+c-c:d.height,c<b.top?(f=b.top-c,c=b.top):f=0,this.drawbackgroundRepeatX(d,i,b.left,c,b.width,e),f>0&&(i.top+=f),c=Math.floor(c+d.height)-f}else this.log("Error loading background:"+c)}};
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(" "),e,d;a.length==1&&(e=a,a=[],a[0]=e,a[1]=e);a[0].toString().indexOf("%")!=-1?(d=parseFloat(a[0])/100,e=b.width*d-c.width*d):e=parseInt(a[0],10);a[1].toString().indexOf("%")!=-1?(d=parseFloat(a[1])/100,b=b.height*d-c.height*d):b=parseInt(a[1],10);return{top:b,left:e}};
html2canvas.prototype.drawbackgroundRepeatY=function(a,b,c,e,d,i){var f=Math.min(a.width,d),g;b.top-=Math.ceil(b.top/a.height)*a.height;for(g=e+b.top;g<i+e;)d=Math.floor(g+a.height)>i+e?i+e-g:a.height,this.drawBackgroundRepeat(a,c+b.left,g,f,d,c,e),g=Math.floor(g+a.height)};
html2canvas.prototype.drawbackgroundRepeatX=function(a,b,c,e,d,i){var i=Math.min(a.height,i),f,g;b.left-=Math.ceil(b.left/a.width)*a.width;for(g=c+b.left;g<d+c;)f=Math.floor(g+a.width)>d+c?d+c-g:a.width,this.drawBackgroundRepeat(a,g,e+b.top,f,i,c,e),g=Math.floor(g+a.width)};html2canvas.prototype.drawBackgroundRepeat=function(a,b,c,e,d,i,f){var g=0,h=0;i-b>0&&(g=i-b);f-c>0&&(h=f-c);this.ctx.drawImage(a,g,h,e-g,d-h,b+g,c+h,e-g,d-h)};
html2canvas.prototype.drawbackgroundRepeatX=function(a,b,c,e,d,i){var i=Math.min(a.height,i),f,g;b.left-=Math.ceil(b.left/a.width)*a.width;for(g=c+b.left;g<d+c;)f=Math.floor(g+a.width)>d+c?d+c-g:a.width,this.drawBackgroundRepeat(a,g,e+b.top,f,i,c,e),g=Math.floor(g+a.width)};html2canvas.prototype.drawBackgroundRepeat=function(a,b,c,e,d,i,f){var g=0,h=0;i-b>0&&(g=i-b);f-c>0&&(h=f-c);this.ctx.drawImage(a,g,h,e-g,d-h,b+g,c+h,e-g,d-h);this.numDraws++};
html2canvas.prototype.getBorderData=function(a){var b=[],c=this;this.each(["top","right","bottom","left"],function(e,d){b.push({width:parseInt(c.getCSS(a,"border-"+d+"-width"),10),color:c.getCSS(a,"border-"+d+"-color")})});return b};
html2canvas.prototype.newElement=function(a){var b=this.getBounds(a),c=b.left,e=b.top,d=b.width,i=b.height,f=this,g;g=this.getCSS(a,"background-color");var h=this.getBorderData(a);this.each(h,function(a,b){if(b.width>0){var g=c,j=e,m=d,n=i-h[2].width;switch(a){case 0:n=h[0].width;break;case 1:g=c+d-h[1].width;m=h[1].width;break;case 2:j=j+i-h[2].width;n=h[2].width;break;case 3:m=h[3].width}f.newRect(g,j,m,n,b.color)}});this.ignoreRe.test(a.nodeName)&&this.opts.iframeDefault!="transparent"&&(g=this.opts.iframeDefault==
"default"?"#efefef":this.opts.iframeDefault);this.newRect(c+h[3].width,e+h[0].width,d-(h[1].width+h[3].width),i-(h[0].width+h[2].width),g);this.drawBackground(a,{left:c+h[3].width,top:e+h[0].width,width:d-(h[1].width+h[3].width),height:i-(h[0].width+h[2].width)});a.nodeName=="IMG"&&((g=f.loadImage(f.getAttr(a,"src")))?this.ctx.drawImage(g,0,0,g.width,g.height,c+parseInt(f.getCSS(a,"padding-left"),10)+h[3].width,e+parseInt(f.getCSS(a,"padding-top"),10)+h[0].width,b.width-(h[1].width+h[3].width+parseInt(f.getCSS(a,
"padding-left"),10)+parseInt(f.getCSS(a,"padding-right"),10)),b.height-(h[0].width+h[2].width+parseInt(f.getCSS(a,"padding-top"),10)+parseInt(f.getCSS(a,"padding-bottom"),10))):this.log("Error loading <img>:"+f.getAttr(a,"src")))};html2canvas.prototype.printText=function(a,b,c){this.trim(a).length>0&&this.ctx.fillText(a,b,c)};html2canvas.prototype.newRect=function(a,b,c,e,d){if(d!="transparent")this.ctx.fillStyle=d,this.ctx.fillRect(a,b,c,e)};
"default"?"#efefef":this.opts.iframeDefault);this.newRect(c+h[3].width,e+h[0].width,d-(h[1].width+h[3].width),i-(h[0].width+h[2].width),g);this.drawBackground(a,{left:c+h[3].width,top:e+h[0].width,width:d-(h[1].width+h[3].width),height:i-(h[0].width+h[2].width)});a.nodeName=="IMG"&&((g=f.loadImage(f.getAttr(a,"src")))?(this.ctx.drawImage(g,0,0,g.width,g.height,c+parseInt(f.getCSS(a,"padding-left"),10)+h[3].width,e+parseInt(f.getCSS(a,"padding-top"),10)+h[0].width,b.width-(h[1].width+h[3].width+parseInt(f.getCSS(a,
"padding-left"),10)+parseInt(f.getCSS(a,"padding-right"),10)),b.height-(h[0].width+h[2].width+parseInt(f.getCSS(a,"padding-top"),10)+parseInt(f.getCSS(a,"padding-bottom"),10))),this.numDraws++):this.log("Error loading <img>:"+f.getAttr(a,"src")))};html2canvas.prototype.printText=function(a,b,c){this.trim(a).length>0&&(this.ctx.fillText(a,b,c),this.numDraws++)};html2canvas.prototype.newRect=function(a,b,c,e,d){if(d!="transparent")this.ctx.fillStyle=d,this.ctx.fillRect(a,b,c,e),this.numDraws++};
html2canvas.prototype.getImages=function(a){var b=this;this.ignoreRe.test(a.nodeName)||this.each($(a).contents(),function(a,e){RegExp("("+this.ignoreElements+")").test(e.nodeName)||b.getImages(e)});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.newText=function(a,b){var c=this.getCSS(a,"font-family"),e=this.getCSS(a,"font-size"),d=this.getCSS(a,"color"),i=this.getCSS(a,"font-weight"),f=this.getCSS(a,"font-style"),g=this.getCSS(a,"text-decoration");b.nodeValue=this.textTransform(b.nodeValue,this.getCSS(a,"text-transform"));var h=b.nodeValue;if(h.length>0){switch(i){case "401":i="bold"}if(g!="none")var k=this.fontMetrics(c,e);this.ctx.font=i+" "+f+" "+e+" "+c;this.ctx.fillStyle=d;c=b;for(e=0;e<h.length;e++){i=c.splitText(1);

View File

@ -43,18 +43,28 @@
new html2canvas(this.get(0), {
logging: true,
ready: function(canvas) {
ready: function(renderer) {
var finishTime = new Date();
// console.log((finishTime.getTime()-timer)/1000);
document.body.appendChild(renderer.canvas);
document.body.appendChild(canvas);
var canvas = $(canvas);
var canvas = $(renderer.canvas);
canvas.css('position','absolute')
.css('left',0).css('top',0);
// $('body').append(canvas);
$(canvas).siblings().toggle();
throwMessage('Screenshot created in '+ ((finishTime.getTime()-timer)/1000) + " seconds<br />Total of "+renderer.numDraws+" draws performed",4000);
$(window).click(function(){
if (!canvas.is(':visible')){
$(canvas).toggle().siblings().toggle();
@ -72,15 +82,16 @@
});
function throwMessage(msg){
function throwMessage(msg,duration){
window.clearTimeout(timeoutTimer);
timeoutTimer = window.setTimeout(function(){
message.fadeOut(function(){
message.remove();
});
},2000);
},duration || 2000);
$(message).remove();
message = $('<div />').text(msg).css({
message = $('<div />').html(msg).css({
margin:0,
padding:10,
background: "#000",
@ -94,6 +105,7 @@
borderRadius:12,
width:'auto',
height:'auto',
textAlign:'center',
textDecoration:'none'
}).hide().fadeIn().appendTo('body');
}

View File

@ -216,4 +216,5 @@ html2canvas.prototype.drawBackgroundRepeat = function(image,x,y,width,height,elx
width-sourceX, // destination width
height-sourceY // destination height
);
this.numDraws++;
}

View File

@ -29,6 +29,7 @@ function html2canvas(el, userOptions) {
this.imagesLoaded = 0;
this.images = [];
this.fontData = [];
this.numDraws = 0;
this.ignoreElements = "IFRAME|OBJECT|PARAM";
this.ignoreRe = new RegExp("("+this.ignoreElements+")");
@ -45,7 +46,9 @@ function html2canvas(el, userOptions) {
}*/
// Start script
this.init();
this.init();
return this;
}
@ -144,6 +147,6 @@ html2canvas.prototype.finish = function(){
newCanvas.height = window.innerHeight;
}
this.opts.ready(this.canvas);
this.opts.ready(this);
}

View File

@ -89,6 +89,7 @@ html2canvas.prototype.newElement = function(el){
if (el.nodeName=="IMG"){
image = _.loadImage(_.getAttr(el,'src'));
if (image){
this.ctx.drawImage(
image,
0, //sx
@ -101,6 +102,7 @@ html2canvas.prototype.newElement = function(el){
bounds.height - (borders[0].width + borders[2].width + parseInt(_.getCSS(el,'padding-top'),10) + parseInt(_.getCSS(el,'padding-bottom'),10)) //dh
);
this.numDraws++;
}else{
this.log("Error loading <img>:" + _.getAttr(el,'src'));
}
@ -122,6 +124,7 @@ html2canvas.prototype.newElement = function(el){
html2canvas.prototype.printText = function(currentText,x,y){
if (this.trim(currentText).length>0){
this.ctx.fillText(currentText,x,y);
this.numDraws++;
}
}
@ -132,5 +135,6 @@ html2canvas.prototype.newRect = function(x,y,w,h,bgcolor){
if (bgcolor!="transparent"){
this.ctx.fillStyle = bgcolor;
this.ctx.fillRect (x, y, w, h);
this.numDraws++;
}
}

View File

@ -13,18 +13,28 @@
new html2canvas(this.get(0), {
logging: true,
ready: function(canvas) {
ready: function(renderer) {
var finishTime = new Date();
// console.log((finishTime.getTime()-timer)/1000);
document.body.appendChild(renderer.canvas);
document.body.appendChild(canvas);
var canvas = $(canvas);
var canvas = $(renderer.canvas);
canvas.css('position','absolute')
.css('left',0).css('top',0);
// $('body').append(canvas);
$(canvas).siblings().toggle();
throwMessage('Screenshot created in '+ ((finishTime.getTime()-timer)/1000) + " seconds<br />Total of "+renderer.numDraws+" draws performed",4000);
$(window).click(function(){
if (!canvas.is(':visible')){
$(canvas).toggle().siblings().toggle();
@ -42,15 +52,16 @@
});
function throwMessage(msg){
function throwMessage(msg,duration){
window.clearTimeout(timeoutTimer);
timeoutTimer = window.setTimeout(function(){
message.fadeOut(function(){
message.remove();
});
},2000);
},duration || 2000);
$(message).remove();
message = $('<div />').text(msg).css({
message = $('<div />').html(msg).css({
margin:0,
padding:10,
background: "#000",
@ -64,6 +75,7 @@
borderRadius:12,
width:'auto',
height:'auto',
textAlign:'center',
textDecoration:'none'
}).hide().fadeIn().appendTo('body');
}