将页面转换为canvas导出为图片

思路

需要将dom对象在canvas重绘,然后调用canvas.toDataurl()将数据转换为base64的data url,可以直接导出图片。

但是Dom对象不能直接在Canvas中绘制,需要先将dom文本转换图片地址,然后在图片加载时将其内容绘制在canvas上。

一种快捷的方式是将dom文本内嵌在svg对象中,再将svg对象转换为xml/svg data url,这个地址可以作为图片对象的地址直接在页面中展示。

如图所示,一个svg文本可以作为url的内容在浏览器中展示一张图片

svg内容如下:

<svg xmlns="http://www.w3.org/2000/svg" height="40" width="40"><foreignObject height="100%" width="100%"><svg width="18" height="18" x="370" y="120" transform=" translate(11, 11) scale(2.23, 2.23) "> <style type="text/css"> .anst0 { fill: #ea4554 } .anst1 { fill-rule: evenodd; clip-rule: evenodd; fill: #333 } .anst2 { fill: rgba(255, 255, 255, 0.33) } </style> <g id="anExclamationMark_1_"> <path id="anTriangle_2_" class="anst0" d="M1 14.4L8.1 1.6c.4-.8 1.3-.8 1.7 0L17 14.4c.4.7-.1 1.6-1 1.6H2c-.8 0-1.4-.9-1-1.6z"></path> <path id="anSquare_2_" class="anst1" d="M8 12h2v2H8z"></path> <path id="anLine_1_" class="anst1" d="M8 5h2v6H8z"></path> <path id="anBottomDot_1_" class="anst2" d="M8 12h2v2H8z"></path> <path id="anTopLine_1_" class="anst2" d="M8 5h2v6H8z"></path> </g> </svg></foreignObject></svg>

需要注意的是,svg对象下可以直接放置svg对象,也可以放非svg对象,如div p h1等等,但是需要作为foreignObject的子集,否者不会展示内容

在dom文本转换为xml/svg url过程中需要对一些特殊字符做转义,否者地址失效

 

svg xml url的第一个svg必须要有命名空间,否者以xml形式显示,当然就不能作为图片地址了

 

 

该xml/svg data url可以作为img对象的地址,在页面内展示图片,如图所示

下载这张矢量图片

只需添加一个超链接,指定为图片地址,点击超链接,即可下载该图片

需要注意的是,xml/svg data url不能作为下载地址,需要将其转换为base64地址

一种简单的方法是将xml/svg data url 作为地址的img对象绘制在canvas上,然后调用canvas.toDataurl(),即可获得base64 dataurl

data:image/png;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jDoLz1KDJVzyYiyowSpGnYVnqcESLnkxFtRgFaPOwrPUYAmXvBgLarCKUWfhWWqwhEtejAU1WMWos/AsNVjCJS/GghqsYtRZeJb/B5eRSOJ64Qf6AAAAAElFTkSuQmCC

点击超链接下载为Png图像,自带透明背景

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
html2canvas是一种JavaScript库,可将HTML元素转换Canvas元素并导出为图像。在使用html2canvas之前,需要先引入该库。 下面是一个使用html2canvas导出图片的示例: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>html2canvas demo</title> <script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script> </head> <body> <div id="content"> <h1>Hello, world!</h1> <p>This is an example of using html2canvas to export an image.</p> <img src="https://via.placeholder.com/150" alt="Placeholder image"> </div> <button id="export-btn">Export as image</button> <script> // 获取要导出的元素 var content = document.getElementById("content"); // 获取导出按钮 var btn = document.getElementById("export-btn"); // 点击导出按钮时执行以下代码 btn.onclick = function() { // 使用html2canvas将元素转换Canvas html2canvas(content).then(function(canvas) { // 创建一个新的img元素 var img = new Image(); // 将Canvas的内容赋值给img img.src = canvas.toDataURL(); // 将img添加到页面上 document.body.appendChild(img); }); }; </script> </body> </html> ``` 在上面的示例中,我们首先获取要导出的元素和导出按钮。然后,当点击导出按钮时,使用html2canvas将元素转换Canvas,并将Canvas转换为图像,将图像添加到页面上。 需要注意的是,由于html2canvas使用Canvas元素来渲染HTML元素,因此可能会遇到一些兼容性问题,尤其是对于一些复杂的布局或样式。在使用html2canvas时,应该注意测试和调试,以确保生成的图片符合预期。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值