4

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);
}

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?

4

1 回答 1

2

经过一番搜索,我在 fabricjs google 组中看到了一个类似的线程。原来我搞砸了三件事:

  1. fromObject()对于我制作的另一个自定义对象(我有几个),我在不同的位置缺少回调定义。
  2. loadFromDatalessJSON()当我应该简单地使用时,我正在使用loadFromJSON()
  3. JSON.parse(json)在将 json 字符串传递给函数之前,我将它解析为实际的 JSON loadFromJSON(),该函数需要一个 json 字符串并在内部处理解析。

德普。

于 2012-07-02T18:14:48.400 回答