简体   繁体   English

将fabricjs canvas转换为base64图像

[英]Convert fabricjs canvas to base64 image

I'm trying to send a canvas as an image to my server and I want to send as base64. 我正在尝试将画布作为图像发送到我的服务器,我想发送为base64。 Fabricjs offers using canvas.toSVG() or canvas.toDataURL({format: 'image/png'}) to convert the canvas to an image however the output on my log appears to be a Klass object (first time seeing that) and upon sending that object to the server and logging what's received on the server I get an empty object or a path ie: Fabricjs提供了使用canvas.toSVG()canvas.toDataURL({format: 'image/png'})来将画布转换为图像,但是我的日志上的输出似乎是一个Klass对象(第一次看到它)并且在将该对象发送到服务器并记录服务器上收到的内容我得到一个空对象或路径,即:

{ version: '2.0.0-rc.3', objects: [] }
{ version: '2.0.0-rc.3',
  objects: 
   [ { type: 'path',
       version: '2.0.0-rc.3',
       originX: 'left',
       originY: 'top',
       left: 156.21249771118164,
       top: 221.20000457763672,
       width: 132,
       height: 53,
       fill: null,
       stroke: 'Red',
       strokeWidth: 10,
       strokeDashArray: null,
       strokeLineCap: 'round',
       strokeLineJoin: 'round',
       strokeMiterLimit: 10,
       scaleX: 1,
       scaleY: 1,
       angle: 0,
       flipX: false,
       flipY: false,
       opacity: 1,
       shadow: null,
       visible: true,
       clipTo: null,
       backgroundColor: '',
       fillRule: 'nonzero',
       paintFirst: 'fill',
       globalCompositeOperation: 'source-over',
       transformMatrix: null,
       skewX: 0,
       skewY: 0,
       path: [Object] } ] }

Klass object console image: Klass对象控制台图像:

在此输入图像描述

My code: 我的代码:

var canvas = new fabric.Canvas();
canvas.loadFromJSON(val[i].story); //val has saved canvas
canvas.toSVG(); //or to dataURL()

I tried this and still the same klass object, need to convert the canvas to base64 image (png preferred) and send to the server where I'll be converting to an image file, thanks. 我尝试了这个并且仍然是相同的klass对象,需要将画布转换为base64图像(png首选)并发送到我将转换为图像文件的服务器,谢谢。

因此,在创建画布时我错过了画布的id

var canvas = new fabric.Canvas('canvasID');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM