html2canvas/html2canvas.js

857 lines
23 KiB
JavaScript
Raw Normal View History

2011-07-16 05:13:38 +04:00
/*
2011-07-16 20:59:15 +04:00
* html2canvas v0.12 <http://html2canvas.hertzen.com>
2011-07-16 05:13:38 +04:00
*
* Copyright 2011, Niklas von Hertzen <http://hertzen.com>
* http://www.twitter.com/niklasvh
*
2011-07-16 20:59:15 +04:00
* The MIT License
* Copyright (c) 2011 Niklas von Hertzen. All rights reserved.
* 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.
2011-07-16 05:13:38 +04:00
*/
function html2canvas(el,userOptions) {
var options = userOptions || {};
2011-07-16 20:59:15 +04:00
this.opts = {
2011-07-16 05:13:38 +04:00
logging:false,
ready: function(canvas){
document.body.appendChild(canvas);
},
renderViewport: true
};
2011-07-16 20:59:15 +04:00
this.opts = this.extendObj(options,this.opts);
this.element = el;
2011-07-16 05:13:38 +04:00
var imageLoaded,
canvas,
ctx,
bgx,
bgy,
2011-07-16 20:59:15 +04:00
image;
this.imagesLoaded = 0;
this.images = [];
this.ignoreElements = "IFRAME|OBJECT|PARAM";
2011-07-16 05:13:38 +04:00
// test how to measure text bounding boxes
2011-07-16 20:59:15 +04:00
this.useRangeBounds = false;
2011-07-16 05:13:38 +04:00
if (document.createRange){
var r = document.createRange();
2011-07-16 20:59:15 +04:00
this.useRangeBounds = new Boolean(r.getBoundingClientRect);
2011-07-16 05:13:38 +04:00
}
2011-07-16 20:59:15 +04:00
// Start script
this.init();
}
html2canvas.prototype.init = function(){
var _ = this;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
canvas = document.createElement('canvas');
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
// TODO remove jQuery dependency
canvas.width = $(document).width();
canvas.height = $(document).height()+10;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
if (!canvas.getContext){
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
// TODO include Flashcanvas
/*
2011-07-16 05:13:38 +04:00
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');
}
*/
2011-07-16 20:59:15 +04:00
}else{
ctx = canvas.getContext('2d');
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
if (!ctx){
// canvas not initialized, let's kill it here
this.log('Canvas not available');
return;
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
// set common settings for canvas
ctx.textBaseline = "bottom";
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
this.log('Finding background images');
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
this.getImages(this.element);
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
this.log('Finding images');
this.each(document.images,function(i,e){
_.preloadImage(_.getAttr(e,'src'));
});
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
if (this.images.length == 0){
this.start();
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
/*
* Check whether all assets have been loaded and start traversing the DOM
*/
html2canvas.prototype.start = function(){
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
if (this.images.length == 0 || this.imagesLoaded==this.images.length/2){
this.log('Started parsing');
this.newElement(this.element);
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
this.parseElement(this.element);
2011-07-16 05:13:38 +04:00
}
2011-07-16 20:59:15 +04:00
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
/*
* Finished rendering, send callback
*/
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
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;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
}
this.opts.ready(canvas);
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
html2canvas.prototype.drawBackground = function(el,bounds){
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
var background_image = this.getCSS(el,"background-image");
var background_repeat = this.getCSS(el,"background-repeat");
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
if (typeof background_image != "undefined" && /^(1|none)$/.test(background_image)==false){
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
background_image = this.backgroundImageUrl(background_image);
var image = this.loadImage(background_image);
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
var bgp = this.getBackgroundPosition(el,bounds,image);
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
if (image){
switch(background_repeat){
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
case "repeat-x":
this.drawbackgroundRepeatX(image,bgp,bounds.left,bounds.top,bounds.width,bounds.height);
break;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
case "repeat-y":
this.drawbackgroundRepeatY(image,bgp,bounds.left,bounds.top,bounds.width,bounds.height);
break;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
case "no-repeat":
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
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;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
default:
var height,
add;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
bgp.top = bgp.top-Math.ceil(bgp.top/image.height)*image.height;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
for(bgy=(bounds.top+bgp.top);bgy<=bounds.height+bounds.top;){
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
var h = Math.min(image.height,(bounds.height+bounds.top)-bgy);
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
if ( Math.floor(bgy+image.height)>h+bgy){
height = (h+bgy)-bgy;
}else{
height = image.height;
}
// console.log(height);
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
if (bgy<bounds.top){
add = bounds.top-bgy;
bgy = bounds.top;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
}else{
add = 0;
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
this.drawbackgroundRepeatX(image,bgp,bounds.left,bgy,bounds.width,height);
if (add>0){
bgp.top += add;
2011-07-16 05:13:38 +04:00
}
2011-07-16 20:59:15 +04:00
bgy = Math.floor(bgy+image.height)-add;
}
break;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
}
}else{
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
log("Error loading background:" + background_image);
//console.log(images);
2011-07-16 05:13:38 +04:00
}
2011-07-16 20:59:15 +04:00
2011-07-16 05:13:38 +04:00
}
2011-07-16 20:59:15 +04:00
}
/*
* 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;
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
/*
* 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);
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
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;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
bgp.top = bgp.top-Math.ceil(bgp.top/image.height)*image.height;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
for(bgy=(y+bgp.top);bgy<=h+y;){
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
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);
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
bgy = Math.floor(bgy+image.height);
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
}
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
html2canvas.prototype.drawbackgroundRepeatX = function(image,bgp,x,y,w,h){
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
var height = Math.min(image.height,h),
width,bgx;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
bgp.left = bgp.left-Math.ceil(bgp.left/image.width)*image.width;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
for(bgx=(x+bgp.left);bgx<=w+x;){
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
if (Math.floor(bgx+image.width)>w+x){
width = (w+x)-bgx;
}else{
width = image.width;
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
this.drawBackgroundRepeat(image,bgx,(y+bgp.top),width,height,x,y);
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
bgx = Math.floor(bgx+image.width);
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
}
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
html2canvas.prototype.drawBackgroundRepeat = function(image,x,y,width,height,elx,ely){
var sourceX = 0,
sourceY=0;
if (elx-x>0){
sourceX = elx-x;
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
if (ely-y>0){
sourceY = ely-y;
2011-07-16 05:13:38 +04:00
}
2011-07-16 20:59:15 +04:00
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
*/
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
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;
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
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");
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
2011-07-16 05:13:38 +04:00
/*
2011-07-16 20:59:15 +04:00
* 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);
}
});
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
// 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){
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'));
2011-07-16 05:13:38 +04:00
}
}
2011-07-16 20:59:15 +04:00
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
}
/*
* Function to draw the text on the canvas
*/
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
html2canvas.prototype.printText = function(currentText,x,y){
if (this.trim(currentText).length>0){
ctx.fillText(currentText,x,y);
}
}
// Drawing a rectangle
html2canvas.prototype.newRect = function(x,y,w,h,bgcolor){
if (bgcolor!="transparent"){
ctx.fillStyle = bgcolor;
ctx.fillRect (x, y, w, h);
2011-07-16 05:13:38 +04:00
}
2011-07-16 20:59:15 +04:00
}
/*
* 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);
}
2011-07-16 05:13:38 +04:00
}
2011-07-16 20:59:15 +04:00
}
/*
* Load image from storage
*/
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
html2canvas.prototype.loadImage = function(src){
var imgIndex = this.images.indexOf(src);
if (imgIndex!=-1){
return this.images[imgIndex+1];
}else{
return false;
2011-07-16 05:13:38 +04:00
}
2011-07-16 20:59:15 +04:00
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
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){
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
var family = this.getCSS(el,"font-family");
var size = this.getCSS(el,"font-size");
var color = this.getCSS(el,"color");
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
var bold = this.getCSS(el,"font-weight");
var font_style = this.getCSS(el,"font-style");
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
var text_decoration = this.getCSS(el,"text-decoration");
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
// apply text-transform:ation to the text
textNode.nodeValue = this.textTransform(textNode.nodeValue,this.getCSS(el,"text-transform"));
var text = textNode.nodeValue;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
//text = $.trim(text);
if (text.length>0){
switch(bold){
case "401":
bold = "bold";
break;
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
ctx.font = bold+" "+font_style+" "+size+" "+family;
ctx.fillStyle = color;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
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();
2011-07-16 05:13:38 +04:00
}else{
2011-07-16 20:59:15 +04:00
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);
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
var bounds = getBounds(wrapElement);
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
parent.replaceChild(backupText,wrapElement);
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
this.printText(oldTextNode.nodeValue,bounds.left,bounds.bottom);
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
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;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
oldTextNode = newTextNode;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
}
2011-07-16 05:13:38 +04:00
}
2011-07-16 20:59:15 +04:00
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
/*
2011-07-16 05:13:38 +04:00
* Function to apply text-transform attribute to text
*/
2011-07-16 20:59:15 +04:00
html2canvas.prototype.textTransform = function(text,transform){
switch(transform){
case "lowercase":
return text.toLowerCase();
break;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
case "capitalize":
return text.replace( /(^|\s|:|-|\(|\))([a-z])/g , function(m,p1,p2){
return p1+p2.toUpperCase();
} );
break;
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
case "uppercase":
return text.toUpperCase();
break;
default:
return text;
2011-07-16 05:13:38 +04:00
}
2011-07-16 20:59:15 +04:00
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
/*
*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)){
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
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){
if (typeof console != "undefined" && console.log){
console.log(a);
}else{
alert(a);
2011-07-16 05:13:38 +04:00
}
}
2011-07-16 20:59:15 +04:00
}
/*
* Function to provide bounds for element
*/
html2canvas.prototype.getBounds = function(el){
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
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(getCSS(el,"border-left-width"),10),
top: p.top + parseInt(getCSS(el,"border-top-width"),10),
width:$(el).innerWidth(),
height:$(el).innerHeight()
2011-07-16 05:13:38 +04:00
}
2011-07-16 20:59:15 +04:00
}
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
/*
* 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]);
2011-07-16 05:13:38 +04:00
}
2011-07-16 20:59:15 +04:00
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
/*
* 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];
2011-07-16 05:13:38 +04:00
}
2011-07-16 20:59:15 +04:00
return defaults;
}
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
2011-07-16 05:13:38 +04:00
2011-07-16 20:59:15 +04:00
/*
* 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);
2011-07-16 05:13:38 +04:00
}