I want to use fabric js on a node server to generate SVG images. I followed the instructions to run fabric js on node in a mac environment. I'm trying to run the hello world fabric application from the fabric js documentation under nodejs examples.
If i run typeof require('canvas'); in the node shell. It returns 'function' as it should, according to fabricjs documentation
Running typeof require('fabric'); also returns object as it should. Im pretty sure that I installed the dependencies correctly but not entirely sure why the code is not running as expected.
However, running the code results in this error: TypeError: fabric.createCanvasForNode is not a function
Here is the code I am trying to run.
I changed my python version to 2.7 and node is v 9.2.0 . All help is appreciated!
var fs = require('fs'),
fabric = require('fabric').fabric,
out = fs.createWriteStream(__dirname + '/helloworld.png');
var canvas = fabric.createCanvasForNode(200, 200);
var text = new fabric.Text('Hello world', {
left: 100,
top: 100,
fill: '#f55',
angle: 15
});
canvas.add(text);
var stream = canvas.createPNGStream();
stream.on('data', function(chunk) {
out.write(chunk);
});
Dez' Answer almost satisfied me, but it was not a fully working example. At least not for me. This is my result after digging a little bit deeper into the topic:
const { fabric } = require("fabric");
const canvas = new fabric.Canvas(null, {width: 500, height: 500});
const fs = require("fs");
const stream = canvas.createPNGStream();
const out = fs.createWriteStream("test.png");
var text = new fabric.Text("Hello world", {
left: 100,
top: 100,
fill: "#f55",
angle: 15
});
canvas.add(text);
canvas.renderAll();
stream.on('data', function(chunk) {
out.write(chunk);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With