fixed background-repeat / background-position problem in IE9 and Opera

This commit is contained in:
Niklas von Hertzen 2011-07-17 01:34:34 +03:00
parent b6bb64e06d
commit 4e19e99756
5 changed files with 76 additions and 59 deletions

View File

@ -43,7 +43,8 @@ function html2canvas(el, userOptions) {
ready: function (canvas) { ready: function (canvas) {
document.body.appendChild(canvas); document.body.appendChild(canvas);
}, },
renderViewport: true flashCanvasPath: "http://html2canvas.hertzen.com/external/flashcanvas/flashcanvas.js",
renderViewport: false
}); });
this.element = el; this.element = el;
@ -91,24 +92,23 @@ html2canvas.prototype.init = function(){
if (!this.canvas.getContext){ if (!this.canvas.getContext){
// TODO include Flashcanvas // TODO include Flashcanvas
/* /*
var script = document.createElement('script');
script.type = "text/javascript";
script.src = this.opts.flashCanvasPath;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(script, s);
var script = document.createElement('script'); if (typeof FlashCanvas != "undefined") {
script.type = "text/javascript";
script.src = "flashcanvas.js";
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(script, s);
FlashCanvas.initElement(this.canvas);
this.ctx = this.canvas.getContext('2d');
} */
if (typeof FlashCanvas != "undefined") {
FlashCanvas.initElement(canvas);
ctx = canvas.getContext('2d');
}
*/
}else{ }else{
this.ctx = this.canvas.getContext('2d'); this.ctx = this.canvas.getContext('2d');
} }
@ -220,7 +220,7 @@ html2canvas.prototype.drawBackground = function(el,bounds){
bgp.top = bgp.top-Math.ceil(bgp.top/image.height)*image.height; bgp.top = bgp.top-Math.ceil(bgp.top/image.height)*image.height;
for(bgy=(bounds.top+bgp.top);bgy<=bounds.height+bounds.top;){ for(bgy=(bounds.top+bgp.top);bgy<bounds.height+bounds.top;){
@ -290,16 +290,24 @@ html2canvas.prototype.getBackgroundPosition = function(el,bounds,image){
left, left,
percentage; percentage;
if (bgposition.length==1){
var val = bgposition,
bgposition = [];
if (bgposition[0].indexOf("%")!=-1){ bgposition[0] = val,
bgposition[1] = val;
}
if (bgposition[0].toString().indexOf("%")!=-1){
percentage = (parseFloat(bgposition[0])/100); percentage = (parseFloat(bgposition[0])/100);
left = ((bounds.width * percentage)-(image.width*percentage)); left = ((bounds.width * percentage)-(image.width*percentage));
}else{ }else{
left = parseInt(bgposition[0],10); left = parseInt(bgposition[0],10);
} }
if (bgposition[1].indexOf("%")!=-1){ if (bgposition[1].toString().indexOf("%")!=-1){
percentage = (parseFloat(bgposition[1])/100); percentage = (parseFloat(bgposition[1])/100);
top = ((bounds.height * percentage)-(image.height*percentage)); top = ((bounds.height * percentage)-(image.height*percentage));
@ -325,7 +333,7 @@ html2canvas.prototype.drawbackgroundRepeatY = function(image,bgp,x,y,w,h){
bgp.top = bgp.top-Math.ceil(bgp.top/image.height)*image.height; bgp.top = bgp.top-Math.ceil(bgp.top/image.height)*image.height;
for(bgy=(y+bgp.top);bgy<=h+y;){ for(bgy=(y+bgp.top);bgy<h+y;){
if ( Math.floor(bgy+image.height)>h+y){ if ( Math.floor(bgy+image.height)>h+y){
@ -349,7 +357,7 @@ html2canvas.prototype.drawbackgroundRepeatX = function(image,bgp,x,y,w,h){
bgp.left = bgp.left-Math.ceil(bgp.left/image.width)*image.width; bgp.left = bgp.left-Math.ceil(bgp.left/image.width)*image.width;
for(bgx=(x+bgp.left);bgx<=w+x;){ for(bgx=(x+bgp.left);bgx<w+x;){
if (Math.floor(bgx+image.width)>w+x){ if (Math.floor(bgx+image.width)>w+x){
width = (w+x)-bgx; width = (w+x)-bgx;

View File

@ -6,15 +6,16 @@
* Released under MIT License * Released under MIT License
*/ */
function html2canvas(a,b){this.opts=this.extendObj(b||{},{logging:!1,ready:function(a){document.body.appendChild(a)},renderViewport:!0});this.element=a;this.imagesLoaded=0;this.images=[];this.fontData=[];this.ignoreElements="IFRAME|OBJECT|PARAM";this.useRangeBounds=!1;this.init()} function html2canvas(a,b){this.opts=this.extendObj(b||{},{logging:!1,ready:function(a){document.body.appendChild(a)},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.useRangeBounds=!1;this.init()}
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.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.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.canvas)};
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+ 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)}}; 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 e=this.getCSS(a,"background-position").split(" "),d;e[0].indexOf("%")!=-1?(d=parseFloat(e[0])/100,a=b.width*d-c.width*d):a=parseInt(e[0],10);e[1].indexOf("%")!=-1?(d=parseFloat(e[1])/100,b=b.height*d-c.height*d):b=parseInt(e[1],10);return{top:b,left:a}}; 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.drawbackgroundRepeatY=function(a,b,c,e,d,i){var f=Math.min(a.width,d),h;b.top-=Math.ceil(b.top/a.height)*a.height;for(h=e+b.top;h<=i+e;)d=Math.floor(h+a.height)>i+e?i+e-h:a.height,this.drawBackgroundRepeat(a,c+b.left,h,f,d,c,e),h=Math.floor(h+a.height)}; 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.drawbackgroundRepeatX=function(a,b,c,e,d,i){var i=Math.min(a.height,i),f,h;b.left-=Math.ceil(b.left/a.width)*a.width;for(h=c+b.left;h<=d+c;)f=Math.floor(h+a.width)>d+c?d+c-h:a.width,this.drawBackgroundRepeat(a,h,e+b.top,f,i,c,e),h=Math.floor(h+a.width)};html2canvas.prototype.drawBackgroundRepeat=function(a,b,c,e,d,i,f){var h=0,g=0;i-b>0&&(h=i-b);f-c>0&&(g=f-c);this.ctx.drawImage(a,h,g,e-h,d-g,b+h,c+g,e-h,d-g)}; html2canvas.prototype.drawbackgroundRepeatY=function(a,b,c,e,d,i){var f=Math.min(a.width,d),h;b.top-=Math.ceil(b.top/a.height)*a.height;for(h=e+b.top;h<i+e;)d=Math.floor(h+a.height)>i+e?i+e-h:a.height,this.drawBackgroundRepeat(a,c+b.left,h,f,d,c,e),h=Math.floor(h+a.height)};
html2canvas.prototype.drawbackgroundRepeatX=function(a,b,c,e,d,i){var i=Math.min(a.height,i),f,h;b.left-=Math.ceil(b.left/a.width)*a.width;for(h=c+b.left;h<d+c;)f=Math.floor(h+a.width)>d+c?d+c-h:a.width,this.drawBackgroundRepeat(a,h,e+b.top,f,i,c,e),h=Math.floor(h+a.width)};html2canvas.prototype.drawBackgroundRepeat=function(a,b,c,e,d,i,f){var h=0,g=0;i-b>0&&(h=i-b);f-c>0&&(g=f-c);this.ctx.drawImage(a,h,g,e-h,d-g,b+h,c+g,e-h,d-g)};
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.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,h;h=this.getCSS(a,"background-color");var g=this.getBorderData(a);this.each(g,function(a,b){if(b.width>0){var h=c,j=e,m=d,n=i-g[2].width;switch(a){case 0:n=g[0].width;break;case 1:h=c+d-g[1].width;m=g[1].width;break;case 2:j=j+i-g[2].width;n=g[2].width;break;case 3:m=g[3].width}f.newRect(h,j,m,n,b.color)}});this.newRect(c+g[3].width,e+g[0].width,d-(g[1].width+g[3].width),i-(g[0].width+ html2canvas.prototype.newElement=function(a){var b=this.getBounds(a),c=b.left,e=b.top,d=b.width,i=b.height,f=this,h;h=this.getCSS(a,"background-color");var g=this.getBorderData(a);this.each(g,function(a,b){if(b.width>0){var h=c,j=e,m=d,n=i-g[2].width;switch(a){case 0:n=g[0].width;break;case 1:h=c+d-g[1].width;m=g[1].width;break;case 2:j=j+i-g[2].width;n=g[2].width;break;case 3:m=g[3].width}f.newRect(h,j,m,n,b.color)}});this.newRect(c+g[3].width,e+g[0].width,d-(g[1].width+g[3].width),i-(g[0].width+
g[2].width),h);this.drawBackground(a,{left:c+g[3].width,top:e+g[0].width,width:d-(g[1].width+g[3].width),height:i-(g[0].width+g[2].width)});a.nodeName=="IMG"&&((h=f.loadImage(f.getAttr(a,"src")))?this.ctx.drawImage(h,0,0,h.width,h.height,c+parseInt(f.getCSS(a,"padding-left"),10)+g[3].width,e+parseInt(f.getCSS(a,"padding-top"),10)+g[0].width,b.width-(g[1].width+g[3].width+parseInt(f.getCSS(a,"padding-left"),10)+parseInt(f.getCSS(a,"padding-right"),10)),b.height-(g[0].width+g[2].width+parseInt(f.getCSS(a, g[2].width),h);this.drawBackground(a,{left:c+g[3].width,top:e+g[0].width,width:d-(g[1].width+g[3].width),height:i-(g[0].width+g[2].width)});a.nodeName=="IMG"&&((h=f.loadImage(f.getAttr(a,"src")))?this.ctx.drawImage(h,0,0,h.width,h.height,c+parseInt(f.getCSS(a,"padding-left"),10)+g[3].width,e+parseInt(f.getCSS(a,"padding-top"),10)+g[0].width,b.width-(g[1].width+g[3].width+parseInt(f.getCSS(a,"padding-left"),10)+parseInt(f.getCSS(a,"padding-right"),10)),b.height-(g[0].width+g[2].width+parseInt(f.getCSS(a,

View File

@ -41,7 +41,7 @@ html2canvas.prototype.drawBackground = function(el,bounds){
bgp.top = bgp.top-Math.ceil(bgp.top/image.height)*image.height; bgp.top = bgp.top-Math.ceil(bgp.top/image.height)*image.height;
for(bgy=(bounds.top+bgp.top);bgy<=bounds.height+bounds.top;){ for(bgy=(bounds.top+bgp.top);bgy<bounds.height+bounds.top;){
@ -111,16 +111,24 @@ html2canvas.prototype.getBackgroundPosition = function(el,bounds,image){
left, left,
percentage; percentage;
if (bgposition.length==1){
var val = bgposition,
bgposition = [];
if (bgposition[0].indexOf("%")!=-1){ bgposition[0] = val,
bgposition[1] = val;
}
if (bgposition[0].toString().indexOf("%")!=-1){
percentage = (parseFloat(bgposition[0])/100); percentage = (parseFloat(bgposition[0])/100);
left = ((bounds.width * percentage)-(image.width*percentage)); left = ((bounds.width * percentage)-(image.width*percentage));
}else{ }else{
left = parseInt(bgposition[0],10); left = parseInt(bgposition[0],10);
} }
if (bgposition[1].indexOf("%")!=-1){ if (bgposition[1].toString().indexOf("%")!=-1){
percentage = (parseFloat(bgposition[1])/100); percentage = (parseFloat(bgposition[1])/100);
top = ((bounds.height * percentage)-(image.height*percentage)); top = ((bounds.height * percentage)-(image.height*percentage));
@ -146,7 +154,7 @@ html2canvas.prototype.drawbackgroundRepeatY = function(image,bgp,x,y,w,h){
bgp.top = bgp.top-Math.ceil(bgp.top/image.height)*image.height; bgp.top = bgp.top-Math.ceil(bgp.top/image.height)*image.height;
for(bgy=(y+bgp.top);bgy<=h+y;){ for(bgy=(y+bgp.top);bgy<h+y;){
if ( Math.floor(bgy+image.height)>h+y){ if ( Math.floor(bgy+image.height)>h+y){
@ -170,7 +178,7 @@ html2canvas.prototype.drawbackgroundRepeatX = function(image,bgp,x,y,w,h){
bgp.left = bgp.left-Math.ceil(bgp.left/image.width)*image.width; bgp.left = bgp.left-Math.ceil(bgp.left/image.width)*image.width;
for(bgx=(x+bgp.left);bgx<=w+x;){ for(bgx=(x+bgp.left);bgx<w+x;){
if (Math.floor(bgx+image.width)>w+x){ if (Math.floor(bgx+image.width)>w+x){
width = (w+x)-bgx; width = (w+x)-bgx;

View File

@ -13,7 +13,8 @@ function html2canvas(el, userOptions) {
ready: function (canvas) { ready: function (canvas) {
document.body.appendChild(canvas); document.body.appendChild(canvas);
}, },
renderViewport: true flashCanvasPath: "http://html2canvas.hertzen.com/external/flashcanvas/flashcanvas.js",
renderViewport: false
}); });
this.element = el; this.element = el;
@ -61,24 +62,23 @@ html2canvas.prototype.init = function(){
if (!this.canvas.getContext){ if (!this.canvas.getContext){
// TODO include Flashcanvas // TODO include Flashcanvas
/* /*
var script = document.createElement('script');
script.type = "text/javascript";
script.src = this.opts.flashCanvasPath;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(script, s);
var script = document.createElement('script'); if (typeof FlashCanvas != "undefined") {
script.type = "text/javascript";
script.src = "flashcanvas.js";
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(script, s);
FlashCanvas.initElement(this.canvas);
this.ctx = this.canvas.getContext('2d');
} */
if (typeof FlashCanvas != "undefined") {
FlashCanvas.initElement(canvas);
ctx = canvas.getContext('2d');
}
*/
}else{ }else{
this.ctx = this.canvas.getContext('2d'); this.ctx = this.canvas.getContext('2d');
} }