其他分享
首页 > 其他分享> > svg转图片base64

svg转图片base64

作者:互联网

 1  const svg = document.getElementById('svg'); //获取元素
 2 // const s = new XMLSerializer().serializeToString(svg); //xml序列化
 3 const src = `data:image/svg+xml;base64,${window.btoa(unescape(encodeURIComponent(svg)))}`; // base64转化
 4 const img = new Image(); // 图片容器承载过渡
 5 img.src = src;
 6 img.onload = () => {
 7 // 图片创建后再执行,转Base64过程
 8 const canvas = document.createElement('canvas');
 9 canvas.width = img.width;
10 canvas.height = img.height;
11 const context = canvas.getContext('2d');
12 context.drawImage(img, 0, 0);
13 const ImgBase64 = canvas.toDataURL('image/png');
14 console.log(ImgBase64, 'Svg 转 png');
15 }

 

标签:src,canvas,const,img,svg,base64,图片
来源: https://www.cnblogs.com/dekui/p/16636631.html