【发布时间】:2021-11-15 21:06:46
【问题描述】:
我正在使用 fabric.js 来设计一个图像编辑器。我能够将画布转换为 JSON 对象并将其发送到后端。
在后端,我可以从 NodeJS 服务器渲染图像以将其视为静态图像。
这是我的前端代码:
var json = canvas.toJSON();
json 对象被发送到服务器。
我的后端服务器中的代码通过 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();
});
});
});
这是图像的渲染方式
当涉及到 GIF 时,我对如何将数据发送到后端有点困惑。
我能够使用 gifuct-js 包将 GIF 添加到画布上。当我将画布转换为 JSON 对象时,大小超过 10mb。我觉得这不是将信息传递到后端的正确方法。另外,如何从后端渲染它?
【问题讨论】:
-
您是如何将 GIF 图像导入矢量画布的?
-
不,它显示为图像
-
那么 gifuct-js 是从哪里进入的呢?
-
奇怪的这个问题没有关闭...您的代码都是关于 PNG 但您描述的是 GIF 错误和一个不在您的示例代码中的包...标题也很误导
标签: javascript node.js html5-canvas fabricjs