FabricJS - 从JSON加载画布时出错 [英] FabricJS - Error when loading Canvas from JSON

查看:506
本文介绍了FabricJS - 从JSON加载画布时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的自定义对象从 fabric.Image 扩展到我的画布,如此答案。当我从服务器检索json字符串时,我试图将其加载到画布时出现奇怪的错误:

I'm using a custom object extended from fabric.Image in my canvas, as suggested in this answer. When I retrieve the json string from the server, I get odd errors when attempting to load it to the canvas:

var canvas = new fabric.Canvsd();

function loadCanvas(resp) {
  // response object contains a data field
  // that's essentialy a JSON string
  var json = JSON.parse(resp.data);
  canvas.loadFromDatalessJSON(json);
}

我得到一个奇怪的打印输出到控制台:未定义的调用方法'setupState'(fabric.min.js:1118)。我尝试用 canvas.loadFromJSON(json)替换调用,而是得到一个含糊的 SyntaxError:Unexpected token o 错误。当我在建议的链接线程中使用常规 fabric.Image 之前,此代码工作正常。我恐怕这可能是我失去了当我扩展favric的Image类与附加数据。

I get an odd printout to the console: Cannot call method 'setupState' of undefined (fabric.min.js: 1118). I tried replacing the call with canvas.loadFromJSON(json), and instead got a vague SyntaxError: Unexpected token o error. When I used a regular fabric.Image before the change suggested in the linked thread, this code worked fine. I fear this might be something I'm missing when I extended favric's Image class with aditional data. Thoughts?

推荐答案

搜索后,我看到一个类似线程在fabricjs google组中。原来我弄错了三件事:

After some search I saw a similar thread in the fabricjs google group. Turned out I messing up 3 things:


  1. 我失去了一个 fromObject()

  2. 我使用 loadFromDatalessJSON()当我使用了另一个自定义对象我应该一直使用 loadFromJSON()

  3. 我使用 JSON.parse(json),然后将它传递到 loadFromJSON()函数,该函数需要一个json字符串并在内部处理解析。 li>
  1. I was missing a fromObject() callback definition in a different location for another custom object I made (I had a couple).
  2. I was using loadFromDatalessJSON() when I should have been using simply loadFromJSON().
  3. I was parsing the json string into an actual JSON using JSON.parse(json) before passing it into the loadFromJSON() function, which expected a json string and handled the parsing internally.

Derp。

这篇关于FabricJS - 从JSON加载画布时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