Saving JSON in canvas with fabric.js

孤人 提交于 2019-12-09 23:46:08

问题


I am trying to import multiple svg file and after modification saving the json to database. I have tried to use

    canvas.item(0).sourcePath = '/URL/FILE.svg';
    JSON.stringify(canvas.toDatalessJSON());

but this won't work for multiple files, it works for the first file only. i am trying to specify every single file url but can't understand how this can be done. any solution please?

thanks in advance


回答1:


canvas.item(0) - gets the first object. canvas.item(1) - gets the second object and so on....

If you don't know the total amount of objects you can use the canvas.forEachObject option.

     canvas.forEachObject(function(obj){
        obj.sourcePath = '/URL/FILE.svg';
        console.log(obj.sourcePath)
     });

     JSON.stringify(canvas.toDatalessJSON());

You could also set obj.sourcePath when you load the svg like this.

    fabric.loadSVGFromString(svg, function(objects, options) {
      var obj = fabric.util.groupSVGElements(objects, options);
      obj.sourcePath = '/URL/FILE.svg';
      canvas.add(obj);
      canvas.renderAll();
    });

Hope that helps!



来源:https://stackoverflow.com/questions/15763057/saving-json-in-canvas-with-fabric-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!