【发布时间】:2017-12-01 17:51:27
【问题描述】:
我正在使用 D3-Celestial 库 (https://github.com/ofrohn/d3-celestial) 并尝试将画布图像转换为 SVG(我需要将其用作矢量图像,以便将其转换为激光切割机的 GCode)。
我曾尝试使用 Fabric JS 库和 Canvas-GetSVG,但没有成功。
有没有人有一个好的起点或简单的方法来做到这一点?
<div style="overflow:hidden;"><div id="celestial-map"></div></div>
<div id="celestial-form"></div>
<script type="text/javascript">
var canvas;
Celestial.display({
form: true,
location: true,
datapath: "../data/",
stars: {proper: true}
});
document.getElementById("celestial-map").firstChild.setAttribute("id", "findthisstring");
function myFunction(){
var canvas = new fabric.Canvas('findthisstring', { isDrawingMode: true });
canvas.isDrawingMode = false;
canvas.toSVG();
}
function do_save() {
var canvas = new fabric.Canvas('findthisstring', { isDrawingMode: true });
canvas.isDrawingMode = false;
var filedata=canvas.toSVG(); // the SVG file is now in filedata
var locfile = new Blob([filedata], {type: "image/svg+xml;charset=utf-8"});
var locfilesrc = URL.createObjectURL(locfile);//mylocfile);
var dwn = document.getElementById('dwn');
dwn.innerHTML = "<a href=" + locfilesrc + "
download='mysvg.svg'>Download</a>";
}
</script>
<button onclick="myFunction()">Click me</button>
<input type="button" id="saveBtn" value="save" onclick="do_save()"></input>
<div id='dwn'></div>
<div id="imgOut"></div>
我尝试使用几个不同的库将数据从 Canvas 对象导出到 SVG,但只能获得 Empty SVG。
【问题讨论】:
-
为了帮助您,我们需要查看您的代码。请编辑您的问题以包含相关的代码 sn-ps,以及您尝试过的内容的解释以及您的预期输出。
-
我添加了 JS 和相关的 HTML,这是否有助于解释我想要做什么?
标签: javascript d3.js canvas svg