0
對於大小問題,我刪除了一些svg內容。我想實現導出爲PNG或任何圖像格式報告目的如何將SVG導出爲PNG格式
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
function SaveasImage(){
var myCanvas = document.getElementById('myCanvas');
// get 2D context
var myCanvasContext = myCanvas.getContext('2d');
// Load up our image.
var source = new Image();
var img = document.getElementById('svg1');
source.src = img.svg;
myCanvasContext.drawImage(source,0,0,200,200);
$("#resultImage").attr("src",myCanvas.toDataURL("image/png"));
}
});
</script>
</head>
<body>
<h1>My first SVG</h1>
<canvas id="myCanvas" width="200" height="100"></canvas>
<svg version="1.1" id="svg1" style="font-family:"Lucida Grande",
"Lucida Sans Unicode", Arial, Helvetica, sans-serif;font-size:12px;" xmlns="http://www.w3.org/2000/svg" width="322" height="194"><text x="49" style="color:#606060;cursor:default;font-size:11px;fill:#606060;width:96px;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="76" opacity="1">50</text><text x="49" style="color:#606060;cursor:default;font-size:11px;fill:#606060;width:96px;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="13" opacity="1">100</text></g></svg>
<input type="button" onclick="SaveasImage()" value="exportasimage" />
</body>
</html>