js使用html2canvas实现屏幕截取的示例代码
作者:Leo-hzj
这篇文章主要介绍了js使用html2canvas实现屏幕截取的示例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
整理文档,搜刮出一个js使用html2canvas实现屏幕截取的示例代码,稍微整理精简一下做下分享。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <title>Test</title> <style type="text/css"> *{ margin:0 auto; padding:0; } .btn{ width:100px; height:30px; line-height:30px; background:green; color:#fff; border-radius:10px; -webkit-border-radius:10px; text-align:center; display:block; text-decoration:none; } .container{ background:#e5e5e5; } </style> <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script src="https://cdn.bootcss.com/html2canvas/0.4.1/html2canvas.js"></script> <script> $(function(){ $("#btn").click(function(){ html2canvas($("#container"), { onrendered: function(canvas) { //把截取到的图片替换到a标签的路径下载 $("#download").attr('href',canvas.toDataURL()); //下载下来的图片名字 $("#download").attr('download','share.png') ; document.body.appendChild(canvas); } //可以带上宽高截取你所需要的部分内容 // , // width: 300, // height: 300 }); }); }); </script> </head> <body> <div style="padding:10px 0"> <div class="btn" id="btn">截取屏幕</div> <p style="color:red; text-align:center;">先点击截取屏幕后再点击截图下载</p> <div style="margin-top:10px"> <a href="javascript:;" rel="external nofollow" class="btn" id="download">截图下载</a> </div> </div> <div class="container" id="container"> <p style="text-align:center">以下是测试内容</p> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAAZ2ElEQVR4Xu2d4Xoquw5DT9//oXs/6N20wCTjJTTO0Or8PU5sy7LiBMr++O+//z7/e5P/Pj95qB8fH5vZjfai9gp0Ix/KXs41FF8lD5cPuo+C0yw/yh/Ff8eaS3fwruqIbMOHUnTa0NRegUJpHMUPXUPxVfJw+aD7UCwu9hEABbUD1yhFpw1N7ZV0lcZR/NA1FF8lD5cPug/FIgKgIHbwGqXotKGpvZKy0jiKH7qG4qvk4fJB96F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sowAPCDtBISeqErRaRNGABSU62uch1fdq27p5HsmgKKY6OXauF8Zvx9ABYuKTyYAZ+U9e0UAik2rKDttKKWktAkzASgo19coPKnv7reMAEQAbghQwaLikwnA38Cv7hgBiABEAF7toh/rMwFsXFN/w6cAM46sLDo9tZU8nKeEK17nVWb4eCX8stBKrJwT2QgTJb9f8T0ApXGMB8twK1dDdY3nrngjAK8/CM9qHgEA3ZsJoA5WBOA4rDIBbGBLRxaFoBGA40itjOdKDbf8KHWlfJshR/OIAEQA6p24YUkJ5GwQGniuALkClDhDFZmqrnKPKgVeNFLipacqxdB5qtFYL/YuTJwC59yLYjLDg8alcOHXPwK6CtJxqhV15a3NOiYZ1yOZ0pwuget6+I0APLBFUVFn0d+6uwvBRwAKIP3fpAOrCEAEoM5Ig2UHqTMB1AsVAYgA1NlisIwA1EHswCoCEAGoM9Jg2UHqTAD1QkUAIgB1thgsIwB1EDuwwgJQD1+3pIkrnpw+6F7UfvYiTHNXXrZX+lAeWCm+9OO2Cx40LhoTxVzlSASgOAHMCkKLS+3V4m7FHAF4RiUCsMEUqnCKYo3WKA1C/Tt90L2ofQSAVZfiGwGIANwQUITPRbh3O53pY5siZCvrMZMdGhflCJO8L2tFyHIFyBXghoBCoKOvGbTRZo2gfMlLEbmtNREAIGkrwVpJuEwAzyRZWY8/PQGAfm0zVcjgCq7jbwFcJ7BKXJd/pU7UtyKWNC4ak4tr//ZxTSxTrEY/CeZOxrEfLaDD5789IgB1NJU60WaLAHjqMXwDqG/fZ6kQyxVdBKCOpFKnCED9+mPFKhNAjdgRgBpOF6sIQB0r5boWAfDgi3aJANThigDUsYoAAKwUYoHtp6YRgDqSSp2spxr8J9lGmdGY6gjVLPMI+ICTQqwa1PtWEYB9jP5ZKHWizZZHQE89/geNN9jBGDVuegAAAABJRU5ErkJggg=="> </div> </body> </html>
由于使用最新版本的html2canvas截取屏幕有问题,所以上面使用的是v0.4.1版本进行测试。
注意事项:
1.页面加载完之后再调用html2canvas。
2.截取背景图片没有效果(就算把背景图片转为base64也没有效果)。
3.<img>标签的图片需要转为base64,不然截取没有效果(网上搜索了一下,好像都说是跨域问题)。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。