简体   繁体   English

保存用fabric.js创建的画布

[英]Save canvas created with fabric.js

i want to store the canvas generated with the fabric.js, i have canvas as like below in my page 我想存储用fabric.js生成的画布,我的画布如下所示

 <div class="canvas-container" style="width: 600px; height: 600px; position: relative; -moz-user-select: none;">
    <canvas height="600" width="600" id="design-stage" class="lower-canvas" style="position: absolute; width: 600px; height: 600px; left: 0px; top: 0px; -moz-user-select: none;"></canvas>
    <canvas class="upper-canvas " style="position: absolute; width: 600px; height: 600px; left: 0px; top: 0px; -moz-user-select: none; cursor: default;" width="600" height="600"></canvas>
    </div>

as said in the fabric.js documentation serialization tried to save the canvas as json, but am unable to get the objects in the canvas it always returns empty as follows 如在fabric.js文档中所述, 序列化尝试将画布另存为json,但是无法获取画布中的对象,因此它总是返回空,如下所示

{"objects":[],"background":""}

and my code to generate the json is follow 和我生成json的代码如下

var canvas = new fabric.Canvas('design-stage');
console.log(canvas);
var json = JSON.stringify( canvas.toJSON()  );
console.log(json);

Code to add the images on canvas 在画布上添加图像的代码

self._loadItemImage = function (url) { 
fabric.Image.fromURL(url, function (img) { 
var w = img.width; var h = img.height;
 var size = self._resizeImage(w, h, self._width - 100, self._height - 100); 
 img.set({ left: self._width / 2, top: self._height / 2, originX: 'center', originY: 'center', hasBorders: false, hasControls: false, hasRotatingPoint: false, selectable: false, scaleX: 0.5, scaleY: 0.5 }); 
 self._motorImg = img; 
 self._canvas.add(self._motorImg);
 }); 
 };

Note: Am not created objects using the functions in the fabric.js, i have loaded the svg images from a directory and added those images into the canvas by positioning the images 注意:不是使用fabric.js中的函数创建的对象,我已经从目录加载了svg图像,并通过放置图像将这些图像添加到画布中

fabric.Image.fromURL does some asynchronous stuff inside. fabric.Image.fromURL在内部做一些异步的事情。 The callback ( function (img) { ... } ) in your code will be called after the image is loaded. 加载图像 ,将调用代码中的回调function (img) { ... } )。 Though I cannot see the rest of the code I am sure you are calling JSON.stringify(canvas) right after calling fabric.Image.fromURL and are not waiting for the asynchronous image loading to complete. 虽然我不能看到代码的其余部分,我相信你在呼唤JSON.stringify(canvas)调用之后fabric.Image.fromURL ,而不是等待异步图像加载完成。

Try this: 尝试这个:

function Foo() {
    var self = this;
    self._motorImg = null;
    self._canvas  = new fabric.Canvas('c');

    self._loadItemImage = function (url) { 
        fabric.Image.fromURL(url, function (img) { 
            img.set({ left: 0, top: 0, originX: 'center', originY: 'center' }); 
            self._motorImg = img; 
            self._canvas.add(self._motorImg);

            self.debug('2');
        }); 
    };

    self.debug = function (when) {
        console.log(when, JSON.stringify(self._canvas));
    };


}

var foo = new Foo();
foo._loadItemImage('http://jsfiddle.net/img/logo.png');
foo.debug('1');

What you will surely notice is that foo.debug('1') outputs an empty canvas while foo.debug('2') outputs one where the image was added. 您肯定会注意到, foo.debug('1')输出一个空画布,而foo.debug('2')输出一个添加了图像的画布。 Try it yourself here . 在这里自己尝试。

On the first debug call function (url) { ... } has not yet been called by fabric.Image.fromURL , thus the canvas is empty. 在第一个debug调用function (url) { ... }尚未由fabric.Image.fromURL调用,因此画布为空。 You need to wait for it to be called, just like with any other AJAX request/callback. 您需要等待它被调用,就像其他任何AJAX请求/回调一样。 So any code depending on that image to be loaded has to be called from that callback. 因此,取决于要加载的图像的任何代码都必须从该回调中调用。

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

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