所以我只是在Node.JS上摆弄Fabric.JS**,而不是web**,我已经成功地制作了一个静态画布,并在上面放了一个矩形。是时候出口了。下面是创建画布和添加矩形的代码:
var canvas = new fabric.StaticCanvas(null, {width: 200, height: 200})
var rect = new fabric.Rect({
left: 100,
top: 100,
width: 100,
height: 50,
fill: "red"
})
canvas.add(rect)
以下是我为导出图像所尝试的所有操作:
//Creates a PNG file that isn't generated correctly. It's as if it's corrupt. No program can open it
canvas.createPNGStream().on("data", function (chunk) {
fs.createWriteStream(__dirname + "/output.png").write(chunk)
})
//Causes a crash
canvas.createJPEGStream().on("data", function (chunk) {
fs.createWriteStream(__dirname + "/output.jpeg").write(chunk)
})
Error:
C:\Users\me\Documents\my project\node_modules\canvas\lib\jpegstream.js:44
canvas[method](options.bufsize, options.quality, options.progressive, function(err, chunk){
^
TypeError: canvas[method] is not a function
at C:\Users\me\Documents\my project\node_modules\canvas\lib\jpegstream.js:44:19
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
//Same thing as first try^^, except it's a JPEG
canvas.createJPEGStream({quality: 100}).pipe(fs.createWriteStream(__dirname + "/output.jpeg"))
//This one makes the PNG file, but when it's opened, its 100% transparent. That's it. The rectangle I added isn't there. Canvas size is correct, however
canvas.createPNGStream().pipe(fs.createWriteStream(__dirname + "/output.png"))
注意:在本例中,fabric.Canvas(null, {width: 200, height: 200})
的工作方式似乎与StaticCanvas
完全相同
注2:此代码:
var canvas = new fabric.createCanvasForNode(200, 200)
导致此错误:
var canvas = new fabric.createCanvasForNode(200, 200)
^
TypeError: fabric.createCanvasForNode is not a constructor
at Client.client.on.message (C:\Users\me\Documents\my project\index.js:31:14)
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)
at native.client.group.onMessage (C:\Users\me\Documents\my project\node_modules\uws\uws.js:57:15)
我是不是漏掉了什么?这些方法似乎都不起作用,这是我在谷歌上能找到的所有方法。导出图像的正确方法是什么?它可以是PNG或JPEG/JPG,最好是PNG,但JPEG/JPG也可以。任何一种常见的格式。
3条答案
按热度按时间ddrv8njm1#
找到答案了。
canvas.createPNGStream().pipe(fs.createWriteStream(__dirname + "/output.png"))
确实有效。它完全为空的原因是因为它看起来像canvas.add()
不是一个即时操作。我所做的就是在输出文件之前使用setTimeout()
。希望这对某人有帮助!33qvvth12#
我使用了来自
node-canvas
的示例代码,它可以正常工作vhmi4jdf3#
正如在这个tutorial,由Durga共享,你忘了使用,
canvas.renderAll();
添加这个解决了我的问题。
完整代码: