split files & created build file

This commit is contained in:
Niklas von Hertzen 2011-07-16 19:59:15 +03:00
parent 4b7ea7d7cc
commit adc30b6361
30 changed files with 4165 additions and 711 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/nbproject/private/
/images/
/tests/templates/
/dist/

21
LICENSE Normal file
View File

@ -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.
*/

77
build.xml Normal file
View File

@ -0,0 +1,77 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="html2canvas" basedir=".">
<property name="src.dir" location="src"/>
<property name="lib.dir" location="lib"/>
<property name="build.dir" location="build"/>
<property name="dist" location="dist"/>
<property name="jquery-externs" value="jquery-1.4.4.externs.js"/>
<property name="JS_NAME" value="html2canvas.js"/>
<property name="JS_NAME_MIN" value="html2canvas.min.js"/>
<property name="JQUERY_PLUGIN_NAME" value="jquery.plugin.html2canvas.js"/>
<path id="sourcefiles">
<fileset dir="${src.dir}" includes="LICENSE"/>
<fileset dir="." includes="LICENSE"/>
<fileset dir="${src.dir}" includes="Core.js"/>
<fileset dir="${src.dir}" includes="Background.js"/>
<fileset dir="${src.dir}" includes="Border.js"/>
<fileset dir="${src.dir}" includes="Draw.js"/>
<fileset dir="${src.dir}" includes="Images.js"/>
<fileset dir="${src.dir}" includes="Text.js"/>
<fileset dir="${src.dir}" includes="Traversing.js"/>
<fileset dir="${src.dir}" includes="Util.js"/>
</path>
<path id="jquery-plugin">
<fileset dir="${src.dir}" includes="LICENSE"/>
<fileset dir="." includes="LICENSE"/>
<fileset dir="${src.dir}/plugins" includes="${JQUERY_PLUGIN_NAME}"/>
</path>
<path id="minified">
<fileset dir="${src.dir}" includes="LICENSE"/>
<fileset dir="${build.dir}" includes="tmp.js"/>
</path>
<target name="plugins">
<concat fixlastline="yes" destfile="${build.dir}/${JQUERY_PLUGIN_NAME}">
<path refid="jquery-plugin"/>
</concat>
</target>
<target name="source">
<concat fixlastline="yes" destfile="${build.dir}/${JS_NAME}">
<path refid="sourcefiles"/>
</concat>
</target>
<taskdef name="jscomp" classname="com.google.javascript.jscomp.ant.CompileTask"
classpath="${lib.dir}/compiler.jar"/>
<target name="release">
<jscomp compilationLevel="simple" warning="verbose"
debug="false"
output="${build.dir}/tmp.js">
<externs dir="${lib.dir}">
<file name="${jquery-externs}"/>
</externs>
<sources dir="${build.dir}">
<file name="${JS_NAME}" />
</sources>
</jscomp>
<concat fixlastline="yes" destfile="${build.dir}/${JS_NAME_MIN}">
<path refid="minified"/>
</concat>
</target>
</project>

871
build/html2canvas.js Normal file
View File

@ -0,0 +1,871 @@
/*
* html2canvas v0.12 <http://html2canvas.hertzen.com>
* 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 (bgy<bounds.top){
add = bounds.top-bgy;
bgy = bounds.top;
}else{
add = 0;
}
this.drawbackgroundRepeatX(image,bgp,bounds.left,bgy,bounds.width,height);
if (add>0){
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 <img>:" + _.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 <img> 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<text.length;c++){
var newTextNode = oldTextNode.splitText(1);
if (this.useRangeBounds){
// getBoundingClientRect is supported for ranges
if (document.createRange){
var range = document.createRange();
range.selectNode(oldTextNode);
}else{
// TODO add IE support
var range = document.body.createTextRange();
}
if (range.getBoundingClientRect()){
var bounds = range.getBoundingClientRect();
}else{
var bounds = {};
}
}else{
// it isn't supported, so let's wrap it inside an element instead and the bounds there
var parent = oldTextNode.parentNode;
var wrapElement = document.createElement('wrapper');
var backupText = oldTextNode.cloneNode(true);
wrapElement.appendChild(oldTextNode.cloneNode(true));
parent.replaceChild(wrapElement,oldTextNode);
var bounds = this.getBounds(wrapElement);
parent.replaceChild(backupText,wrapElement);
}
this.printText(oldTextNode.nodeValue,bounds.left,bounds.bottom);
switch(text_decoration) {
case "underline":
// guesstimate the y-position of the line
// TODO try and find a more accurate way to find the baseline of the text
this.newRect(bounds.left,Math.round(bounds.bottom-(bounds.height/7)),bounds.width,1,color);
break;
case "overline":
this.newRect(bounds.left,bounds.top,bounds.width,1,color);
break;
case "line-through":
// TODO try and find exact position for line-through
this.newRect(bounds.left,Math.round(bounds.top+(bounds.height/2)),bounds.width,1,color);
break;
}
oldTextNode = newTextNode;
}
}
}
/*
* Function to apply text-transform attribute to text
*/
html2canvas.prototype.textTransform = function(text,transform){
switch(transform){
case "lowercase":
return text.toLowerCase();
break;
case "capitalize":
return text.replace( /(^|\s|:|-|\(|\))([a-z])/g , function(m,p1,p2){
return p1+p2.toUpperCase();
} );
break;
case "uppercase":
return text.toUpperCase();
break;
default:
return text;
}
}
/*
*Function to trim whitespace from text
*/
html2canvas.prototype.trim = function(text) {
return text.replace(/^\s*/, "").replace(/\s*$/, "");
}
html2canvas.prototype.parseElement = function(element){
var _ = this;
this.each(element.children,function(index,el){
_.parsing(el);
});
this.finish();
}
html2canvas.prototype.parsing = function(el){
var ignRe = new RegExp("("+this.ignoreElements+")");
var _ = this;
if (!ignRe.test(el.nodeName)){
this.newElement(el);
// TODO remove jQuery dependancy
var contents = $(el).contents();
if (contents.length == 1){
// check nodeType
if (contents[0].nodeType==1){
// it's an element, so let's look inside it
this.parsing(contents[0]);
}else if (contents[0].nodeType==3){
// it's a text node, so lets print the text
this.newText(el,contents[0]);
}
}else{
this.each(contents,function(cid,cel){
if (cel.nodeType==1){
// element
_.parsing(cel);
}else if (cel.nodeType==3){
_.newText(el,cel);
}
});
}
}
}
// Simple logger
html2canvas.prototype.log = function(a){
if (this.opts.logging){
var logger = window.console.log || function(log){
alert(log);
};
/*
if (typeof(window.console) != "undefined" && console.log){
console.log(a);
}else{
alert(a);
}*/
}
}
/**
* Function to provide bounds for element
* @return {Bounds} object with position and dimension information
*/
html2canvas.prototype.getBounds = function(el){
window.scroll(0,0);
if (el.getBoundingClientRect){
var bounds = el.getBoundingClientRect();
bounds.top = bounds.top;
bounds.left = bounds.left;
return bounds;
}else{
// TODO remove jQuery dependancy
var p = $(el).offset();
return {
left: p.left + parseInt(this.getCSS(el,"border-left-width"),10),
top: p.top + parseInt(this.getCSS(el,"border-top-width"),10),
width:$(el).innerWidth(),
height:$(el).innerHeight()
}
}
}
/*
* Function for looping through array
*/
html2canvas.prototype.each = function(arrayLoop,callbackFunc){
callbackFunc = callbackFunc || function(){};
for (var i=0;i<arrayLoop.length;i++){
callbackFunc(i,arrayLoop[i]);
}
}
/*
* Function for fetching the element attribute
*/
html2canvas.prototype.getAttr = function(el,attribute){
return el.getAttribute(attribute);
//return $(el).attr(attribute);
}
/*
* Function to extend object
*/
html2canvas.prototype.extendObj = function(options,defaults){
for (var key in options){
defaults[key] = options[key];
}
return defaults;
}
/*
* Get element childNodes
*/
html2canvas.prototype.getContents = function(el){
return (el.nodeName == "iframe" ) ?
el.contentDocument || el.contentWindow.document :
el.childNodes;
}
/*
* Function for fetching the css attribute
* TODO remove jQuery dependancy
*/
html2canvas.prototype.getCSS = function(el,attribute){
return $(el).css(attribute);
}

27
build/html2canvas.min.js vendored Normal file
View File

