【发布时间】:2017-11-10 14:31:45
【问题描述】:
在我的页面上,我有一个按钮和一个具有以下属性的画布 -
<button onclick="openSVG()">Open</button>
<canvas></canvas>
点击按钮时,openSVG() 函数应该是
- 让用户从本地硬盘驱动器中选择并打开一个 SVG 文件(之前使用 FabricJS 创建)
- 使用 FileReader 读取 SVG 文件的内容并将其存储在变量中
- 使用该变量使用
fabric.loadSVGFromString()方法填充画布
现在单击“打开”按钮会打开一个文件选择窗口,但不会将对象绘制到画布中。这是我的 openSVG() 函数 -
function openSVG() {
// importing file
var input = $(document.createElement('input'));
input.attr("id", "mySVGFile")
input.attr("type", "file");
input.trigger('click');
//storing file contents in var using FileReader
var myInputFile = document.getElementById('mySVGFile');
var myFile = myInputFile.files[0];
var fr = new FileReader();
fr.onload = function(){
var canvasD = this.result;
//loading data from var to canvas
fabric.loadSVGFromString(canvasD, function(objects, options) {
var obj = fabric.util.groupSVGElements(objects, options);
canvas.add(obj).renderAll();
});
};
fr.readAsText(myFile); }
我是 FileReader 的新手。控制台中没有错误,所以我什至不知道哪里出错了。 我的方法不起作用吗?有没有更好的方法来实现我想要的。
【问题讨论】:
标签: javascript html canvas svg fabricjs