我想將SVG數據轉換爲PNG。爲此,我正在使用canvas和drawimage()
函數。這工作100%罰款,並將我的SVG轉換爲PNG,但如果我的SVG有任何圖像標記,那麼它不會工作。將帶圖像標籤的Svg轉換爲PNG
在我的虛擬代碼一看: -
<html>
<body>
<div id="mainsvg">
<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'>
<rect x='0' y='0' width='50' height='100' fill='red'></rect>
<image x='50' y='0' width='100' height='100' xlink:href='http://www.rashflash.com/assets/images/homepage/bubble2.png'/>
</svg>
</div>
<p>
<canvas id="canvas" style="border:2px solid black;" width="200" height="200"></canvas>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var mainsvg=document.getElementById('mainsvg');
var data =mainsvg.innerHTML;
var DOMURL = self.URL || self.webkitURL || self;
var img = new Image();
var svg = new Blob([data], {type: "image/svg+xml;charset=utf-8"});
var url = DOMURL.createObjectURL(svg);
img.onload = function() {
ctx.drawImage(img, 0, 0);
DOMURL.revokeObjectURL(url);
};
img.src = url;
</script>
</body>
</html>
上面的代碼生成的圖像僅在我的SVG是如下: -
<div id="mainsvg">
<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'>
<rect x='0' y='0' width='50' height='100' fill='red'></rect>
</svg>
</div>
爲什麼不把它轉換SVG的imagetag ?需要幫助的感謝