html2canvas 实现纯JS网页截图简单例子
代码库地址: https://github.com/niklasvh/html2canvas
自己修改其中的 test.js (主要是其中的一些库的路径) 把test.js external文件夹 src文件夹 放在同一目录下
1 var h2cSelector, h2cOptions; 2 (function(document, window) { 3 var scrStart = '<script type="text/javascript" src="', scrEnd = '"></script>'; 4 document.write(scrStart + 'external/jquery-1.6.2.js' + scrEnd); 5 var html2canvas = ['Core', 'Generate', 'Parse', 'Preload', 'Queue', 'Renderer', 'Util', 'renderers/Canvas', 'plugins/jquery.plugin.html2canvas'], i; 6 for (i = 0; i < html2canvas.length; ++i) { 7 document.write(scrStart + 'src/' + html2canvas[i] + '.js' + scrEnd); 8 } 9 window.onload = function() { 10 h2cSelector = [document.body]; 11 12 if (window.setUp) { 13 window.setUp(); 14 } 15 16 17 }; 18 }(document, window)); 19 20 function screenShot() 21 { 22 setTimeout(function() { 23 $(h2cSelector).html2canvas($.extend({ 24 flashcanvas: "external/flashcanvas.min.js", 25 logging: true, 26 profile: true, 27 useCORS: true, 28 onrendered:function(canvas ) 29 { 30 var screenshot; 31 screenshot = canvas.toDataURL( "image/png" ); 32 console.info(screenshot); 33 } 34 }, h2cOptions)); 35 }, 100); 36 }
在主页中包含 test.js 然后调用 screenshot 函数就可以在控制台看到图片了,该数据可以上传至服务器。