vtk.js

vtk.js


svg 转 base64

<pre><code class="language-javascript">/** * svg 转base64 * @param {*} svgDom * @param {*} MIMEType * @param {*} option * @returns */ export async function export2Base64Img(svgDom, MIMEType, option) { const serializer = new XMLSerializer(); const source = serializer.serializeToString(svgDom); // const path = 'data:image/svg+xml;base64,' + window.btoa(reEncode(source)); const svg = new Blob([source], { type: 'image/svg+xml;charset=utf-8' }); const url = URL.createObjectURL(svg); // eslint-disable-next-line one-var var canvas = document.createElement('canvas'), context = canvas.getContext('2d', { willReadFrequently: true }), img = new Image(), _exportPath, handler; option = option || {}; canvas.width = parseFloat(svgDom.getAttribute('width')); canvas.height = parseFloat(svgDom.getAttribute('height')); img.src = url; img.onload = function (e) { URL.revokeObjectURL(url); // 增加底色 // if (option.background) { // context.beginPath(); // context.rect(0, 0, canvas.width, canvas.height); // context.fillStyle = option.background; // context.fill(); // context.closePath(); // } // // ctx.drawImage(e.target, 0, 0); context.drawImage(e.target, 0, 0); // // 添加水印 // const marker = option.watermark || ''; // if (marker) { // context.font = '18px 微软雅黑'; // context.fillStyle = 'rgba(12, 0, 70, 0.5)'; // // eslint-disable-next-line one-var // var textWidth = context.measureText(marker).width, // textHegith = 50, // pk = 1.2, // rotate = (option.rotation || -45) * Math.PI / 180, // sinReg = Math.sin(rotate), // cosReg = Math.cos(rotate), // width = Math.abs(canvas.width * cosReg) + Math.abs(canvas.height * sinReg), // height = Math.abs(canvas.height * cosReg) + Math.abs(canvas.width * sinReg); // const xf = Math.ceil(width / textWidth * pk); // const yf = Math.ceil(height / textHegith); // context.rotate(rotate); // for (let i = 0; i &amp;lt; yf; i++) { // for (let k = 0; k &amp;lt; xf; k++) { // context.fillText(marker, textWidth * k * pk - canvas.height * cosReg, textHegith * i); // } // } // } _exportPath = canvas.toDataURL('image/png'); typeof handler === 'function' &amp;amp;&amp;amp; handler(_exportPath); }; return new Promise(function (resolve, reject) { handler = resolve; }); }</code></pre>

页面列表

ITEM_HTML