本文介绍了在Fabric.js中重置canvas并重新加载JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立类似kitchensink示例,除非我需要能够有多个视图。

I'm building something like the kitchensink example except I need to be able to have multiple views.http://fabricjs.com/kitchensink/

名片:
正面和背面

Business Card:front and back

在编辑时,我定期保存json。

As you edit I save the json periodically.

当你想编辑后面而不重新加载页面(因为我使用AngularJS),我如何擦拭当前画布并重新加载新的JSON?

When you want to edit the back without reloading the page (because I'm using AngularJS), how do I wipe the current canvas and reload new JSON?

我正在加载JSON,你点击更改视图,我运行 canvas.clearContext(),并重新加载新的JSON。这不是我的预期的方式。

I'm currently loading the JSON, you click change view, I run canvas.clearContext(), and reload the new JSON. This is not working the way I anticipated.

我使用指令管理此画布。我正在做指令实例化当json可用,当我试图更新它,无论什么原因,不会工作。

I'm using a directive to manage this canvas. I was making the directive instantiate when json was available and when I tried to update it, for whatever reason, would not work.

我删除了,并使指令启动本身为空,现在我只是loadjson通过服务。 Yay!

I removed that and made the directive initiate itself empty and now I just loadJson via service. Yay!

推荐答案

通常 canvas.loadFromJSON()清除整个画布之前加载新对象。否则可以运行 canvas.clear();

Normally canvas.loadFromJSON() clears the whole canvas before loading new objects. Otherwise you can run canvas.clear();.

如何您是否加载您的JSON?

How do you load your JSON?

这样应该可以工作:

var json = canvas.toJSON();

canvas.clear();

canvas.loadFromJSON(json, canvas.renderAll.bind(canvas));

加载JSON后,调用 canvas.renderAll c $ c>。在 canvas.loadFromJSON(json,callback)的第二个参数中,您可以定义一个cllback函数,在所有对象加载/添加后调用。

After loading JSON it's important to call canvas.renderAll(). In the 2nd parameter of canvas.loadFromJSON(json, callback) you can define a cllback function which is invoked after all objects are loaded/added.

这篇关于在Fabric.js中重置canvas并重新加载JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 16:20