@ -0,0 +1,27 @@
/*
* html2canvas v0.12 <http://html2canvas.hertzen.com>
* 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,c<b.top?(g=b.top-c,c=b.top):g=0,this.drawbackgroundRepeatX(d,h,b.left,c,b.width,f),g>0&&(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 <img>:"+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<i.length;f++){h=c.splitText(1);if(this.useRangeBounds)document.createRange?
(g=document.createRange(),g.selectNode(c)):g=document.body.createTextRange(),g=g.getBoundingClientRect()?g.getBoundingClientRect():{};else{var n=c.parentNode,j=document.createElement("wrapper"),k=c.cloneNode(!0);j.appendChild(c.cloneNode(!0));n.replaceChild(j,c);g=this.getBounds(j);n.replaceChild(k,j)}this.printText(c.nodeValue,g.left,g.bottom);switch(e){case "underline":this.newRect(g.left,Math.round(g.bottom-g.height/7),g.width,1,d);break;case "overline":this.newRect(g.left,g.top,g.width,1,d);break;
case "line-through":this.newRect(g.left,Math.round(g.top+g.height/2),g.width,1,d)}c=h}}};html2canvas.prototype.textTransform=function(a,b){switch(b){case "lowercase":return a.toLowerCase();case "capitalize":return a.replace(/(^|\s|:|-|\(|\))([a-z])/g,function(a,b,d){return b+d.toUpperCase()});case "uppercase":return a.toUpperCase();default:return a}};html2canvas.prototype.trim=function(a){return a.replace(/^\s*/,"").replace(/\s*$/,"")};
html2canvas.prototype.parseElement=function(a){var b=this;this.each(a.children,function(a,f){b.parsing(f)});this.finish()};html2canvas.prototype.parsing=function(a){var b=this;if(!RegExp("("+this.ignoreElements+")").test(a.nodeName)){this.newElement(a);var c=$(a).contents();c.length==1?c[0].nodeType==1?this.parsing(c[0]):c[0].nodeType==3&&this.newText(a,c[0]):this.each(c,function(c,d){d.nodeType==1?b.parsing(d):d.nodeType==3&&b.newText(a,d)})}};html2canvas.prototype.log=function(){};
html2canvas.prototype.getBounds=function(a){window.scroll(0,0);if(a.getBoundingClientRect)return a=a.getBoundingClientRect(),a.top=a.top,a.left=a.left,a;else{var b=$(a).offset();return{left:b.left+parseInt(this.getCSS(a,"border-left-width"),10),top:b.top+parseInt(this.getCSS(a,"border-top-width"),10),width:$(a).innerWidth(),height:$(a).innerHeight()}}};html2canvas.prototype.each=function(a,b){for(var b=b||function(){},c=0;c<a.length;c++)b(c,a[c])};html2canvas.prototype.getAttr=function(a,b){return a.getAttribute(b)};
html2canvas.prototype.extendObj=function(a,b){for(var c in a)b[c]=a[c];return b};html2canvas.prototype.getContents=function(a){return a.nodeName=="iframe"?a.contentDocument||a.contentWindow.document:a.childNodes};html2canvas.prototype.getCSS=function(a,b){return $(a).css(b)};

View File

@ -0,0 +1,101 @@
/*
* html2canvas v0.12 <http://html2canvas.hertzen.com>
* 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 = $('<div />').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 );

19
build/tmp.js Normal file
View File

@ -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,c<b.top?(g=b.top-c,c=b.top):g=0,this.drawbackgroundRepeatX(d,h,b.left,c,b.width,f),g>0&&(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 <img>:"+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<i.length;f++){h=c.splitText(1);if(this.useRangeBounds)document.createRange?
(g=document.createRange(),g.selectNode(c)):g=document.body.createTextRange(),g=g.getBoundingClientRect()?g.getBoundingClientRect():{};else{var n=c.parentNode,j=document.createElement("wrapper"),k=c.cloneNode(!0);j.appendChild(c.cloneNode(!0));n.replaceChild(j,c);g=this.getBounds(j);n.replaceChild(k,j)}this.printText(c.nodeValue,g.left,g.bottom);switch(e){case "underline":this.newRect(g.left,Math.round(g.bottom-g.height/7),g.width,1,d);break;case "overline":this.newRect(g.left,g.top,g.width,1,d);break;
case "line-through":this.newRect(g.left,Math.round(g.top+g.height/2),g.width,1,d)}c=h}}};html2canvas.prototype.textTransform=function(a,b){switch(b){case "lowercase":return a.toLowerCase();case "capitalize":return a.replace(/(^|\s|:|-|\(|\))([a-z])/g,function(a,b,d){return b+d.toUpperCase()});case "uppercase":return a.toUpperCase();default:return a}};html2canvas.prototype.trim=function(a){return a.replace(/^\s*/,"").replace(/\s*$/,"")};
html2canvas.prototype.parseElement=function(a){var b=this;this.each(a.children,function(a,f){b.parsing(f)});this.finish()};html2canvas.prototype.parsing=function(a){var b=this;if(!RegExp("("+this.ignoreElements+")").test(a.nodeName)){this.newElement(a);var c=$(a).contents();c.length==1?c[0].nodeType==1?this.parsing(c[0]):c[0].nodeType==3&&this.newText(a,c[0]):this.each(c,function(c,d){d.nodeType==1?b.parsing(d):d.nodeType==3&&b.newText(a,d)})}};html2canvas.prototype.log=function(){};
html2canvas.prototype.getBounds=function(a){window.scroll(0,0);if(a.getBoundingClientRect)return a=a.getBoundingClientRect(),a.top=a.top,a.left=a.left,a;else{var b=$(a).offset();return{left:b.left+parseInt(this.getCSS(a,"border-left-width"),10),top:b.top+parseInt(this.getCSS(a,"border-top-width"),10),width:$(a).innerWidth(),height:$(a).innerHeight()}}};html2canvas.prototype.each=function(a,b){for(var b=b||function(){},c=0;c<a.length;c++)b(c,a[c])};html2canvas.prototype.getAttr=function(a,b){return a.getAttribute(b)};
html2canvas.prototype.extendObj=function(a,b){for(var c in a)b[c]=a[c];return b};html2canvas.prototype.getContents=function(a){return a.nodeName=="iframe"?a.contentDocument||a.contentWindow.document:a.childNodes};html2canvas.prototype.getCSS=function(a,b){return $(a).css(b)};

View File

@ -1,8 +1,8 @@
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="html2canvas.js"></script>
<script type="text/javascript" src="jquery.plugin.html2canvas.js"></script>
<script type="text/javascript" src="external/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="build/html2canvas.js"></script>
<script type="text/javascript" src="build/jquery.plugin.html2canvas.js"></script>
<script type="text/javascript">
$(window).ready(function() {
$('body').html2canvas();

View File

@ -3,10 +3,9 @@
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="html2canvas.js"></script>
<script type="text/javascript" src="jquery.plugin.html2canvas.js"></script>
<script type="text/javascript" src="external/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="build/html2canvas.js"></script>
<script type="text/javascript" src="build/jquery.plugin.html2canvas.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('body').html2canvas();

View File

@ -9,12 +9,9 @@ and open the template in the editor.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.js"></script>
<script type="text/javascript" src="html2canvas.js"></script>
<script type="text/javascript" src="jquery.plugin.html2canvas.js"></script>
<script type="text/javascript" src="external/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="build/html2canvas.js"></script>
<script type="text/javascript" src="build/jquery.plugin.html2canvas.js"></script>
<script type="text/javascript">
$(function(){

18
external/jquery-1.6.2.min.js vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

21
html2canvas.min.js vendored
View File

@ -1,21 +0,0 @@
/*
* html2canvas v0.11 <http://html2canvas.hertzen.com>
*
* Copyright 2011, Niklas von Hertzen <http://hertzen.com>
* http://www.twitter.com/niklasvh
*
* Released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
*/
function html2canvas(s,I){function t(){if(j.length==0||u==j.length/2)m("Started parsing"),x(s),J(s)}function m(a){q.logging&&(typeof console!="undefined"&&console.log?console.log(a):alert(a))}function y(a){if(j.indexOf(a)==-1){j.push(a);var b=new Image;$(b).load(function(){u++;t()});b.onerror=function(){j.splice(j.indexOf(b.src),2);u++;t()};b.src=a;j.push(b)}}function z(a){n($(a).contents(),function(a,e){RegExp("("+A+")").test(e.nodeName)||z(e)});if(a.nodeType==1||typeof a.nodeType=="undefined")if((a=
i(a,"background-image"))&&a!="1"&&a!="none"&&a.substring(0,7)!="-webkit"&&a.substring(0,4)!="-moz")a=B(a),y(a)}function v(a){if(!RegExp("("+A+")").test(a.nodeName)){x(a);var b=$(a).contents();b.length==1?b[0].nodeType==1?v(b[0]):b[0].nodeType==3&&C(a,b[0]):n(b,function(b,g){g.nodeType==1?v(g):g.nodeType==3&&C(a,g)})}}function J(a){n(a.children,function(a,e){v(e)});K()}function B(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}function D(a){a=
j.indexOf(a);return a!=-1?j[a+1]:!1}function E(a){window.scroll(0,0);if(a.getBoundingClientRect)return a=a.getBoundingClientRect(),a.top=a.top,a.left=a.left,a;else{var b=$(a).offset();return{left:b.left+parseInt(i(a,"border-left-width"),10),top:b.top+parseInt(i(a,"border-top-width"),10),width:$(a).innerWidth(),height:$(a).innerHeight()}}}function x(a){var b=E(a),e=b.left,g=b.top,f=b.width,d=b.height,b=i(a,"background-color"),c=L(a);n(c,function(a,b){if(b.width>0){var h=e,i=g,j=f,k=d-c[2].width;switch(a){case 0:k=
c[0].width;break;case 1:h=e+f-c[1].width;j=c[1].width;break;case 2:i=i+d-c[2].width;k=c[2].width;break;case 3:j=c[3].width}r(h,i,j,k,b.color)}});r(e+c[3].width,g+c[0].width,f-(c[1].width+c[3].width),d-(c[0].width+c[2].width),b);M(a,{left:e+c[3].width,top:g+c[0].width,width:f-(c[1].width+c[3].width),height:d-(c[0].width+c[2].width)});a.nodeName=="IMG"&&((F=D(a.getAttribute("src")))?k.drawImage(F,e+parseInt(i(a,"padding-left"),10),g+parseInt(i(a,"padding-top"),10)):m("Error loading <img>:"+a.getAttribute("src")))}
function M(a,b){var e=i(a,"background-image"),g=i(a,"background-repeat");if(typeof e!="undefined"&&/^(1|none)$/.test(e)==!1){var e=B(e),f=D(e),d,c=i(a,"background-position").split(" "),o;c[0].indexOf("%")!=-1?(o=parseFloat(c[0])/100,d=b.width*o-f.width*o):d=parseInt(c[0],10);c[1].indexOf("%")!=-1?(o=parseFloat(c[1])/100,c=b.height*o-f.height*o):c=parseInt(c[1],10);d={top:c,left:d};if(f)switch(g){case "repeat-x":G(f,d,b.left,b.top,b.width,b.height);break;case "repeat-y":var e=b.left,g=b.top,c=b.height,
j=Math.min(f.width,b.width);d.top-=Math.ceil(d.top/f.height)*f.height;for(h=g+d.top;h<=c+g;)o=Math.floor(h+f.height)>c+g?c+g-h:f.height,w(f,e+d.left,h,j,o,e,g),h=Math.floor(h+f.height);break;case "no-repeat":w(f,d.left+b.left,d.top+b.top,Math.min(b.width,f.width),Math.min(b.height,f.height),b.left,b.top);break;default:d.top-=Math.ceil(d.top/f.height)*f.height;for(h=b.top+d.top;h<=b.height+b.top;)e=Math.min(f.height,b.height+b.top-h),e=Math.floor(h+f.height)>e+h?e+h-h:f.height,h<b.top?(g=b.top-h,h=
b.top):g=0,G(f,d,b.left,h,b.width,e),g>0&&(d.top+=g),h=Math.floor(h+f.height)-g}else m("Error loading background:"+e)}}function G(a,b,e,g,f,d){var d=Math.min(a.height,d),c;b.left-=Math.ceil(b.left/a.width)*a.width;for(l=e+b.left;l<=f+e;)c=Math.floor(l+a.width)>f+e?f+e-l:a.width,w(a,l,g+b.top,c,d,e,g),l=Math.floor(l+a.width)}function w(a,b,e,g,f,d,c){var h=0,i=0;d-b>0&&(h=d-b);c-e>0&&(i=c-e);k.drawImage(a,h,i,g-h,f-i,b+h,e+i,g-h,f-i)}function L(a){var b=[];n(["top","right","bottom","left"],function(e,
g){b.push({width:parseInt(i(a,"border-"+g+"-width"),10),color:i(a,"border-"+g+"-color")})});return b}function r(a,b,e,g,f){if(f!="transparent")k.fillStyle=f,k.fillRect(a,b,e,g)}function i(a,b){return $(a).css(b)}function C(a,b){var e=i(a,"font-family"),g=i(a,"font-size"),f=i(a,"color"),d=i(a,"font-weight"),c=i(a,"font-style"),h=i(a,"text-decoration");b.nodeValue=N(b.nodeValue,i(a,"text-transform"));var j=b.nodeValue;if(j.length>0){switch(d){case "401":d="bold"}k.font=d+" "+c+" "+g+" "+e;k.fillStyle=
f;c=b;for(e=0;e<j.length;e++){g=c.splitText(1);if(H)document.createRange?(d=document.createRange(),d.selectNode(c)):d=document.body.createTextRange(),d=d.getBoundingClientRect()?d.getBoundingClientRect():{};else{var m=c.parentNode,l=document.createElement("wrapper"),n=c.cloneNode(!0);l.appendChild(c.cloneNode(!0));m.replaceChild(l,c);d=E(l);m.replaceChild(n,l)}c=c.nodeValue;m=d.left;l=d.bottom;c.replace(/^\s*/,"").replace(/\s*$/,"").length>0&&k.fillText(c,m,l);switch(h){case "underline":r(d.left,
Math.round(d.bottom-d.height/7),d.width,1,f);break;case "overline":r(d.left,d.top,d.width,1,f);break;case "line-through":r(d.left,Math.round(d.top+d.height/2),d.width,1,f)}c=g}}}function N(a,b){switch(b){case "lowercase":return a.toLowerCase();case "capitalize":return a.replace(/(^|\s|:|-|\(|\))([a-z])/g,function(a,b,f){return b+f.toUpperCase()});case "uppercase":return a.toUpperCase();default:return a}}function K(){m("Finished rendering");if(q.renderViewport){var a=document.createElement("canvas");
a.getContext("2d");a.width=window.innerWidth;a.height=window.innerHeight}q.ready(p)}function n(a,b){for(var b=b||function(){},e=0;e<a.length;e++)b(e,a[e])}var q={logging:!1,ready:function(a){document.body.appendChild(a)},renderViewport:!0},q=function(a,b){for(var e in a)b[e]=a[e];return b}(I||{},q),u=0,j=[],p,k,l,h,F,A="IFRAME|OBJECT|PARAM",H=!1;if(document.createRange)var O=document.createRange(),H=new Boolean(O.getBoundingClientRect);(function(){p=document.createElement("canvas");p.width=$(document).width();
p.height=$(document).height()+10;p.getContext&&(k=p.getContext("2d"));k?(k.textBaseline="bottom",m("Finding background images"),z(s),m("Finding images"),n(document.images,function(a,b){y(b.getAttribute("src"))}),j.length==0&&t()):m("Canvas not available")})()};

211
index.html Normal file
View File

@ -0,0 +1,211 @@
<!DOCTYPE html>
<html>
<!--
* @author Niklas von Hertzen <niklas at hertzen.com>
* @created 6.7.2011
* @website http://hertzen.com
-->
<head>
<title>html2canvas - screenshots with JavaScript</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="http://hertzen.com/min/g=css" media="screen" rel="stylesheet" type="text/css" >
<script type="text/javascript" src="external/jquery-1.6.2.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js" type="text/javascript"></script>
<style>
body,html{
background: #262626;
}
</style>
<script type="text/javascript" src="http://www.hertzen.com/js/ganalytics-heatmap.js"></script>
<script type="text/javascript" src="build/html2canvas.js"></script>
<script type="text/javascript" src="build/jquery.plugin.html2canvas.js"></script>
<script type="text/javascript">
$(window).ready(function() {
var saveCanvas;
var hideCanvas;
$('.buttons').children().button();
$('button').click(function(){
$(this).unbind('click');
html2canvas($('body').get(0),{
logging:true,
ready:function(canvas){
saveCanvas = $(canvas).css('position','absolute')
.css('left',0).css('top',0);
$('body').append(canvas);
hideCanvas = $('<div />')
.css({
position:'fixed',
right:10,
top:10,
background: 'red',
color:'white',
padding:10,
fontSize:16,
cursor:'pointer',
borderRadius:5
}).text('Hide screenshot').click(function(){
$(hideCanvas).hide();
$(canvas).hide();
});
$('button').bind('click',function(){
$(hideCanvas).show();
$(canvas).show();
});
$('body').append(hideCanvas);
}
});
});
});
var _gaq = _gaq || [];_gaq.push(['_setAccount', 'UA-188600-10']);_gaq.push(['_trackPageview']);(function() {var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);})();
</script>
<style>
.examples li{
list-style-type:none;
float:left;
width:120px;
}
h3{
clear:both;
}
.share{
float:right;
}
.share div{
float:right;
}
h1{
float:left;
}
p{
clear:both;
}
</style>
<meta property="og:title" content="html2canvas - screenshots with JavaScript" />
<meta property="og:url" content="http://html2canvas.hertzen.com/" />
<meta property="og:image" content="http://html2canvas.hertzen.com/image.jpg" />
<meta property="og:site_name" content="Hertzen" />
<meta property="fb:admins" content="516961119" />
<meta property="og:type" content="website" />
</head>
<body>
<div class="wrapper" style="width:1000px;">
<div id="content" style="margin:10px auto;">
<div class="wrappost" style="width:100%;">
<div class="post entry">
<div class="entry">
<h1>html2canvas - Screenshots with JavaScript</h1>
<div class="share">
<div style="margin-left:17px;">
<!-- Place this tag in your head or just before your close body tag -->
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<!-- Place this tag where you want the +1 button to render -->
<g:plusone size="tall"></g:plusone>
</div>
<div>
<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://html2canvas.hertzen.com/" data-text="html2canvas - screenshots with #JavaScript" data-count="vertical" data-via="niklasvh">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
</div>
</div>
<p>
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.
</p>
<h2>How does it work?</h2>
<p>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
</p>
<p> It does <b>not require any rendering from the server</b>, as the whole image is created on the <b>clients browser</b>. However, for browsers without <code>canvas</code> support alternatives such as <a href="http://flashcanvas.net/">flashcanvas</a> or <a href="http://excanvas.sourceforge.net/">ExplorerCanvas</a> are necessary to create the image.
Additionally, to render <code>iframe</code> content or images situated outside of the <a href="http://en.wikipedia.org/wiki/Same_origin_policy">same origin policy</a> 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.
</p>
<p>The minified version of the script is currently less than <b>6kb</b> (less than <b>2.5kb</b> gzipped). It currently still has jQuery as a dependancy, but that will get removed soon.</p>
<h2>Browser compatibility</h2>
<p>The script should work fine on the following browsers:</p>
<ul>
<li>Firefox 3.5+</li>
<li>Google Chrome</li>
<li>&gt;=IE9 (Older versions compatible with the use of flashcanvas)</li>
</ul>
<p>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.</p>
<h2>So what isn't included yet?</h2>
<p>
There are still a lot of CSS properties missing, including most CSS3 properties such as <code>text-shadow</code>, <code>box-radius</code> 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 <code>object</code> content such as Flash will most likely never be supported.
</p>
<div class="buttons">
<button>Create screenshot of this page</button>
<a href="https://github.com/niklasvh/html2canvas">Download on GitHub</a>
</div><br />
<h2>Examples</h2>
<p>The screenshot gets overlayed on top of the page. You can toggle the visibility of the canvas by clicking anywhere on the page.</p>
<ul class="examples">
<li><a href="demo.html" title="Basic" target="_blank"><img src="images/1.jpg" alt="Basic" /></a></li>
<li><a href="tests/templates/dusky/index.html" title="Dusky template" target="_blank"><img src="images/8.jpg" alt="Dusky template" /></a></li>
<li><a href="tests/templates/combination/index.html" title="Combination template" target="_blank"><img src="images/6.jpg" alt="Combination template" /></a></li>
<li><a href="tests/templates/projection/index.html" title="Projection template" target="_blank"><img src="images/9.jpg" alt="Projection template" /></a></li>
<li><a href="tests/templates/facing/index.html" title="Facing template" target="_blank"><img src="images/11.jpg" alt="Facing template" /></a></li>
<li><a href="tests/templates/palmtrees/index.html" title="Palmtrees template" target="_blank"><img src="images/7.jpg" alt="Palmtrees template" /></a></li>
<li><a href="tests/templates/artificial/index.html" title="Artificial template" target="_blank"><img src="images/10.jpg" alt="Artificial template" /></a></li>
</ul>
<div style="clear:both;"></div>
<h3>Tests</h3>
<ul>
<li><a href="demo2.html">CSS tests</li>
<li><a href="demo3.html">More CSS tests</li>
<li><a href="tests/background.html">Background tests</li>
<li><a href="tests/text.html">Text tests</li>
</ul>
<p>Any <a href="http://www.twitter.com/niklasvh">comments or feedback</a> is greatly appreciated.</p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

BIN
lib/compiler.jar Normal file

Binary file not shown.

1204
lib/jquery-1.4.4.externs.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -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=.

9
nbproject/project.xml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.php.project</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/php-project/1">
<name>html2canvas</name>
</data>
</configuration>
</project>

211
src/Background.js Normal file
View File

@ -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 (bgy<bounds.top){
add = bounds.top-bgy;
bgy = bounds.top;
}else{
add = 0;
}
this.drawbackgroundRepeatX(image,bgp,bounds.left,bgy,bounds.width,height);
if (add>0){
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
);
}

18
src/Border.js Normal file
View File

@ -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;
}

140
src/Core.js Normal file
View File

@ -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);
}

113
src/Draw.js Normal file
View File

@ -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 <img>:" + _.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);
}
}

68
src/Images.js Normal file
View File

@ -0,0 +1,68 @@
/*
* Function to find all images from <img> 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);
}
}

7
src/LICENSE Normal file
View File

@ -0,0 +1,7 @@
/*
* html2canvas v0.12 <http://html2canvas.hertzen.com>
* Copyright (c) 2011 Niklas von Hertzen. All rights reserved.
* http://www.twitter.com/niklasvh
*
* Released under MIT License
*/

137
src/Text.js Normal file
View File

@ -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<text.length;c++){
var newTextNode = oldTextNode.splitText(1);
if (this.useRangeBounds){
// getBoundingClientRect is supported for ranges
if (document.createRange){
var range = document.createRange();
range.selectNode(oldTextNode);
}else{
// TODO add IE support
var range = document.body.createTextRange();
}
if (range.getBoundingClientRect()){
var bounds = range.getBoundingClientRect();
}else{
var bounds = {};
}
}else{
// it isn't supported, so let's wrap it inside an element instead and the bounds there
var parent = oldTextNode.parentNode;
var wrapElement = document.createElement('wrapper');
var backupText = oldTextNode.cloneNode(true);
wrapElement.appendChild(oldTextNode.cloneNode(true));
parent.replaceChild(wrapElement,oldTextNode);
var bounds = this.getBounds(wrapElement);
parent.replaceChild(backupText,wrapElement);
}
this.printText(oldTextNode.nodeValue,bounds.left,bounds.bottom);
switch(text_decoration) {
case "underline":
// guesstimate the y-position of the line
// TODO try and find a more accurate way to find the baseline of the text
this.newRect(bounds.left,Math.round(bounds.bottom-(bounds.height/7)),bounds.width,1,color);
break;
case "overline":
this.newRect(bounds.left,bounds.top,bounds.width,1,color);
break;
case "line-through":
// TODO try and find exact position for line-through
this.newRect(bounds.left,Math.round(bounds.top+(bounds.height/2)),bounds.width,1,color);
break;
}
oldTextNode = newTextNode;
}
}
}
/*
* Function to apply text-transform attribute to text
*/
html2canvas.prototype.textTransform = function(text,transform){
switch(transform){
case "lowercase":
return text.toLowerCase();
break;
case "capitalize":
return text.replace( /(^|\s|:|-|\(|\))([a-z])/g , function(m,p1,p2){
return p1+p2.toUpperCase();
} );
break;
case "uppercase":
return text.toUpperCase();
break;
default:
return text;
}
}
/*
*Function to trim whitespace from text
*/
html2canvas.prototype.trim = function(text) {
return text.replace(/^\s*/, "").replace(/\s*$/, "");
}

