简体   繁体   English

如何重新创建Fabric.js画布?

[英]How to recreate a Fabric.js canvas?

I have a site where users can move images around in a Fabric.js canvas. 我有一个站点,用户可以在其中移动Fabric.js画布中的图像。 I want to be able to recreate their "design" in a bigger canvas, so my question is; 我希望能够在更大的画布上重新创建他们的“设计”,所以我的问题是; what different ways are there to recreate a canvas using the variables from the original? 有什么不同的方法可以使用原始变量来重新创建画布?

Right now im using JSON.stringify(canvas); 现在我正在使用JSON.stringify(canvas); to get back the data, but its not really ideal since i then have to copy paste the variables again, it looks like this: type":"image","originX":"center","originY":"center","left":135,"top":259," . 取回数据,但是它不是很理想,因为我不得不再次复制粘贴变量,它看起来像这样: type":"image","originX":"center","originY":"center","left":135,"top":259," Any suggestions on how to do this or is JSON.stringify(canvas); 有关如何执行此操作的任何建议,或者为JSON.stringify(canvas); and then copy pasting all the data the simplest? 然后复制粘贴所有数据最简单?

Use the canvas.toJSON() method to create a json object of your first canvas's objects and properties, then load that json object into your new canvas using the 使用canvas.toJSON()方法创建第一个画布的对象和属性的json对象,然后使用将该对象加载到新画布中

canvas.loadFromJSON("your json object"); method. 方法。

Once you have loaded the object into your canvas with the above method, call canvas.renderAll(); 使用上述方法将对象加载到画布后,请调用canvas.renderAll(); and you should see the new canvas contains all the objects from the original. 您应该会看到新画布包含原始画布中的所有对象。

You may optionally choose which properties to store in your json object by passing an array of property names to the toJSON() method, documentation can be found here : 您可以选择通过将属性名称数组传递给toJSON()方法来选择要存储在json对象中的属性,可以在这里找到文档:

http://fabricjs.com/docs/fabric.Canvas.html#toJSON http://fabricjs.com/docs/fabric.Canvas.html#toJSON

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM