diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c777af9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/nbproject/private/ +/images/ +/tests/templates/ +/dist/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..250a767 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +/* + * The MIT License + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..609988a --- /dev/null +++ b/build.xml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/build/html2canvas.js b/build/html2canvas.js new file mode 100644 index 0000000..ed5f18f --- /dev/null +++ b/build/html2canvas.js @@ -0,0 +1,871 @@ +/* + * html2canvas v0.12 + * Copyright (c) 2011 Niklas von Hertzen. All rights reserved. + * http://www.twitter.com/niklasvh + * + * Released under MIT License + */ + +/* + * The MIT License + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * Creates a render of the element el + * @constructor + */ + +function html2canvas(el, userOptions) { + + var options = userOptions || {}; + + + this.opts = this.extendObj(options, { + logging: false, + ready: function (canvas) { + document.body.appendChild(canvas); + }, + renderViewport: true + }); + + this.element = el; + + var imageLoaded, + canvas, + ctx, + bgx, + bgy, + image; + this.imagesLoaded = 0; + this.images = []; + this.ignoreElements = "IFRAME|OBJECT|PARAM"; + + + + // test how to measure text bounding boxes + this.useRangeBounds = false; + if (document.createRange){ + var r = document.createRange(); + this.useRangeBounds = new Boolean(r.getBoundingClientRect); + } + + // Start script + this.init(); +} + + + + +html2canvas.prototype.init = function(){ + + var _ = this; + + this.canvas = document.createElement('canvas'); + + // TODO remove jQuery dependency + this.canvas.width = $(document).width(); + this.canvas.height = $(document).height()+10; + + + + if (!this.canvas.getContext){ + + // TODO include Flashcanvas + /* + + var script = document.createElement('script'); + script.type = "text/javascript"; + script.src = "flashcanvas.js"; + var s = document.getElementsByTagName('script')[0]; + s.parentNode.insertBefore(script, s); + + + if (typeof FlashCanvas != "undefined") { + + FlashCanvas.initElement(canvas); + ctx = canvas.getContext('2d'); + } + */ + }else{ + this.ctx = this.canvas.getContext('2d'); + } + + if (!this.ctx){ + // canvas not initialized, let's kill it here + this.log('Canvas not available'); + return; + } + + // set common settings for canvas + this.ctx.textBaseline = "bottom"; + + this.log('Finding background images'); + + this.getImages(this.element); + + this.log('Finding images'); + this.each(document.images,function(i,e){ + _.preloadImage(_.getAttr(e,'src')); + }); + + + if (this.images.length == 0){ + this.start(); + } + + +} + +/* + * Check whether all assets have been loaded and start traversing the DOM + */ + +html2canvas.prototype.start = function(){ + + if (this.images.length == 0 || this.imagesLoaded==this.images.length/2){ + this.log('Started parsing'); + this.newElement(this.element); + + this.parseElement(this.element); + } +} + + + +/* + * Finished rendering, send callback + */ + +html2canvas.prototype.finish = function(){ + this.log("Finished rendering"); + + if (this.opts.renderViewport){ + // let's crop it to viewport only then + var newCanvas = document.createElement('canvas'); + var newctx = newCanvas.getContext('2d'); + newCanvas.width = window.innerWidth; + newCanvas.height = window.innerHeight; + + } + this.opts.ready(this.canvas); +} + + + + +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 + ); +} +/* + * Function to provide border details for an element + */ + +html2canvas.prototype.getBorderData = function(el){ + + var borders = []; + var _ = this; + this.each(["top","right","bottom","left"],function(i,borderSide){ + borders.push({ + width: parseInt(_.getCSS(el,'border-'+borderSide+'-width'),10), + color: _.getCSS(el,'border-'+borderSide+'-color') + }); + }); + + return borders; + +} + + + + +html2canvas.prototype.newElement = function(el){ + + var bounds = this.getBounds(el); + + var x = bounds.left; + var y = bounds.top; + var w = bounds.width; + var h = bounds.height; + var _ = this, + image; + var bgcolor = this.getCSS(el,"background-color"); + + + + + /* + * TODO add support for different border-style's than solid + */ + var borders = this.getBorderData(el); + + this.each(borders,function(borderSide,borderData){ + if (borderData.width>0){ + var bx = x, + by = y, + bw = w, + bh = h-(borders[2].width); + switch(borderSide){ + case 0: + // top border + bh = borders[0].width; + break; + case 1: + // right border + bx = x+w-(borders[1].width); + bw = borders[1].width; + break; + case 2: + // bottom border + by = (by+h)-(borders[2].width); + bh = borders[2].width; + break; + case 3: + // left border + bw = borders[3].width; + break; + } + + _.newRect(bx,by,bw,bh,borderData.color); + + + + } + + }); + + + + // draw base element bgcolor + this.newRect( + x+borders[3].width, + y+borders[0].width, + w-(borders[1].width+borders[3].width), + h-(borders[0].width+borders[2].width), + bgcolor + ); + + this.drawBackground(el,{ + left: x+borders[3].width, + top: y+borders[0].width, + width: w-(borders[1].width+borders[3].width), + height: h-(borders[0].width+borders[2].width) + }); + + if (el.nodeName=="IMG"){ + image = _.loadImage(_.getAttr(el,'src')); + if (image){ + this.ctx.drawImage(image,x+parseInt(_.getCSS(el,'padding-left'),10),y+parseInt(_.getCSS(el,'padding-top'),10)); + }else{ + this.log("Error loading :" + _.getAttr(el,'src')); + } + } + + + +} + + + + + + +/* + * Function to draw the text on the canvas + */ + +html2canvas.prototype.printText = function(currentText,x,y){ + if (this.trim(currentText).length>0){ + this.ctx.fillText(currentText,x,y); + } +} + + +// Drawing a rectangle +html2canvas.prototype.newRect = function(x,y,w,h,bgcolor){ + if (bgcolor!="transparent"){ + this.ctx.fillStyle = bgcolor; + this.ctx.fillRect (x, y, w, h); + } +} +/* + * Function to find all images from and background-image + */ +html2canvas.prototype.getImages = function(el) { + + var self = this; + + // TODO remove jQuery dependancy + this.each($(el).contents(),function(i,element){ + var ignRe = new RegExp("("+this.ignoreElements+")"); + if (!ignRe.test(element.nodeName)){ + self.getImages(element); + } + }) + + if (el.nodeType==1 || typeof el.nodeType == "undefined"){ + var background_image = this.getCSS(el,'background-image'); + + if (background_image && background_image != "1" && background_image != "none" && background_image.substring(0,7)!="-webkit" && background_image.substring(0,4)!="-moz"){ + var src = this.backgroundImageUrl(background_image); + this.preloadImage(src); + } + } +} + + +/* + * Load image from storage + */ + +html2canvas.prototype.loadImage = function(src){ + + var imgIndex = this.images.indexOf(src); + if (imgIndex!=-1){ + return this.images[imgIndex+1]; + }else{ + return false; + } + +} + + + +html2canvas.prototype.preloadImage = function(src){ + + if (this.images.indexOf(src)==-1){ + this.images.push(src); + + var img = new Image(); + // TODO remove jQuery dependancy + var _ = this; + $(img).load(function(){ + _.imagesLoaded++; + _.start(); + + }); + img.onerror = function(){ + _.images.splice(_.images.indexOf(img.src),2); + _.imagesLoaded++; + _.start(); + } + img.src = src; + this.images.push(img); + + } + +} + + +html2canvas.prototype.newText = function(el,textNode){ + + + + + var family = this.getCSS(el,"font-family"); + var size = this.getCSS(el,"font-size"); + var color = this.getCSS(el,"color"); + + var bold = this.getCSS(el,"font-weight"); + var font_style = this.getCSS(el,"font-style"); + + + var text_decoration = this.getCSS(el,"text-decoration"); + + + // apply text-transform:ation to the text + textNode.nodeValue = this.textTransform(textNode.nodeValue,this.getCSS(el,"text-transform")); + var text = textNode.nodeValue; + + //text = $.trim(text); + if (text.length>0){ + switch(bold){ + case "401": + bold = "bold"; + break; + } + + + + this.ctx.font = bold+" "+font_style+" "+size+" "+family; + this.ctx.fillStyle = color; + + + + + + var oldTextNode = textNode; + for(var c=0;c + * Copyright (c) 2011 Niklas von Hertzen. All rights reserved. + * http://www.twitter.com/niklasvh + * + * 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.ignoreElements="IFRAME|OBJECT|PARAM";this.useRangeBounds=!1;if(document.createRange){var c=document.createRange();this.useRangeBounds=new Boolean(c.getBoundingClientRect)}this.init()} +html2canvas.prototype.init=function(){var a=this;this.canvas=document.createElement("canvas");this.canvas.width=$(document).width();this.canvas.height=$(document).height()+10;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.newElement(this.element),this.parseElement(this.element)};html2canvas.prototype.finish=function(){this.log("Finished rendering");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"),f=this.getCSS(a,"background-repeat");if(typeof c!="undefined"&&/^(1|none)$/.test(c)==!1){var c=this.backgroundImageUrl(c),d=this.loadImage(c),h=this.getBackgroundPosition(a,b,d);if(d)switch(f){case "repeat-x":this.drawbackgroundRepeatX(d,h,b.left,b.top,b.width,b.height);break;case "repeat-y":this.drawbackgroundRepeatY(d,h,b.left,b.top,b.width,b.height);break;case "no-repeat":this.drawBackgroundRepeat(d,h.left+ +b.left,h.top+b.top,Math.min(b.width,d.width),Math.min(b.height,d.height),b.left,b.top);break;default:var g;h.top-=Math.ceil(h.top/d.height)*d.height;for(c=b.top+h.top;c<=b.height+b.top;)f=Math.min(d.height,b.height+b.top-c),f=Math.floor(c+d.height)>f+c?f+c-c:d.height,c0&&(h.top+=g),c=Math.floor(c+d.height)-g}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 f=this.getCSS(a,"background-position").split(" "),d;f[0].indexOf("%")!=-1?(d=parseFloat(f[0])/100,a=b.width*d-c.width*d):a=parseInt(f[0],10);f[1].indexOf("%")!=-1?(d=parseFloat(f[1])/100,b=b.height*d-c.height*d):b=parseInt(f[1],10);return{top:b,left:a}}; +html2canvas.prototype.drawbackgroundRepeatY=function(a,b,c,f,d,h){var g=Math.min(a.width,d),e;b.top-=Math.ceil(b.top/a.height)*a.height;for(e=f+b.top;e<=h+f;)d=Math.floor(e+a.height)>h+f?h+f-e:a.height,this.drawBackgroundRepeat(a,c+b.left,e,g,d,c,f),e=Math.floor(e+a.height)}; +html2canvas.prototype.drawbackgroundRepeatX=function(a,b,c,f,d,h){var h=Math.min(a.height,h),g,e;b.left-=Math.ceil(b.left/a.width)*a.width;for(e=c+b.left;e<=d+c;)g=Math.floor(e+a.width)>d+c?d+c-e:a.width,this.drawBackgroundRepeat(a,e,f+b.top,g,h,c,f),e=Math.floor(e+a.width)};html2canvas.prototype.drawBackgroundRepeat=function(a,b,c,f,d,h,g){var e=0,i=0;h-b>0&&(e=h-b);g-c>0&&(i=g-c);this.ctx.drawImage(a,e,i,f-e,d-i,b+e,c+i,f-e,d-i)}; +html2canvas.prototype.getBorderData=function(a){var b=[],c=this;this.each(["top","right","bottom","left"],function(f,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,f=b.top,d=b.width,h=b.height,g=this,b=this.getCSS(a,"background-color"),e=this.getBorderData(a);this.each(e,function(a,b){if(b.width>0){var j=c,k=f,l=d,m=h-e[2].width;switch(a){case 0:m=e[0].width;break;case 1:j=c+d-e[1].width;l=e[1].width;break;case 2:k=k+h-e[2].width;m=e[2].width;break;case 3:l=e[3].width}g.newRect(j,k,l,m,b.color)}});this.newRect(c+e[3].width,f+e[0].width,d-(e[1].width+e[3].width),h-(e[0].width+e[2].width), +b);this.drawBackground(a,{left:c+e[3].width,top:f+e[0].width,width:d-(e[1].width+e[3].width),height:h-(e[0].width+e[2].width)});a.nodeName=="IMG"&&((b=g.loadImage(g.getAttr(a,"src")))?this.ctx.drawImage(b,c+parseInt(g.getCSS(a,"padding-left"),10),f+parseInt(g.getCSS(a,"padding-top"),10)):this.log("Error loading :"+g.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,f,d){if(d!="transparent")this.ctx.fillStyle=d,this.ctx.fillRect(a,b,c,f)};html2canvas.prototype.getImages=function(a){var b=this;this.each($(a).contents(),function(a,f){RegExp("("+this.ignoreElements+")").test(f.nodeName)||b.getImages(f)});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"),f=this.getCSS(a,"font-size"),d=this.getCSS(a,"color"),h=this.getCSS(a,"font-weight"),g=this.getCSS(a,"font-style"),e=this.getCSS(a,"text-decoration");b.nodeValue=this.textTransform(b.nodeValue,this.getCSS(a,"text-transform"));var i=b.nodeValue;if(i.length>0){switch(h){case "401":h="bold"}this.ctx.font=h+" "+g+" "+f+" "+c;this.ctx.fillStyle=d;c=b;for(f=0;f + * Copyright (c) 2011 Niklas von Hertzen. All rights reserved. + * http://www.twitter.com/niklasvh + * + * Released under MIT License + */ + +/* + * The MIT License + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + +/* + * jQuery helper plugin for examples and tests + */ + +(function( $ ){ + $.fn.html2canvas = function() { + + var date = new Date(); + var message, + timeoutTimer, + timer = date.getTime(); + + new html2canvas(this.get(0),{ + ready:function(canvas){ + + var finishTime = new Date(); + // console.log((finishTime.getTime()-timer)/1000); + + + document.body.appendChild(canvas); + var canvas = $(canvas); + canvas.css('position','absolute') + .css('left',0).css('top',0); + // $('body').append(canvas); + $(canvas).siblings().toggle(); + $(window).click(function(){ + if (!canvas.is(':visible')){ + $(canvas).toggle().siblings().toggle(); + throwMessage("Canvas Render visible"); + } else{ + $(canvas).siblings().toggle(); + $(canvas).toggle(); + throwMessage("Canvas Render hidden"); + } + + + }); + } + + }); + + + function throwMessage(msg){ + window.clearTimeout(timeoutTimer); + timeoutTimer = window.setTimeout(function(){ + message.fadeOut(function(){ + message.remove(); + }); + },2000); + $(message).remove(); + message = $('
').text(msg).css({ + margin:0, + padding:10, + background: "#000", + opacity:0.7, + position:"fixed", + top:10, + right:10, + fontFamily: 'Tahoma' , + color:'#fff', + fontSize:12, + borderRadius:12, + width:'auto', + height:'auto' + }).hide().fadeIn().appendTo('body'); + } + + }; +})( jQuery ); + diff --git a/build/tmp.js b/build/tmp.js new file mode 100644 index 0000000..37b69f3 --- /dev/null +++ b/build/tmp.js @@ -0,0 +1,19 @@ +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.ignoreElements="IFRAME|OBJECT|PARAM";this.useRangeBounds=!1;if(document.createRange){var c=document.createRange();this.useRangeBounds=new Boolean(c.getBoundingClientRect)}this.init()} +html2canvas.prototype.init=function(){var a=this;this.canvas=document.createElement("canvas");this.canvas.width=$(document).width();this.canvas.height=$(document).height()+10;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.newElement(this.element),this.parseElement(this.element)};html2canvas.prototype.finish=function(){this.log("Finished rendering");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"),f=this.getCSS(a,"background-repeat");if(typeof c!="undefined"&&/^(1|none)$/.test(c)==!1){var c=this.backgroundImageUrl(c),d=this.loadImage(c),h=this.getBackgroundPosition(a,b,d);if(d)switch(f){case "repeat-x":this.drawbackgroundRepeatX(d,h,b.left,b.top,b.width,b.height);break;case "repeat-y":this.drawbackgroundRepeatY(d,h,b.left,b.top,b.width,b.height);break;case "no-repeat":this.drawBackgroundRepeat(d,h.left+ +b.left,h.top+b.top,Math.min(b.width,d.width),Math.min(b.height,d.height),b.left,b.top);break;default:var g;h.top-=Math.ceil(h.top/d.height)*d.height;for(c=b.top+h.top;c<=b.height+b.top;)f=Math.min(d.height,b.height+b.top-c),f=Math.floor(c+d.height)>f+c?f+c-c:d.height,c0&&(h.top+=g),c=Math.floor(c+d.height)-g}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 f=this.getCSS(a,"background-position").split(" "),d;f[0].indexOf("%")!=-1?(d=parseFloat(f[0])/100,a=b.width*d-c.width*d):a=parseInt(f[0],10);f[1].indexOf("%")!=-1?(d=parseFloat(f[1])/100,b=b.height*d-c.height*d):b=parseInt(f[1],10);return{top:b,left:a}}; +html2canvas.prototype.drawbackgroundRepeatY=function(a,b,c,f,d,h){var g=Math.min(a.width,d),e;b.top-=Math.ceil(b.top/a.height)*a.height;for(e=f+b.top;e<=h+f;)d=Math.floor(e+a.height)>h+f?h+f-e:a.height,this.drawBackgroundRepeat(a,c+b.left,e,g,d,c,f),e=Math.floor(e+a.height)}; +html2canvas.prototype.drawbackgroundRepeatX=function(a,b,c,f,d,h){var h=Math.min(a.height,h),g,e;b.left-=Math.ceil(b.left/a.width)*a.width;for(e=c+b.left;e<=d+c;)g=Math.floor(e+a.width)>d+c?d+c-e:a.width,this.drawBackgroundRepeat(a,e,f+b.top,g,h,c,f),e=Math.floor(e+a.width)};html2canvas.prototype.drawBackgroundRepeat=function(a,b,c,f,d,h,g){var e=0,i=0;h-b>0&&(e=h-b);g-c>0&&(i=g-c);this.ctx.drawImage(a,e,i,f-e,d-i,b+e,c+i,f-e,d-i)}; +html2canvas.prototype.getBorderData=function(a){var b=[],c=this;this.each(["top","right","bottom","left"],function(f,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,f=b.top,d=b.width,h=b.height,g=this,b=this.getCSS(a,"background-color"),e=this.getBorderData(a);this.each(e,function(a,b){if(b.width>0){var j=c,k=f,l=d,m=h-e[2].width;switch(a){case 0:m=e[0].width;break;case 1:j=c+d-e[1].width;l=e[1].width;break;case 2:k=k+h-e[2].width;m=e[2].width;break;case 3:l=e[3].width}g.newRect(j,k,l,m,b.color)}});this.newRect(c+e[3].width,f+e[0].width,d-(e[1].width+e[3].width),h-(e[0].width+e[2].width), +b);this.drawBackground(a,{left:c+e[3].width,top:f+e[0].width,width:d-(e[1].width+e[3].width),height:h-(e[0].width+e[2].width)});a.nodeName=="IMG"&&((b=g.loadImage(g.getAttr(a,"src")))?this.ctx.drawImage(b,c+parseInt(g.getCSS(a,"padding-left"),10),f+parseInt(g.getCSS(a,"padding-top"),10)):this.log("Error loading :"+g.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,f,d){if(d!="transparent")this.ctx.fillStyle=d,this.ctx.fillRect(a,b,c,f)};html2canvas.prototype.getImages=function(a){var b=this;this.each($(a).contents(),function(a,f){RegExp("("+this.ignoreElements+")").test(f.nodeName)||b.getImages(f)});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"),f=this.getCSS(a,"font-size"),d=this.getCSS(a,"color"),h=this.getCSS(a,"font-weight"),g=this.getCSS(a,"font-style"),e=this.getCSS(a,"text-decoration");b.nodeValue=this.textTransform(b.nodeValue,this.getCSS(a,"text-transform"));var i=b.nodeValue;if(i.length>0){switch(h){case "401":h="bold"}this.ctx.font=h+" "+g+" "+f+" "+c;this.ctx.fillStyle=d;c=b;for(f=0;f - - - + + + - - - + + + - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+
+

html2canvas - Screenshots with JavaScript

+ +

+ This script allows you to take "screenshots" of webpages or parts of it, directly on the users browser. The screenshot is based on the DOM and as such may not be 100% accurate to the real representation as it does not make an actual screenshot, but builds the screenshot based on the information available on the page. + +

+

How does it work?

+

The script renders the current page as a canvas image, by reading the DOM and the different styles applied to the elements. However, as many elements are displayed differently on different browsers and operating systems (such as form elements such as radio buttons or checkboxes) as well as +

+

It does not require any rendering from the server, as the whole image is created on the clients browser. However, for browsers without canvas support alternatives such as flashcanvas or ExplorerCanvas are necessary to create the image. + Additionally, to render iframe content or images situated outside of the same origin policy a proxy will be necessary to load the content to the users browser. + + + The script is still in a very experimental state, so I don't recommend using it in a production environment nor start building applications with it yet, as there will be still major changes made. However, please do test it out and report your findings, especially if something should be working, but is displaying it incorrectly. +

+ +

The minified version of the script is currently less than 6kb (less than 2.5kb gzipped). It currently still has jQuery as a dependancy, but that will get removed soon.

+ + +

Browser compatibility

+

The script should work fine on the following browsers:

+
    +
  • Firefox 3.5+
  • +
  • Google Chrome
  • +
  • >=IE9 (Older versions compatible with the use of flashcanvas)
  • +
+

Note that the compatibility will most likely be increased in future builds, as many of the current restrictions have at least partial work arounds, which can be used with older browser versions.

+ +

So what isn't included yet?

+

+ There are still a lot of CSS properties missing, including most CSS3 properties such as text-shadow, box-radius etc. as well as all elements created by the browser, such as radio and checkbox buttons and list icons. I will compile a full list of supported elements and CSS properties soon. There is no support for opacity or z-index layering yet either and object content such as Flash will most likely never be supported. + +

+ +
+ + + + Download on GitHub + +

+ +

Examples

+

The screenshot gets overlayed on top of the page. You can toggle the visibility of the canvas by clicking anywhere on the page.

+
    + +
  • Basic
  • + +
  • Dusky template
  • +
  • Combination template
  • +
  • Projection template
  • +
  • Facing template
  • + +
  • Palmtrees template
  • +
  • Artificial template
  • + +
+ +
+

Tests

+ + +

Any comments or feedback is greatly appreciated.

+ + + +
+
+
+
+
+ + + diff --git a/lib/compiler.jar b/lib/compiler.jar new file mode 100644 index 0000000..694808c Binary files /dev/null and b/lib/compiler.jar differ diff --git a/lib/jquery-1.4.4.externs.js b/lib/jquery-1.4.4.externs.js new file mode 100644 index 0000000..a81a378 --- /dev/null +++ b/lib/jquery-1.4.4.externs.js @@ -0,0 +1,1204 @@ +/* +* Copyright 2010 The Closure Compiler Authors +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/** +* @fileoverview Externs for jQuery 1.4.4. +* Note that some functions use different return types depending on the number +* of parameters passed in. In these cases, you may need to annotate the type +* of the result in your code, so the JSCompiler understands which type you're +* expecting. For example: +* var elt = /** @type {Element} * / (foo.get(0)); +* @see http://api.jquery.com/ +* @externs +*/ + +/** @typedef {(Window|Document|Element|Array.|string|jQueryObject)} */ +var jQuerySelector; + +/** + * @param {(jQuerySelector|Element|Array|Object|string|function())=} arg1 + * @param {(Element|jQueryObject|Document|Object)=} arg2 + * @return {jQueryObject} + */ +function $(arg1, arg2) {}; + +/** + * @param {(jQuerySelector|Element|Array|Object|string|function())=} arg1 + * @param {(Element|jQueryObject|Document|Object)=} arg2 + * @return {jQueryObject} + */ +function jQuery(arg1, arg2) {}; + +/** + * @param {Object.} settings + * @return {XMLHttpRequest} + */ +jQuery.ajax = function(settings) {}; + +/** @param {Object.} options */ +jQuery.ajaxSetup = function(options) {}; + +/** @type {boolean} */ +jQuery.boxModel; + +/** @type {Object.} */ +jQuery.browser; + +/** @type {string} */ +jQuery.browser.version; + +/** + * @param {Element} container + * @param {Element} contained + * @return {boolean} + */ +jQuery.contains = function(container, contained) {}; + +/** + * @param {Element} elem + * @param {string=} key + * @param {Object=} value + * @return {(jQueryObject|Object)} + */ +jQuery.data = function(elem, key, value) {}; + +/** + * @param {Element} elem + * @param {string=} queueName + * @return {jQueryObject} + */ +jQuery.dequeue = function(elem, queueName) {}; + +/** + * @param {Object} collection + * @param {function(number,*)} callback + * @return {Object} + */ +jQuery.each = function(collection, callback) {}; + +/** @param {string} message */ +jQuery.error = function(message) {}; + +/** + * @constructor + * @param {string} eventType + */ +jQuery.event = function(eventType) {}; + +/** @type {Element} */ +jQuery.event.prototype.currentTarget; + +/** @type {*} */ +jQuery.event.prototype.data; + +/** @return {boolean} */ +jQuery.event.prototype.isDefaultPrevented = function() {}; + +/** @return {boolean} */ +jQuery.event.prototype.isImmediatePropagationStopped = function() {}; + +/** @return {boolean} */ +jQuery.event.prototype.isPropagationStopped = function() {}; + +/** @type {string} */ +jQuery.event.prototype.namespace; + +/** @type {number} */ +jQuery.event.prototype.pageX; + +/** @type {number} */ +jQuery.event.prototype.pageY; + +/** @return {undefined} */ +jQuery.event.prototype.preventDefault = function() {}; + +/** @type {Element} */ +jQuery.event.prototype.relatedTarget; + +/** @type {Object} */ +jQuery.event.prototype.result; + +/** @return {undefined} */ +jQuery.event.prototype.stopImmediatePropagation = function() {}; + +/** @return {undefined} */ +jQuery.event.prototype.stopPropagation = function() {}; + +/** @type {Element} */ +jQuery.event.prototype.target; + +/** @type {number} */ +jQuery.event.prototype.timeStamp; + +/** @type {string} */ +jQuery.event.prototype.type; + +/** @type {number} */ +jQuery.event.prototype.which; + +/** + * @param {(Object|boolean)=} arg1 + * @param {Object=} arg2 + * @param {Object=} arg3 + * @param {Object=} objectN + * @return {Object} + */ +jQuery.extend = function(arg1, arg2, arg3, objectN) {}; + +jQuery.fx = {}; + +/** @type {number} */ +jQuery.fx.interval; + +/** @type {boolean} */ +jQuery.fx.off; + +/** + * @param {string} url + * @param {(Object.|string)=} data + * @param {function(string,string,XMLHttpRequest)=} callback + * @param {string=} dataType + * @return {XMLHttpRequest} + */ +jQuery.get = function(url, data, callback, dataType) {}; + +/** + * @param {string} url + * @param {Object.=} data + * @param {function(string,string,XMLHttpRequest)=} callback + * @return {XMLHttpRequest} + */ +jQuery.getJSON = function(url, data, callback) {}; + +/** + * @param {string} url + * @param {function(string,string)=} success + * @return {XMLHttpRequest} + */ +jQuery.getScript = function(url, success) {}; + +/** @param {string} code */ +jQuery.globalEval = function(code) {}; + +/** + * @param {Array} arr + * @param {function(*,number)} fnc + * @param {boolean=} invert + * @return {Array} + */ +jQuery.grep = function(arr, fnc, invert) {}; + +/** + * @param {*} value + * @param {Array} arr + * @return {number} + */ +jQuery.inArray = function(value, arr) {}; + +/** + * @param {Object} obj + * @return {boolean} + * @nosideeffects + */ +jQuery.isArray = function(obj) {}; + +/** + * @param {Object} obj + * @return {boolean} + * @nosideeffects + */ +jQuery.isEmptyObject = function(obj) {}; + +/** + * @param {Object} obj + * @return {boolean} + * @nosideeffects + */ +jQuery.isFunction = function(obj) {}; + +/** + * @param {Object} obj + * @return {boolean} + * @nosideeffects + */ +jQuery.isPlainObject = function(obj) {}; + +/** + * @param {Object} obj + * @return {boolean} + * @nosideeffects + */ +jQuery.isWindow = function(obj) {}; + +/** + * @param {Element} node + * @return {boolean} + * @nosideeffects + */ +jQuery.isXMLDoc = function(node) {}; + +/** + * @param {Object} obj + * @return {Array} + */ +jQuery.makeArray = function(obj) {}; + +/** + * @param {Array} arr + * @param {function(*,number)} callback + * @return {Array} + */ +jQuery.map = function(arr, callback) {}; + +/** + * @param {Array} first + * @param {Array} second + * @return {Array} + */ +jQuery.merge = function(first, second) {}; + +/** + * @param {boolean=} removeAll + * @return {Object} + */ +jQuery.noConflict = function(removeAll) {}; + +/** + * @return {function()} + * @nosideeffects + */ +jQuery.noop = function() {}; + +/** + * @param {(Array|Object)} obj + * @param {boolean=} traditional + * @return {string} + */ +jQuery.param = function(obj, traditional) {}; + +/** + * @param {string} json + * @return {Object} + */ +jQuery.parseJSON = function(json) {}; + +/** + * @param {string} url + * @param {(Object.|string)=} data + * @param {function(string,string,XMLHttpRequest)=} success + * @param {string=} dataType + * @return {XMLHttpRequest} + */ +jQuery.post = function(url, data, success, dataType) {}; + +/** + * @param {(function()|Object)} arg1 + * @param {(Object|string)} arg2 + * @return {function()} + */ +jQuery.proxy = function(arg1, arg2) {}; + +/** + * @param {Element} elem + * @param {string=} queueName + * @param {(Array|function())=} arg3 + * @return {(Array|jQueryObject)} + */ +jQuery.queue = function(elem, queueName, arg3) {}; + +/** + * @param {Element} elem + * @param {string=} name + * @return {jQueryObject} + */ +jQuery.removeData = function(elem, name) {}; + +/** @type {Object} */ +jQuery.support; + +/** + * @param {string} str + * @return {string} + * @nosideeffects + */ +jQuery.trim = function(str) {}; + +/** + * @param {Object} obj + * @return {string} + * @nosideeffects + */ +jQuery.type = function(obj) {}; + +/** + * @param {Array} arr + * @return {Array} + */ +jQuery.unique = function(arr) {}; + +/** + * @constructor + * @private + */ +function jQueryObject() { }; + +/** + * @param {(jQuerySelector|Array.|string)} arg1 + * @param {Element=} context + * @return {jQueryObject} + */ +jQueryObject.prototype.add = function(arg1, context) {}; + +/** + * @param {(string|function(number,string))} arg1 + * @return {jQueryObject} + */ +jQueryObject.prototype.addClass = function(arg1) {}; + +/** + * @param {(string|Element|jQueryObject|function(number))} arg1 + * @return {jQueryObject} + */ +jQueryObject.prototype.after = function(arg1) {}; + +/** + * @param {function(jQuery.event,XMLHttpRequest,Object.)} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.ajaxComplete = function(handler) {}; + +/** + * @param {function(jQuery.event,XMLHttpRequest,Object.,*)} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.ajaxError = function(handler) {}; + +/** + * @param {function(jQuery.event,XMLHttpRequest,Object.)} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.ajaxSend = function(handler) {}; + +/** + * @param {function()} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.ajaxStart = function(handler) {}; + +/** + * @param {function()} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.ajaxStop = function(handler) {}; + +/** + * @param {function(jQuery.event,XMLHttpRequest,Object.)} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.ajaxSuccess = function(handler) {}; + +/** @return {jQueryObject} */ +jQueryObject.prototype.andSelf = function() {}; + +/** + * @param {Object.} properties + * @param {(string|number|Object.)=} arg2 + * @param {string=} easing + * @param {function()=} callback + * @return {jQueryObject} + */ +jQueryObject.prototype.animate + = function(properties, arg2, easing, callback) {}; + +/** + * @param {(string|Element|jQueryObject|function(number,string))} arg1 + * @return {jQueryObject} + */ +jQueryObject.prototype.append = function(arg1) {}; + +/** + * @param {(jQuerySelector|Element|jQueryObject)} target + * @return {jQueryObject} + */ +jQueryObject.prototype.appendTo = function(target) {}; + +/** + * @param {(string|Object.)} arg1 + * @param {(string|number|function(number,string))=} arg2 + * @return {(string|jQueryObject)} + */ +jQueryObject.prototype.attr = function(arg1, arg2) {}; + +/** + * @param {(string|Element|jQueryObject|function())} arg1 + * @return {jQueryObject} + */ +jQueryObject.prototype.before = function(arg1) {}; + +/** + * @param {(string|Object)} arg1 + * @param {Object=} eventData + * @param {(function(jQuery.event)|boolean)=} arg3 + * @return {jQueryObject} + */ +jQueryObject.prototype.bind = function(arg1, eventData, arg3) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.blur = function(arg1, handler) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.change = function(arg1, handler) {}; + +/** + * @param {jQuerySelector=} selector + * @return {jQueryObject} + */ +jQueryObject.prototype.children = function(selector) {}; + +/** + * @param {string=} queueName + * @return {jQueryObject} + */ +jQueryObject.prototype.clearQueue = function(queueName) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.click = function(arg1, handler) {}; + +/** + * @param {boolean=} withDataAndEvents + * @return {jQueryObject} + */ +jQueryObject.prototype.clone = function(withDataAndEvents) {}; + +/** + * @param {(jQuerySelector|Array)} arg1 + * @param {Element=} context + * @return {(jQueryObject|Array)} + */ +jQueryObject.prototype.closest = function(arg1, context) {}; + +/** @return {jQueryObject} */ +jQueryObject.prototype.contents = function() {}; + +/** @type {Element} */ +jQueryObject.prototype.context; + +/** + * @param {(string|Object.)} arg1 + * @param {(string|number|function(number,*))=} arg2 + * @return {(string|jQueryObject)} + */ +jQueryObject.prototype.css = function(arg1, arg2) {}; + +/** + * @param {(string|Object)=} arg1 + * @param {Object=} value + * @return {(jQueryObject|Object)} + */ +jQueryObject.prototype.data = function(arg1, value) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.dblclick = function(arg1, handler) {}; + +/** + * @param {number} duration + * @param {string=} queueName + * @return {jQueryObject} + */ +jQueryObject.prototype.delay = function(duration, queueName) {}; + +/** + * @param {string} selector + * @param {string} eventType + * @param {(function()|Object)} arg3 + * @param {function()=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.delegate + = function(selector, eventType, arg3, handler) {}; + +/** + * @param {string=} queueName + * @return {jQueryObject} + */ +jQueryObject.prototype.dequeue = function(queueName) {}; + +/** + * @param {jQuerySelector=} selector + * @return {jQueryObject} + */ +jQueryObject.prototype.detach = function(selector) {}; + +/** + * @param {string=} eventType + * @param {string=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.die = function(eventType, handler) {}; + +/** + * @param {function(number,Element)} fnc + * @return {jQueryObject} + */ +jQueryObject.prototype.each = function(fnc) {}; + +/** @return {jQueryObject} */ +jQueryObject.prototype.empty = function() {}; + +/** @return {jQueryObject} */ +jQueryObject.prototype.end = function() {}; + +/** + * @param {number} arg1 + * @return {jQueryObject} + */ +jQueryObject.prototype.eq = function(arg1) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.error = function(arg1, handler) {}; + +/** + * @param {(string|number)=} duration + * @param {(function()|string)=} arg2 + * @param {function()=} callback + * @return {jQueryObject} + */ +jQueryObject.prototype.fadeIn = function(duration, arg2, callback) {}; + +/** + * @param {(string|number)=} duration + * @param {(function()|string)=} arg2 + * @param {function()=} callback + * @return {jQueryObject} + */ +jQueryObject.prototype.fadeOut = function(duration, arg2, callback) {}; + +/** + * @param {(string|number)=} duration + * @param {number=} opacity + * @param {(function()|string)=} arg3 + * @param {function()=} callback + * @return {jQueryObject} + */ +jQueryObject.prototype.fadeTo = function(duration, opacity, arg3, callback) {}; + +/** + * @param {(string|number)=} duration + * @param {string=} easing + * @param {function()=} callback + * @return {jQueryObject} + */ +jQueryObject.prototype.fadeToggle = function(duration, easing, callback) {}; + +/** + * @param {(jQuerySelector|function(number)|Element|Object)} arg1 + * @return {jQueryObject} + */ +jQueryObject.prototype.filter = function(arg1) {}; + +/** + * @param {jQuerySelector} selector + * @return {jQueryObject} + */ +jQueryObject.prototype.find = function(selector) {}; + +/** @return {jQueryObject} */ +jQueryObject.prototype.first = function() {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.focus = function(arg1, handler) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.focusin = function(arg1, handler) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.focusout = function(arg1, handler) {}; + +/** + * @param {number=} index + * @return {(Element|Array)} + * @nosideeffects + */ +jQueryObject.prototype.get = function(index) {}; + +/** + * @param {(string|Element)} arg1 + * @return {jQueryObject} + */ +jQueryObject.prototype.has = function(arg1) {}; + +/** + * @param {string} className + * @return {boolean} + */ +jQueryObject.prototype.hasClass = function(className) {}; + +/** + * @param {(string|number|function(number,number))=} arg1 + * @return {(number|jQueryObject)} + * @nosideeffects + */ +jQueryObject.prototype.height = function(arg1) {}; + +/** + * @param {(string|number)=} duration + * @param {(function()|string)=} arg2 + * @param {function()=} callback + * @return {jQueryObject} + */ +jQueryObject.prototype.hide = function(duration, arg2, callback) {}; + +/** + * @param {function(jQuery.event)} arg1 + * @param {function(jQuery.event)=} handlerOut + * @return {jQueryObject} + */ +jQueryObject.prototype.hover = function(arg1, handlerOut) {}; + +/** + * @param {(string|function(number,string))=} arg1 + * @return {(string|jQueryObject)} + */ +jQueryObject.prototype.html = function(arg1) {}; + +/** + * @param {(jQuerySelector|Element|jQueryObject)=} arg1 + * @return {number} + */ +jQueryObject.prototype.index = function(arg1) {}; + +/** + * @return {number} + * @nosideeffects + */ +jQueryObject.prototype.innerHeight = function() {}; + +/** + * @return {number} + * @nosideeffects + */ +jQueryObject.prototype.innerWidth = function() {}; + +/** + * @param {(jQuerySelector|Element|jQueryObject)} target + * @return {jQueryObject} + */ +jQueryObject.prototype.insertAfter = function(target) {}; + +/** + * @param {(jQuerySelector|Element|jQueryObject)} target + * @return {jQueryObject} + */ +jQueryObject.prototype.insertBefore = function(target) {}; + +/** + * @param {jQuerySelector} selector + * @return {boolean} + * @nosideeffects + */ +jQueryObject.prototype.is = function(selector) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.keydown = function(arg1, handler) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.keypress = function(arg1, handler) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.keyup = function(arg1, handler) {}; + +/** @return {jQueryObject} */ +jQueryObject.prototype.last = function() {}; + +/** @type {number} */ +jQueryObject.prototype.length; + +/** + * @param {string} eventType + * @param {(function()|Object)} arg2 + * @param {function()=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.live = function(eventType, arg2, handler) {}; + +/** + * @param {(function(jQuery.event)|Object|string)=} arg1 + * @param {(function(jQuery.event)|Object.|string)=} arg2 + * @param {function(string,string,XMLHttpRequest)=} complete + * @return {jQueryObject} + */ +jQueryObject.prototype.load = function(arg1, arg2, complete) {}; + +/** + * @param {function(number,Element)} callback + * @return {jQueryObject} + */ +jQueryObject.prototype.map = function(callback) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.mousedown = function(arg1, handler) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.mouseenter = function(arg1, handler) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.mouseleave = function(arg1, handler) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.mousemove = function(arg1, handler) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.mouseout = function(arg1, handler) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.mouseover = function(arg1, handler) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.mouseup = function(arg1, handler) {}; + +/** + * @param {jQuerySelector=} selector + * @return {jQueryObject} + */ +jQueryObject.prototype.next = function(selector) {}; + +/** + * @param {string=} selector + * @return {jQueryObject} + */ +jQueryObject.prototype.nextAll = function(selector) {}; + +/** + * @param {jQuerySelector=} selector + * @return {jQueryObject} + */ +jQueryObject.prototype.nextUntil = function(selector) {}; + +/** + * @param {(jQuerySelector|Array.|function(number))} arg1 + * @return {jQueryObject} + */ +jQueryObject.prototype.not = function(arg1) {}; + +/** + * @param {(Object|function(number,{top:number,left:number}))=} arg1 + * @return {(Object|jQueryObject)} + * @nosideeffects + */ +jQueryObject.prototype.offset = function(arg1) {}; + +/** @return {jQueryObject} */ +jQueryObject.prototype.offsetParent = function() {}; + +/** + * @param {string} eventType + * @param {Object=} eventData + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.one = function(eventType, eventData, handler) {}; + +/** + * @param {boolean=} includeMargin + * @return {number} + * @nosideeffects + */ +jQueryObject.prototype.outerHeight = function(includeMargin) {}; + +/** + * @param {boolean=} includeMargin + * @return {number} + * @nosideeffects + */ +jQueryObject.prototype.outerWidth = function(includeMargin) {}; + +/** + * @param {jQuerySelector=} selector + * @return {jQueryObject} + */ +jQueryObject.prototype.parent = function(selector) {}; + +/** + * @param {jQuerySelector=} selector + * @return {jQueryObject} + */ +jQueryObject.prototype.parents = function(selector) {}; + +/** + * @param {jQuerySelector=} selector + * @return {jQueryObject} + */ +jQueryObject.prototype.parentsUntil = function(selector) {}; + +/** + * @return {Object} + * @nosideeffects + */ +jQueryObject.prototype.position = function() {}; + +/** + * @param {(string|Element|jQueryObject|function(number,string))} arg1 + * @return {jQueryObject} + */ +jQueryObject.prototype.prepend = function(arg1) {}; + +/** + * @param {(jQuerySelector|Element|jQueryObject)} target + * @return {jQueryObject} + */ +jQueryObject.prototype.prependTo = function(target) {}; + +/** + * @param {jQuerySelector=} selector + * @return {jQueryObject} + */ +jQueryObject.prototype.prev = function(selector) {}; + +/** + * @param {jQuerySelector=} selector + * @return {jQueryObject} + */ +jQueryObject.prototype.prevAll = function(selector) {}; + +/** + * @param {jQuerySelector=} selector + * @return {jQueryObject} + */ +jQueryObject.prototype.prevUntil = function(selector) {}; + +/** + * @param {Array} elements + * @param {string=} name + * @param {Array=} args + * @return {jQueryObject} + */ +jQueryObject.prototype.pushStack = function(elements, name, args) {}; + +/** + * @param {string=} queueName + * @param {(Array|function(function()))=} arg2 + * @return {(Array|jQueryObject)} + */ +jQueryObject.prototype.queue = function(queueName, arg2) {}; + +/** + * @param {function()} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.ready = function(handler) {}; + +/** + * @param {string=} selector + * @return {jQueryObject} + */ +jQueryObject.prototype.remove = function(selector) {}; + +/** + * @param {string} attributeName + * @return {jQueryObject} + */ +jQueryObject.prototype.removeAttr = function(attributeName) {}; + +/** + * @param {(string|function(number,string))=} arg1 + * @return {jQueryObject} + */ +jQueryObject.prototype.removeClass = function(arg1) {}; + +/** + * @param {string=} name + * @return {jQueryObject} + */ +jQueryObject.prototype.removeData = function(name) {}; + +/** @return {jQueryObject} */ +jQueryObject.prototype.replaceAll = function() {}; + +/** + * @param {(string|Element|jQueryObject|function())} arg1 + * @return {jQueryObject} + */ +jQueryObject.prototype.replaceWith = function(arg1) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.resize = function(arg1, handler) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.scroll = function(arg1, handler) {}; + +/** + * @param {number=} value + * @return {(number|jQueryObject)} + */ +jQueryObject.prototype.scrollLeft = function(value) {}; + +/** + * @param {number=} value + * @return {(number|jQueryObject)} + */ +jQueryObject.prototype.scrollTop = function(value) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.select = function(arg1, handler) {}; + +/** @type {string} */ +jQueryObject.prototype.selector; + +/** @return {string} */ +jQueryObject.prototype.serialize = function() {}; + +/** @return {Array} */ +jQueryObject.prototype.serializeArray = function() {}; + +/** + * @param {(string|number)=} duration + * @param {(function()|string)=} arg2 + * @param {function()=} callback + * @return {jQueryObject} + */ +jQueryObject.prototype.show = function(duration, arg2, callback) {}; + +/** + * @param {jQuerySelector=} selector + * @return {jQueryObject} + */ +jQueryObject.prototype.siblings = function(selector) {}; + +/** @return {number} */ +jQueryObject.prototype.size = function() {}; + +/** + * @param {number} start + * @param {number=} end + * @return {jQueryObject} + */ +jQueryObject.prototype.slice = function(start, end) {}; + +/** + * @param {(string|number)=} duration + * @param {(function()|string)=} arg2 + * @param {function()=} callback + * @return {jQueryObject} + */ +jQueryObject.prototype.slideDown = function(duration, arg2, callback) {}; + +/** + * @param {(string|number)=} duration + * @param {(function()|string)=} arg2 + * @param {function()=} callback + * @return {jQueryObject} + */ +jQueryObject.prototype.slideToggle = function(duration, arg2, callback) {}; + +/** + * @param {(string|number)=} duration + * @param {(function()|string)=} arg2 + * @param {function()=} callback + * @return {jQueryObject} + */ +jQueryObject.prototype.slideUp = function(duration, arg2, callback) {}; + +/** + * @param {boolean=} clearQueue + * @param {boolean=} jumpToEnd + * @return {jQueryObject} + */ +jQueryObject.prototype.stop = function(clearQueue, jumpToEnd) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.submit = function(arg1, handler) {}; + +/** + * @param {(string|function(number,string))=} arg1 + * @return {(string|jQueryObject)} + */ +jQueryObject.prototype.text = function(arg1) {}; + +/** @return {Array} */ +jQueryObject.prototype.toArray = function() {}; + +/** + * @param {(function(jQuery.event)|string|number|boolean)=} arg1 + * @param {(function(jQuery.event)|string)=} arg2 + * @param {function(jQuery.event)=} arg3 + * @return {jQueryObject} + */ +jQueryObject.prototype.toggle = function(arg1, arg2, arg3) {}; + +/** + * @param {(string|function(number,string))} arg1 + * @param {boolean=} flag + * @return {jQueryObject} + */ +jQueryObject.prototype.toggleClass = function(arg1, flag) {}; + +/** + * @param {(string|jQuery.event)} arg1 + * @param {Array=} extraParameters + * @return {jQueryObject} + */ +jQueryObject.prototype.trigger = function(arg1, extraParameters) {}; + +/** + * @param {string} eventType + * @param {Array} extraParameters + * @return {Object} + */ +jQueryObject.prototype.triggerHandler = function(eventType, extraParameters) {}; + +/** + * @param {(string|Object)=} arg1 + * @param {(function(jQuery.event)|boolean)=} arg2 + * @return {jQueryObject} + */ +jQueryObject.prototype.unbind = function(arg1, arg2) {}; + +/** + * @param {string=} selector + * @param {string=} eventType + * @param {function()=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.undelegate = function(selector, eventType, handler) {}; + +/** + * @param {(function(jQuery.event)|Object)=} arg1 + * @param {function(jQuery.event)=} handler + * @return {jQueryObject} + */ +jQueryObject.prototype.unload = function(arg1, handler) {}; + +/** @return {jQueryObject} */ +jQueryObject.prototype.unwrap = function() {}; + +/** + * @param {(string|function(number,*))=} arg1 + * @return {(string|Array|jQueryObject)} + */ +jQueryObject.prototype.val = function(arg1) {}; + +/** + * @param {(string|number|function(number,number))=} arg1 + * @return {(number|jQueryObject)} + * @nosideeffects + */ +jQueryObject.prototype.width = function(arg1) {}; + +/** + * @param {(string|jQuerySelector|Element|jQueryObject|function())} arg1 + * @return {jQueryObject} + */ +jQueryObject.prototype.wrap = function(arg1) {}; + +/** + * @param {(string|jQuerySelector|Element|jQueryObject)} wrappingElement + * @return {jQueryObject} + */ +jQueryObject.prototype.wrapAll = function(wrappingElement) {}; + +/** + * @param {(string|function())} arg1 + * @return {jQueryObject} + */ +jQueryObject.prototype.wrapInner = function(arg1) {}; \ No newline at end of file diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..6ffde2f --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,7 @@ +include.path=${php.global.include.path} +php.version=PHP_5 +source.encoding=UTF-8 +src.dir=. +tags.asp=false +tags.short=true +web.root=. diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..a76fbad --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,9 @@ + + + org.netbeans.modules.php.project + + + html2canvas + + + diff --git a/src/Background.js b/src/Background.js new file mode 100644 index 0000000..2e961af --- /dev/null +++ b/src/Background.js @@ -0,0 +1,211 @@ + + +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 + ); +} \ No newline at end of file diff --git a/src/Border.js b/src/Border.js new file mode 100644 index 0000000..200e465 --- /dev/null +++ b/src/Border.js @@ -0,0 +1,18 @@ +/* + * Function to provide border details for an element + */ + +html2canvas.prototype.getBorderData = function(el){ + + var borders = []; + var _ = this; + this.each(["top","right","bottom","left"],function(i,borderSide){ + borders.push({ + width: parseInt(_.getCSS(el,'border-'+borderSide+'-width'),10), + color: _.getCSS(el,'border-'+borderSide+'-color') + }); + }); + + return borders; + +} \ No newline at end of file diff --git a/src/Core.js b/src/Core.js new file mode 100644 index 0000000..142662c --- /dev/null +++ b/src/Core.js @@ -0,0 +1,140 @@ +/** + * Creates a render of the element el + * @constructor + */ + +function html2canvas(el, userOptions) { + + var options = userOptions || {}; + + + this.opts = this.extendObj(options, { + logging: false, + ready: function (canvas) { + document.body.appendChild(canvas); + }, + renderViewport: true + }); + + this.element = el; + + var imageLoaded, + canvas, + ctx, + bgx, + bgy, + image; + this.imagesLoaded = 0; + this.images = []; + this.ignoreElements = "IFRAME|OBJECT|PARAM"; + + + + // test how to measure text bounding boxes + this.useRangeBounds = false; + if (document.createRange){ + var r = document.createRange(); + this.useRangeBounds = new Boolean(r.getBoundingClientRect); + } + + // Start script + this.init(); +} + + + + +html2canvas.prototype.init = function(){ + + var _ = this; + + this.canvas = document.createElement('canvas'); + + // TODO remove jQuery dependency + this.canvas.width = $(document).width(); + this.canvas.height = $(document).height()+10; + + + + if (!this.canvas.getContext){ + + // TODO include Flashcanvas + /* + + var script = document.createElement('script'); + script.type = "text/javascript"; + script.src = "flashcanvas.js"; + var s = document.getElementsByTagName('script')[0]; + s.parentNode.insertBefore(script, s); + + + if (typeof FlashCanvas != "undefined") { + + FlashCanvas.initElement(canvas); + ctx = canvas.getContext('2d'); + } + */ + }else{ + this.ctx = this.canvas.getContext('2d'); + } + + if (!this.ctx){ + // canvas not initialized, let's kill it here + this.log('Canvas not available'); + return; + } + + // set common settings for canvas + this.ctx.textBaseline = "bottom"; + + this.log('Finding background images'); + + this.getImages(this.element); + + this.log('Finding images'); + this.each(document.images,function(i,e){ + _.preloadImage(_.getAttr(e,'src')); + }); + + + if (this.images.length == 0){ + this.start(); + } + + +} + +/* + * Check whether all assets have been loaded and start traversing the DOM + */ + +html2canvas.prototype.start = function(){ + + if (this.images.length == 0 || this.imagesLoaded==this.images.length/2){ + this.log('Started parsing'); + this.newElement(this.element); + + this.parseElement(this.element); + } +} + + + +/* + * Finished rendering, send callback + */ + +html2canvas.prototype.finish = function(){ + this.log("Finished rendering"); + + if (this.opts.renderViewport){ + // let's crop it to viewport only then + var newCanvas = document.createElement('canvas'); + var newctx = newCanvas.getContext('2d'); + newCanvas.width = window.innerWidth; + newCanvas.height = window.innerHeight; + + } + this.opts.ready(this.canvas); +} + diff --git a/src/Draw.js b/src/Draw.js new file mode 100644 index 0000000..2386489 --- /dev/null +++ b/src/Draw.js @@ -0,0 +1,113 @@ + + + + +html2canvas.prototype.newElement = function(el){ + + var bounds = this.getBounds(el); + + var x = bounds.left; + var y = bounds.top; + var w = bounds.width; + var h = bounds.height; + var _ = this, + image; + var bgcolor = this.getCSS(el,"background-color"); + + + + + /* + * TODO add support for different border-style's than solid + */ + var borders = this.getBorderData(el); + + this.each(borders,function(borderSide,borderData){ + if (borderData.width>0){ + var bx = x, + by = y, + bw = w, + bh = h-(borders[2].width); + switch(borderSide){ + case 0: + // top border + bh = borders[0].width; + break; + case 1: + // right border + bx = x+w-(borders[1].width); + bw = borders[1].width; + break; + case 2: + // bottom border + by = (by+h)-(borders[2].width); + bh = borders[2].width; + break; + case 3: + // left border + bw = borders[3].width; + break; + } + + _.newRect(bx,by,bw,bh,borderData.color); + + + + } + + }); + + + + // draw base element bgcolor + this.newRect( + x+borders[3].width, + y+borders[0].width, + w-(borders[1].width+borders[3].width), + h-(borders[0].width+borders[2].width), + bgcolor + ); + + this.drawBackground(el,{ + left: x+borders[3].width, + top: y+borders[0].width, + width: w-(borders[1].width+borders[3].width), + height: h-(borders[0].width+borders[2].width) + }); + + if (el.nodeName=="IMG"){ + image = _.loadImage(_.getAttr(el,'src')); + if (image){ + this.ctx.drawImage(image,x+parseInt(_.getCSS(el,'padding-left'),10),y+parseInt(_.getCSS(el,'padding-top'),10)); + }else{ + this.log("Error loading :" + _.getAttr(el,'src')); + } + } + + + +} + + + + + + +/* + * Function to draw the text on the canvas + */ + +html2canvas.prototype.printText = function(currentText,x,y){ + if (this.trim(currentText).length>0){ + this.ctx.fillText(currentText,x,y); + } +} + + +// Drawing a rectangle +html2canvas.prototype.newRect = function(x,y,w,h,bgcolor){ + if (bgcolor!="transparent"){ + this.ctx.fillStyle = bgcolor; + this.ctx.fillRect (x, y, w, h); + } +} \ No newline at end of file diff --git a/src/Images.js b/src/Images.js new file mode 100644 index 0000000..d32f741 --- /dev/null +++ b/src/Images.js @@ -0,0 +1,68 @@ +/* + * Function to find all images from and background-image + */ +html2canvas.prototype.getImages = function(el) { + + var self = this; + + // TODO remove jQuery dependancy + this.each($(el).contents(),function(i,element){ + var ignRe = new RegExp("("+this.ignoreElements+")"); + if (!ignRe.test(element.nodeName)){ + self.getImages(element); + } + }) + + if (el.nodeType==1 || typeof el.nodeType == "undefined"){ + var background_image = this.getCSS(el,'background-image'); + + if (background_image && background_image != "1" && background_image != "none" && background_image.substring(0,7)!="-webkit" && background_image.substring(0,4)!="-moz"){ + var src = this.backgroundImageUrl(background_image); + this.preloadImage(src); + } + } +} + + +/* + * Load image from storage + */ + +html2canvas.prototype.loadImage = function(src){ + + var imgIndex = this.images.indexOf(src); + if (imgIndex!=-1){ + return this.images[imgIndex+1]; + }else{ + return false; + } + +} + + + +html2canvas.prototype.preloadImage = function(src){ + + if (this.images.indexOf(src)==-1){ + this.images.push(src); + + var img = new Image(); + // TODO remove jQuery dependancy + var _ = this; + $(img).load(function(){ + _.imagesLoaded++; + _.start(); + + }); + img.onerror = function(){ + _.images.splice(_.images.indexOf(img.src),2); + _.imagesLoaded++; + _.start(); + } + img.src = src; + this.images.push(img); + + } + +} + \ No newline at end of file diff --git a/src/LICENSE b/src/LICENSE new file mode 100644 index 0000000..ac2290a --- /dev/null +++ b/src/LICENSE @@ -0,0 +1,7 @@ +/* + * html2canvas v0.12 + * Copyright (c) 2011 Niklas von Hertzen. All rights reserved. + * http://www.twitter.com/niklasvh + * + * Released under MIT License + */ diff --git a/src/Text.js b/src/Text.js new file mode 100644 index 0000000..7accfd0 --- /dev/null +++ b/src/Text.js @@ -0,0 +1,137 @@ + +html2canvas.prototype.newText = function(el,textNode){ + + + + + var family = this.getCSS(el,"font-family"); + var size = this.getCSS(el,"font-size"); + var color = this.getCSS(el,"color"); + + var bold = this.getCSS(el,"font-weight"); + var font_style = this.getCSS(el,"font-style"); + + + var text_decoration = this.getCSS(el,"text-decoration"); + + + // apply text-transform:ation to the text + textNode.nodeValue = this.textTransform(textNode.nodeValue,this.getCSS(el,"text-transform")); + var text = textNode.nodeValue; + + //text = $.trim(text); + if (text.length>0){ + switch(bold){ + case "401": + bold = "bold"; + break; + } + + + + this.ctx.font = bold+" "+font_style+" "+size+" "+family; + this.ctx.fillStyle = color; + + + + + + var oldTextNode = textNode; + for(var c=0;c -* @created 16.7.2011 -* @website http://hertzen.com - */ - /* * jQuery helper plugin for examples and tests @@ -12,12 +6,19 @@ (function( $ ){ $.fn.html2canvas = function() { + var date = new Date(); var message, - timer; + timeoutTimer, + timer = date.getTime(); - html2canvas(this.get(0),{ + new html2canvas(this.get(0),{ ready:function(canvas){ - document.body.appendChild(canvas); + + var finishTime = new Date(); + // console.log((finishTime.getTime()-timer)/1000); + + + document.body.appendChild(canvas); var canvas = $(canvas); canvas.css('position','absolute') .css('left',0).css('top',0); @@ -41,8 +42,8 @@ function throwMessage(msg){ - window.clearTimeout(timer); - timer = window.setTimeout(function(){ + window.clearTimeout(timeoutTimer); + timeoutTimer = window.setTimeout(function(){ message.fadeOut(function(){ message.remove(); }); diff --git a/tests/background.html b/tests/background.html index dd21749..1ab60a5 100644 --- a/tests/background.html +++ b/tests/background.html @@ -9,13 +9,15 @@ Background attribute tests - - - + + + diff --git a/tests/text.html b/tests/text.html index 76d7502..119c233 100644 --- a/tests/text.html +++ b/tests/text.html @@ -9,12 +9,12 @@ Text tests - - - + + + + @@ -64,7 +64,7 @@
-

Verdana

+

Verdana

  1. text-decoration:none;
  2. text-decoration:underline;
  3. @@ -87,7 +87,7 @@
-

Tahoma

+

Tahoma

  1. text-decoration:none;
  2. text-decoration:underline;
  3. @@ -124,6 +124,6 @@
  4. text-align:right;width:300px;
  5. font-variant:small-caps; (not yet supported)
  6. - +