怎么导出画布到JSON?
运用Konva将舞台保存为JSON字符串,能够运用toJSON()
办法将Konva节点树序列化为可保存的文本
在网络存储或离线数据库中。咱们也能够序列化其他节点,
包含层、组和形状。
toJSON()办法不能保存过滤器、图画和事情监听器。所以它适用于十分小的应用程序。关于更杂乱的状况,请阅览最佳实践
###将整个舞台转化为json和转化为目标
转化都是依照“”树“”结构的
letstageJson=stage.toJSON()\n\n//{"attrs":{"width":1200,"height":800},"className":"Stage","children":[{"attrs":{},"className":"Layer","children":[{"attrs":{"name":"rect","x":37,"y":34,"width":41,"height":35,"fill":"red","strokeWidth":0,"opacity":0.5,"draggable":true},"className":"Rect"}]}]}\n\nletstageObj=JSON.parse(stageJson)\n\n/**\n\nattrs:\n\nheight:800\n\nwidth:1200\n\n[[Prototype]]:Object\n\nchildren:Array(1)\n\n0:\n\nattrs:{}\n\nchildren:Array(1)\n\n0:\n\nattrs:{name:'rect',x:37,y:34,width:41,height:35,…}\n\nclassName:"Rect"\n\n[[Prototype]]:Object\n\nlength:1\n\n[[Prototype]]:Array(0)\n\nclassName:"Layer"\n\n[[Prototype]]:Object\n\nlength:1\n\n[[Prototype]]:Array(0)\n\nclassName:"Stage"\n\n*/
实时预览
「链接」