html2canvas.js 要改2处代码

第1处代码 line 603

window.html2canvas=function(nodeList,options){varindex=html2canvasCloneIndex++;options=options||{};if(options.logging){window.html2canvas.logging=true;window.html2canvas.start=Date.now();}options.async=typeof(options.async)==="undefined"?true:options.async;options.allowTaint=typeof(options.allowTaint)==="undefined"?false:options.allowTaint;options.removeContainer=typeof(options.removeContainer)==="undefined"?true:options.removeContainer;options.javascriptEnabled=typeof(options.javascriptEnabled)==="undefined"?false:options.javascriptEnabled;options.p_w_picpathTimeout=typeof(options.p_w_picpathTimeout)==="undefined"?10000:options.p_w_picpathTimeout;options.renderer=typeof(options.renderer)==="function"?options.renderer:CanvasRenderer;options.strict=!!options.strict;if(typeof(nodeList)==="string"){if(typeof(options.proxy)!=="string"){returnPromise.reject("Proxymustbeusedwhenrenderingurl");}varwidth=options.width!=null?options.width:window.innerWidth;varheight=options.height!=null?options.height:window.innerHeight;returnloadUrlDocument(absoluteUrl(nodeList),options.proxy,document,width,height,options).then(function(container){returnrenderWindow(container.contentWindow.document.documentElement,container,options,width,height);});}varnode=((nodeList===undefined)?[document.documentElement]:((nodeList.length)?nodeList:[nodeList]))[0];node.setAttribute(html2canvasNodeAttribute+index,index);//returnrenderDocument(node.ownerDocument,options,node.ownerDocument.defaultView.innerWidth,node.ownerDocument.defaultView.innerHeight,index).then(function(canvas){varwidth=options.width!=null?options.width:node.ownerDocument.defaultView.innerWidth;varheight=options.height!=null?options.height:node.ownerDocument.defaultView.innerHeight;returnrenderDocument(node.ownerDocument,options,width,height,index).then(function(canvas){if(typeof(options.onrendered)==="function"){log("options.onrenderedisdeprecated,html2canvasreturnsaPromisecontainingthecanvas");options.onrendered(canvas);}returncanvas;});};

第2处 line 633

functionrenderWindow(node,container,options,windowWidth,windowHeight){varclonedWindow=container.contentWindow;varsupport=newSupport(clonedWindow.document);varp_w_picpathLoader=newImageLoader(options,support);varbounds=getBounds(node);varwidth=options.type==="view"?windowWidth:documentWidth(clonedWindow.document);varheight=options.type==="view"?windowHeight:documentHeight(clonedWindow.document);varrenderer=newoptions.renderer(width,height,p_w_picpathLoader,options,document);varparser=newNodeParser(node,renderer,support,p_w_picpathLoader,options);returnparser.ready.then(function(){log("Finishedrendering");varcanvas;if(options.type==="view"){canvas=crop(renderer.canvas,{width:renderer.canvas.width,height:renderer.canvas.height,top:0,left:0,x:0,y:0});}elseif(node===clonedWindow.document.body||node===clonedWindow.document.documentElement){//xiaominmodifycanvas=renderer.canvas;}elseif(options.scale){//xiaominzhmodifyvarscale=options.scale||1;canvas=crop(renderer.canvas,{width:bounds.width*scale,height:bounds.height*scale,top:bounds.top*scale,left:bounds.left*scale,x:0,y:0});}else{canvas=crop(renderer.canvas,{width:options.width!=null?options.width:bounds.width,height:options.height!=null?options.height:bounds.height,top:bounds.top,left:bounds.left,x:clonedWindow.pageXOffset,y:clonedWindow.pageYOffset});}cleanupContainer(container,options);returncanvas;});}


如何使用

varshareContent=$("#share-person-report")[0];//需要截图的包裹的DOM元素varwidth=shareContent.offsetWidth;//获取dom宽度varheight=shareContent.offsetHeight;//获取dom高度varcanvas=document.createElement("canvas");//创建一个canvas节点varscale=2;//定义任意放大倍数支持小数canvas.width=width*scale;//定义canvas宽度*缩放canvas.height=height*scale;//定义canvas高度*缩放canvas.getContext("2d").scale(scale,scale);//获取context,设置scalehtml2canvas($('#share-person-report'),{allowTaint:true,taintTest:false,useCORS:true,background:'#fff',scale:scale,width:width,height:height,canvas:canvas,onrendered:function(cav){varsrc=cav.toDataURL();varhtml="<imgsrc='"+src+"'class='canvas'/>";$('#share-person-report').html(html);}});

附件:http://down.51cto.com/data/2366743