2017-01-02 48 views
0

我有一个简单的画布上我加入的形状(三角形,圆形,矩形)。我已经使用了支持rgba的光谱颜色选择器,并且我正在根据所选颜色设置形状的填充。到目前为止,一切正常。现在我出口画布作为JSON,Fabric.js loadFromJSON不工作与RGBA填充

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中的错误,或者我做错了什么?

+0

在你loadFromJSON叫你好像路过'fill'的对象。每对[文件](http://fabricjs.com/docs/fabric.Object.html#fill),我相信它预计某种颜色的字符串。你可能想检查你的实现。 – Ben

+0

@我在这段代码中使用的JSON是由Fabric.js本身生成的。所以这不是我从其他来源将JSON加载到Fabric.js中。所以如果Fabric.js已经导出了这个JSON,那么它也应该能够加载它。如果Fabric.js不支持hsv和rgba,那么为什么它在导出JSON时包含了这些内容? –

+0

你有没有jsfiddle或它的东西? – Ben

回答

1

确定。我已经发现它为什么会发生。问题是我如何设置形状对象的颜色。我使用频谱颜色选择器,并使用以下代码来初始化频谱,并且还添加代码,当在光谱的ColorPicker则选择的颜色它也被改变为在画布上所选择的形状的对象。

$(".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); 

我已通过全彩色对象从光谱的织物形状对象。当我检查在控制台颜色的值,则它是简单的RGB字符串“RGB(255,0,0)”,但是当完整的对象被分配到织物的形状的对象则是加入HSV(色调,饱和度和亮度)与RGB沿弦向矩阵形状对象像这样,

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

如HSV无法识别由Fabric.js因此它被渲染为黑色的颜色的形状的物体。为了解决这个问题,我不得不更换,

selectedObject.set('stroke', color); 

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