How to add custom content in fabric.Image()

和自甴很熟 提交于 2020-01-06 04:08:06

问题


I am trying to use the following suggested way to store canvas in the server:

Fabric.js - how to save canvas on server with custom attributes

But in my case, I am loading an image from a url like:

fabric.Image.fromURL(url, function(image) {
   image.alt = product.skuId;
   image.productClass = product.productClass;
   canvas.add(image);
 }, {
   crossOrigin: "Annoymous"
});

But when I try to store the same in the db the newer attributes are not stored.

Is there a way to store custom attribute (or even "alt") in the DB?

Edit:

Created a fiddle with what I tried: https://jsfiddle.net/anjhawar/dpyb7cf7/

When we press save the canvas is dumped in the local storage, but when we check the "objects" array we do not get the custom attributes i.e. "id" and "alt", as per the example.

Am I missing something?


回答1:


Here my solution to store custom attribute (id).

fabric.Image.fromURL(link, function(img) {
    img.set({
        id : 'image_'+index,
        width : canvas.width / 2,
        height : canvas.height / 2
    });
    canvas.add(img).renderAll().setActiveObject(img);
});

For quick review here is a working code : http://jsfiddle.net/mullainathan/dpyb7cf7/1



来源:https://stackoverflow.com/questions/36970398/how-to-add-custom-content-in-fabric-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!