html5保存png,html - Save <svg> HTML5 to PNG or image - Stack Overflow

Server-side script method

Here is a simple method to save svg elements to png(though it uses server-side scripting, which may be different from what you'd expect):

Checkout this page.

As is documented by the author, the client extract the svg element (using XMLSerializer.serializeToString)and send it to server; the server convert it to png and send it back to the client. The server can use any program that is convenient(rsvg-convert in this case).

Canvg library

You can use this library to do this on client side (check it out!):

canvg is a SVG parser and renderer. It takes a URL to a SVG file or the text of an SVG file, parses it in JavaScript, and renders the result on a Canvas element

Use it like this:

//load a svg snippet in the canvas with id = 'drawingArea'

canvg(document.getElementById('drawingArea'), '... ')

Then you can use the toDataURL method:

document.getElementById('drawingArea').toDataURL('image/png');

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值