+
96
-

html中svg如何转换成base64?

html中svg如何转换成base64?

网友回复

+
15
-

很简单

var mySvg = document.querySelector(".mysvg");
var svgCon = mySvg.outerHTML;

//svg转base64;
alert(window.btoa(unescape(encodeURIComponent(svgCon))));

完整代码如下:

<!DOCTYPE html>
<html>
<head>
    <title>svgAsHtml</title>
    <meta charset="utf-8">
   <script type="text/javascript" src="http://repo.bfw.wiki/bfwrepo/js/d3.js"></script>
</head>
<body>
    <svg width="300" height="170" class="mysvg" viewbox="0 0 300 170" xmlns="http://www.w3.org/2000/svg" version="1.1">

        <path d="M100 10 L50 60 L150 60 Z" stroke="#FB716D" stroke-width="3" fill="#FBA28A" />
        <rect x="55" y="61" width="90" height="100" fill="#F3C392"></rect>
        <rect x="85" y="97" width="30" height="30" stroke="#f6f6f6" stroke-width="3" fill="#B2DBCB" />
        <line x1="85" x2="115" y1="112" y2="112" stroke="#f6f6f6" stroke-width="3" fill="transparent" />
        <line x1="100" x2="100" y1="97" y2="127" stroke="#f6f6f6" stroke-width="3" fill="transparent" />
        <g transform="translate(150,-18)">
            <path d="M 30 160 A 30 35, 0, 1, 1, 60 160 L 45 160 Z" fill="#7ACB9E"></path>
            <rect x="40" y="160" width="10" height="22" fill="#EBBEA6" />
        </g>

        <g transform="translate(187,-18)">
            <path d="M70 100 L40 160 L100 160 Z" fill="#7ACB9E" />
            <rect x="65" y="160" width="10" height="22" fill="#EBBEA6" />
        </g>

    </svg>
    <script type=...

点击查看剩余70%

我知道答案,我要回答