怎么使用html2canvas将HTML内容写入Canvas生成图片

发布时间:2021-11-12 10:48:29 作者:小新
来源:亿速云 阅读:280

小编给大家分享一下怎么使用html2canvas将HTML内容写入Canvas生成图片,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

html2canvas.js 能够实现在用户浏览器端直接对整个或部分页面进行截屏。

html2canvas.js可以将当页面渲染成一个Canvas图片,通过读取DOM并将不同的样式应用到这些元素上实现。

它不需要来自服务器任何渲染,整张图片都是在客户端浏览器创建。当

浏览器不支持Canvas时,将采用Flashcanvas或ExplorerCanvas技术代替实现。

以下浏览器能够很好的支持该脚本:Firefox 3.5+, Google Chrome, Opera新的版本, IE9以上的浏览器。

基本语法

html2canvas(element, options);html2canvas(document.body, { onrendered: function(canvas) {  var url = canvas.toDataURL();//图片地址  document.body.appendChild(canvas); }, width: 300, height: 300

或者使用ES6的promise

//两个参数:所需要截图的元素id,截图后要执行的函数, canvas为截图后返回的最后一个canvas html2canvas(document.getElementById('id')).then(function(canvas) {document.body.appendChild(canvas);});

html2canvas基本参数说明

allowTaintbooleanfalseWhether to allow cross-origin images to taint the canvas---允许跨域backgroundstring#fffCanvas background color, if none is specified in DOM. Set undefined for transparent---canvas的背景颜色,如果没有设定默认透明heightnumbernullDefine the heigt of the canvas in pixels. If null, renders with full height of the window.---canvas高度设定letterRenderingbooleanfalseWhether to render each letter seperately. Necessary if letter-spacing is used.---在设置了字间距的时候有用loggingbooleanfalseWhether to log events in the console.---在console.log()中输出信息proxystringundefinedUrl to the proxy which is to be used for loading cross-origin images. If left empty, cross-origin images won't be loaded.---代理地址taintTestbooleantrueWhether to test each image if it taints the canvas before drawing them---是否在渲染前测试图片timeoutnumber0Timeout for loading images, in milliseconds. Setting it to 0 will result in no timeout.---图片加载延迟,默认延迟为0,单位毫秒widthnumbernullDefine the width of the canvas in pixels. If null, renders with full width of the window.---canvas宽度useCORSbooleanfalseWhether to attempt to load cross-origin images as CORS served, before reverting back to proxy--这个我也不知道是干嘛的

例子

<!DOCTYPE html><html><head>  <meta charset="utf-8">  <meta http-equiv="X-UA-Compatible" content="IE=edge">  <title>html2canvas example</title>  <script type="text/javascript" src="html2canvas.js"></script></head><script type="text/javascript">function takeScreenshot() { console.log('test');  html2canvas(document.getElementById('view'), {    onrendered: function(canvas) {      document.body.appendChild(canvas);    },   // width: 300,   // height: 300  });}</script><body>  <p id="view" >    <input type="button" value="截图" onclick="takeScreenshot()">  </p></body></html>

以上是“怎么使用html2canvas将HTML内容写入Canvas生成图片”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. html2canvas 简单使用
  2. html2canvas 生成高清图片

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

html2canvas html canvas

上一篇:如何使用html2canvas将网页保存为图片

下一篇:Django中的unittest应用是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》