I'm using fabric.js to design an Image editor. I'm able to convert the canvas to JSON object and send it to the backend.
In the backend I'm able to render the Image from NodeJS server to view it as a static image.
Here is my front end code:
var json = canvas.toJSON();
The json object is sent to server.
Code in my backend server to render the image via URL.
app.get('/image', function(req, res) {
let data = {"version":"4.6.0","objects":[{"type":"rect","version":"4.6.0","originX":"left","originY":"top","left":87,"top":24,"width":20,"height":20,"fill":"blue","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeDashOffset":0,"strokeLineJoin":"miter","strokeUniform":false,"strokeMiterLimit":4,"scaleX":16.48,"scaleY":16.48,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"backgroundColor":"","fillRule":"nonzero","paintFirst":"fill","globalCompositeOperation":"source-over","skewX":0,"skewY":0,"rx":0,"ry":0},{"type":"textbox","version":"4.6.0","originX":"left","originY":"top","left":148,"top":134,"width":237,"height":45.2,"fill":"rgb(0,0,0)","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeDashOffset":0,"strokeLineJoin":"miter","strokeUniform":false,"strokeMiterLimit":4,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"backgroundColor":"","fillRule":"nonzero","paintFirst":"fill","globalCompositeOperation":"source-over","skewX":0,"skewY":0,"fontFamily":"Fontdiner Swanky","fontWeight":"normal","fontSize":40,"text":"Demo Text","underline":false,"overline":false,"linethrough":false,"textAlign":"left","fontStyle":"normal","lineHeight":1.16,"textBackgroundColor":"","charSpacing":0,"styles":{},"direction":"ltr","path":null,"pathStartOffset":0,"pathSide":"left","minWidth":20,"splitByGrapheme":false}]}
res.writeHead(200, { 'Content-Type': 'image/png' });
var canvas = new fabric.StaticCanvas(null, { width: 800, height: 400 });
canvas.loadFromJSON(data, function() {
canvas.renderAll();
var stream = canvas.createPNGStream();
stream.on('data', function(chunk) {
res.write(chunk);
});
stream.on('end', function() {
res.end();
});
});
});
Here is how the image renders
I'm little confused on how to send data to backend when it comes to GIFs.
I was able to add GIF on to the canvas using gifuct-js package. When I convert the canvas to JSON object, the size is more than 10mb. I felt this was not the right way to pass information to backend. Also, how do I render it from backend?