54
src/Traversing.js Normal file
View File

@ -0,0 +1,54 @@
html2canvas.prototype.parseElement = function(element){
var _ = this;
this.each(element.children,function(index,el){
_.parsing(el);
});
this.finish();
}
html2canvas.prototype.parsing = function(el){
var ignRe = new RegExp("("+this.ignoreElements+")");
var _ = this;
if (!ignRe.test(el.nodeName)){
this.newElement(el);
// TODO remove jQuery dependancy
var contents = $(el).contents();
if (contents.length == 1){
// check nodeType
if (contents[0].nodeType==1){
// it's an element, so let's look inside it
this.parsing(contents[0]);
}else if (contents[0].nodeType==3){
// it's a text node, so lets print the text
this.newText(el,contents[0]);
}
}else{
this.each(contents,function(cid,cel){
if (cel.nodeType==1){
// element
_.parsing(cel);
}else if (cel.nodeType==3){
_.newText(el,cel);
}
});
}
}
}

99
src/Util.js Normal file
View File

@ -0,0 +1,99 @@
// Simple logger
html2canvas.prototype.log = function(a){
if (this.opts.logging){
var logger = window.console.log || function(log){
alert(log);
};
/*
if (typeof(window.console) != "undefined" && console.log){
console.log(a);
}else{
alert(a);
}*/
}
}
/**
* Function to provide bounds for element
* @return {Bounds} object with position and dimension information
*/
html2canvas.prototype.getBounds = function(el){
window.scroll(0,0);
if (el.getBoundingClientRect){
var bounds = el.getBoundingClientRect();
bounds.top = bounds.top;
bounds.left = bounds.left;
return bounds;
}else{
// TODO remove jQuery dependancy
var p = $(el).offset();
return {
left: p.left + parseInt(this.getCSS(el,"border-left-width"),10),
top: p.top + parseInt(this.getCSS(el,"border-top-width"),10),
width:$(el).innerWidth(),
height:$(el).innerHeight()
}
}
}
/*
* Function for looping through array
*/
html2canvas.prototype.each = function(arrayLoop,callbackFunc){
callbackFunc = callbackFunc || function(){};
for (var i=0;i<arrayLoop.length;i++){
callbackFunc(i,arrayLoop[i]);
}
}
/*
* Function for fetching the element attribute
*/
html2canvas.prototype.getAttr = function(el,attribute){
return el.getAttribute(attribute);
//return $(el).attr(attribute);
}
/*
* Function to extend object
*/
html2canvas.prototype.extendObj = function(options,defaults){
for (var key in options){
defaults[key] = options[key];
}
return defaults;
}
/*
* Get element childNodes
*/
html2canvas.prototype.getContents = function(el){
return (el.nodeName == "iframe" ) ?
el.contentDocument || el.contentWindow.document :
el.childNodes;
}
/*
* Function for fetching the css attribute
* TODO remove jQuery dependancy
*/
html2canvas.prototype.getCSS = function(el,attribute){
return $(el).css(attribute);
}

View File

@ -1,9 +1,3 @@
/*
* @author Niklas von Hertzen <niklas at hertzen.com>
* @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();
});

View File

@ -9,13 +9,15 @@
<title>Background attribute tests</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="#" type="text/css" rel="stylesheet">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" src="../html2canvas.min.js"></script>
<script type="text/javascript" src="../jquery.plugin.html2canvas.js"></script>
<script type="text/javascript" src="../external/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../build/html2canvas.js"></script>
<script type="text/javascript" src="../build/jquery.plugin.html2canvas.js"></script>
<script type="text/javascript">
$(window).ready(function() {
$('body').html2canvas();
$('body').html2canvas({
logging:true
});
});
</script>

View File

@ -9,12 +9,12 @@
<title>Text tests</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="#" type="text/css" rel="stylesheet">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" src="../html2canvas.js"></script>
<script type="text/javascript" src="../jquery.plugin.html2canvas.js"></script>
<script type="text/javascript" src="../external/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../build/html2canvas.js"></script>
<script type="text/javascript" src="../build/jquery.plugin.html2canvas.js"></script>
<script type="text/javascript">
$(window).ready(function() {
$(window).ready(function() {
$('body').html2canvas();
});
</script>
@ -64,7 +64,7 @@
</div>
<div style="font-family:Verdana;">
<h2>Verdana</h2>
<h2>Verdana</h2>
<ol class="small">
<li style="text-decoration:none;">text-decoration:none;</li>
<li style="text-decoration:underline;">text-decoration:underline;</li>
@ -87,7 +87,7 @@
</div>
<div style="font-family:Tahoma;">
<h2>Tahoma</h2>
<h2>Tahoma</h2>
<ol class="small">
<li style="text-decoration:none;">text-decoration:none;</li>
<li style="text-decoration:underline;">text-decoration:underline;</li>
@ -124,6 +124,6 @@
<li style="text-align:right;width:300px;">text-align:right;width:300px;</li>
<li style="font-variant:small-caps;">font-variant:small-caps; (not yet supported)</li>
</ul>
</body>
</html>