Fabric.JS无法使用Promises或回调

时间:2018-05-15 14:22:33

标签: javascript node.js callback promise fabricjs

所以我使用名为fabric.js 的Node.JS / NPM库(Node.JS,而不是浏览器),这是一种奇怪的方式。

//Make a new Canvas object (`fabric.createCanvasForNode` is not a constructor)
var canvas = new fabric.StaticCanvas(null, {width: 200, height: 200})

  //Make a new Rectangle
  var rect = new fabric.Rect({
    left: 100,
    top: 100,
    width: 100,
    height: 50,
    fill: "red"
  })

//Put the Rectangle on the Canvas
canvas.add(rect)

//Save the image as a PNG
canvas.createPNGStream().pipe(fs.createWriteStream(__dirname + "/output.png"))

唯一的问题是canvas.add()应该返回Promise的功能,但不是。它是一个不是即时的函数,但是还没有与Promises(canvas.add(...).then() is not a function)一起使用。我接下来要做的就是回调。我试过了:

canvas.add(rect, function (err, canvas) {
  console.log("working")
})

一起
canvas.add(rect, function () {
  console.log("working")
})

但两人都误以为:

C:\Users\me\Documents\my project\node_modules\fabric\dist\fabric.js:3696
            obj._set("canvas", this);
                ^

TypeError: obj._set is not a function
    at klass._onObjectAdded (C:\Users\me\Documents\my project\node_modules\fabric\dist\fabric.js:3696:17)
    at klass.add (C:\Users\me\Documents\my project\node_modules\fabric\dist\fabric.js:152:22)
    at Client.client.on.message (C:\Users\me\Documents\my project\index.js:41:10)
    at emitOne (events.js:115:13)
    at Client.emit (events.js:210:7)
    at MessageCreateHandler.handle (C:\Users\me\Documents\my project\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\me\Documents\my project\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
    at WebSocketConnection.onPacket (C:\Users\me\Documents\my project\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:330:35)
    at WebSocketConnection.onMessage (C:\Users\me\Documents\my project\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:293:17)
    at WebSocketClient.internalOnMessage (C:\Users\me\Documents\my project\node_modules\uws\uws.js:103:17)

那我该怎么办?我是否在使用Promise和/或回调的方式做错了什么?面料根本不支持它们吗?什么是解决方法?

1 个答案:

答案 0 :(得分:0)

根据documentation方法fabric.StaticCanvas.add,请勿使用PromiseCallback

enter image description here