html2canvas.prototype.drawBackground = function(el,bounds){ var background_image = this.getCSS(el,"background-image"); var background_repeat = this.getCSS(el,"background-repeat"); if (typeof background_image != "undefined" && /^(1|none)$/.test(background_image)==false){ background_image = this.backgroundImageUrl(background_image); var image = this.loadImage(background_image); var bgp = this.getBackgroundPosition(el,bounds,image), bgy; if (image){ switch(background_repeat){ case "repeat-x": this.drawbackgroundRepeatX(image,bgp,bounds.left,bounds.top,bounds.width,bounds.height); break; case "repeat-y": this.drawbackgroundRepeatY(image,bgp,bounds.left,bounds.top,bounds.width,bounds.height); break; case "no-repeat": this.drawBackgroundRepeat(image,bgp.left+bounds.left,bgp.top+bounds.top,Math.min(bounds.width,image.width),Math.min(bounds.height,image.height),bounds.left,bounds.top); // ctx.drawImage(image,(bounds.left+bgp.left),(bounds.top+bgp.top)); break; default: var height, add; bgp.top = bgp.top-Math.ceil(bgp.top/image.height)*image.height; for(bgy=(bounds.top+bgp.top);bgy<=bounds.height+bounds.top;){ var h = Math.min(image.height,(bounds.height+bounds.top)-bgy); if ( Math.floor(bgy+image.height)>h+bgy){ height = (h+bgy)-bgy; }else{ height = image.height; } // console.log(height); if (bgy0){ bgp.top += add; } bgy = Math.floor(bgy+image.height)-add; } break; } }else{ this.log("Error loading background:" + background_image); //console.log(images); } } } /* * Function to retrieve the actual src of a background-image */ html2canvas.prototype.backgroundImageUrl = function(src){ if (src.substr(0,5)=='url("'){ src = src.substr(5); src = src.substr(0,src.length-2); }else{ src = src.substr(4); src = src.substr(0,src.length-1); } return src; } /* * Function to retrieve background-position, both in pixels and % */ html2canvas.prototype.getBackgroundPosition = function(el,bounds,image){ var bgposition = this.getCSS(el,"background-position").split(" "), top, left, percentage; if (bgposition[0].indexOf("%")!=-1){ percentage = (parseFloat(bgposition[0])/100); left = ((bounds.width * percentage)-(image.width*percentage)); }else{ left = parseInt(bgposition[0],10); } if (bgposition[1].indexOf("%")!=-1){ percentage = (parseFloat(bgposition[1])/100); top = ((bounds.height * percentage)-(image.height*percentage)); }else{ top = parseInt(bgposition[1],10); } return { top: top, left: left }; } html2canvas.prototype.drawbackgroundRepeatY = function(image,bgp,x,y,w,h){ var height, width = Math.min(image.width,w),bgy; bgp.top = bgp.top-Math.ceil(bgp.top/image.height)*image.height; for(bgy=(y+bgp.top);bgy<=h+y;){ if ( Math.floor(bgy+image.height)>h+y){ height = (h+y)-bgy; }else{ height = image.height; } this.drawBackgroundRepeat(image,x+bgp.left,bgy,width,height,x,y); bgy = Math.floor(bgy+image.height); } } html2canvas.prototype.drawbackgroundRepeatX = function(image,bgp,x,y,w,h){ var height = Math.min(image.height,h), width,bgx; bgp.left = bgp.left-Math.ceil(bgp.left/image.width)*image.width; for(bgx=(x+bgp.left);bgx<=w+x;){ if (Math.floor(bgx+image.width)>w+x){ width = (w+x)-bgx; }else{ width = image.width; } this.drawBackgroundRepeat(image,bgx,(y+bgp.top),width,height,x,y); bgx = Math.floor(bgx+image.width); } } html2canvas.prototype.drawBackgroundRepeat = function(image,x,y,width,height,elx,ely){ var sourceX = 0, sourceY=0; if (elx-x>0){ sourceX = elx-x; } if (ely-y>0){ sourceY = ely-y; } this.ctx.drawImage( image, sourceX, // source X sourceY, // source Y width-sourceX, // source Width height-sourceY, // source Height x+sourceX, // destination X y+sourceY, // destination Y width-sourceX, // destination width height-sourceY // destination height ); }