Fabric.js loadFromJSON无法使用rgba fill

时间:2017-01-02 17:54:48

标签: javascript fabricjs

我有一个简单的画布,我在其中添加形状(三角形,圆形,矩形)。我使用了支持rgba的光谱颜色选择器,我根据所选颜色设置了形状的填充。到目前为止一切正常。现在我将画布导出为JSON,

var json = JSON.stringify(canvas);
console.log(json);

此JSON输出稍后将保存到数据库中,但出于测试目的,我将从控制台复制json并按如下方式使用它,

canvas.loadFromJSON('{"objects":[{"type":"circle","originX":"center","originY":"center","left":400,"top":200,"width":100,"height":100,"fill":{"_originalInput":{"h":"0%","s":"100%","v":"100%","a":0.13},"_r":255,"_g":0,"_b":0,"_a":0.13,"_roundA":0.13,"_format":"rgb","_ok":true,"_tc_id":3288},"stroke":{"_originalInput":{"h":"0%","s":"0%","v":"0%","a":1},"_r":0,"_g":0,"_b":0,"_a":1,"_roundA":1,"_format":"rgb","_ok":true,"_tc_id":3783},"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":0.4,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"radius":50,"startAngle":0,"endAngle":6.283185307179586},{"type":"triangle","originX":"left","originY":"top","left":583,"top":89,"width":50,"height":50,"fill":{"_originalInput":{"h":"17.374213836477992%","s":"86.88524590163934%","v":"95.68627450980392%","a":0.34},"_r":235.01046366720004,"_g":243.98400000000004,"_b":32.01070080000003,"_a":0.34,"_roundA":0.34,"_format":"hex","_ok":true,"_tc_id":2503},"stroke":"rgb(0, 0, 0)","strokeWidth":6,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0},{"type":"rect","originX":"left","originY":"top","left":485,"top":196,"width":50,"height":50,"fill":"#1d128f","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":0.4,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"rx":0,"ry":0}],"background":""}');
canvas.renderAll();

我面临的问题是,由于导出的JSON具有rgba值,因此当在画布上呈现形状时,填充为黑色。画布忽略了JSON字符串中的rgba值。

但是,当我使用以下JSON字符串(填充“绿色”和“红色”)时,形状会呈现绿色和红色。

canvas.loadFromJSON('{"objects":[{"type":"rect","left":50,"top":50,"width":20,"height":20,"fill":"green","overlayFill":null,"stroke":null,"strokeWidth":1,"strokeDashArray":null,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"selectable":true,"hasControls":true,"hasBorders":true,"hasRotatingPoint":false,"transparentCorners":true,"perPixelTargetFind":false,"rx":0,"ry":0},{"type":"circle","left":100,"top":100,"width":100,"height":100,"fill":"red","overlayFill":null,"stroke":null,"strokeWidth":1,"strokeDashArray":null,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"selectable":true,"hasControls":true,"hasBorders":true,"hasRotatingPoint":false,"transparentCorners":true,"perPixelTargetFind":false,"radius":50}],"background":"rgba(0, 0, 0, 0)"}');

这是fabric.js中的错误还是我做错了?

1 个答案:

答案 0 :(得分:1)

行。我发现了为什么会这样。问题是我如何设置形状对象的颜色。我正在使用Spectrum颜色选择器并使用以下代码初始化Spectrum并添加代码,当在光谱颜色选择器上选择颜色时,它也会在画布上针对选定的形状对象进行更改。

$(".spectrumColor").spectrum({
        showAlpha: true,
        showButtons: false,
        showPalette: true,
        palette: defaultPallete,
        allowEmpty: true,
        showInput: true,
        move: function (color) {

            var selectedObject = canvas.getActiveObject();
            if (selectedObject != null) {
                if ($(this).attr('id') == "FillColor") {
                    selectedObject.set('fill', color);
                    canvas.renderAll();
                }
                else if ($(this).attr('id') == "StrokeColor") {
                    selectedObject.set('stroke', color);
                    canvas.renderAll();
                }
            }
        }
    });

问题出在这一行,

selectedObject.set('stroke', color);

我已将整个颜色对象从光谱传递到Fabric形状对象。当我在控制台中检查颜色的值时,它是简单的rgb字符串“rgb(255,0,0)”,但是当完整对象被分配给Fabric的形状对象时,它将添加hsv(色调,饱和度和亮度)字符串以及RGB到Fabric形状对象,如下所示,

fill":{"_originalInput":{"h":"0%","s":"100%","v":"100%","a":0.13},"_r":255,"_g":0,"_b":0,"_a":0.13

由于Fabric.js无法识别HSV,因此它会将形状对象渲染为黑色。要解决这个问题,我必须更换,

    selectedObject.set('stroke', color);

    selectedObject.set('stroke', color.toRgbString());