由于json中的字体样式对象,fabric js粗体斜体不起作用

时间:2016-02-09 06:13:17

标签: javascript jquery json canvas fabricjs

我在fabric js中面临一个典型问题,我正在使用canvas.toJson将canvas转换为json并将其保存到数据库中。后来我使用loadFromJSON将json加载到画布中,这一切都正常。

现在的问题是,我有json,它有很多层,一层是textbox,在字体上有一些样式,所以该层的json就像,

 {
     "type":"textbox",
     "originX":"center",
     "originY":"top",
     "left":1200.84,
     "top":573.63,
     "width":312.81,
     "height":127.46,
     "fill":"rgb(0, 0, 0)",
     "stroke":null,
     "strokeWidth":1,
     "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,
     "lockMovementX":false,
     "lockMovementY":false,
     "evented":true,
     "overrideSecondary":1,
     "text":"NATURAL\nVENEERS",
     "fontSize":63,
     "fontWeight":"",
     "fontFamily":"'trebuchet ms'",
     "fontStyle":"",
     "lineHeight":0.9,
     "textDecoration":"",
     "textAlign":"left",
     "textBackgroundColor":"",
     "styles":{
        "0":{
           "0":{
              "fontSize":62.666651,
              "fontFamily":"'trebuchet ms'",
              "fontWeight":"",
              "fontStyle":""
           },
           "1":{
              "fontSize":62.666651,
              "fontFamily":"'trebuchet ms'",
              "fontWeight":"",
              "fontStyle":""
           },
           "2":{
              "fontSize":62.666651,
              "fontFamily":"'trebuchet ms'",
              "fontWeight":"",
              "fontStyle":""
           },
           "3":{
              "fontSize":62.666651,
              "fontFamily":"'trebuchet ms'",
              "fontWeight":"",
              "fontStyle":""
           },
           "4":{
              "fontSize":62.666651,
              "fontFamily":"'trebuchet ms'",
              "fontWeight":"",
              "fontStyle":""
           },
           "5":{
              "fontSize":62.666651,
              "fontFamily":"'trebuchet ms'",
              "fontWeight":"",
              "fontStyle":""
           },
           "6":{
              "fontSize":62.666651,
              "fontFamily":"'trebuchet ms'",
              "fontWeight":"",
              "fontStyle":""
           },
           "7":{
              "fontSize":62.666651
           }
        },
        "1":{
           "0":{
              "fontSize":62.666651,
              "fontFamily":"'trebuchet ms'",
              "fontWeight":"",
              "fontStyle":""
           },
           "1":{
              "fontSize":62.666651,
              "fontFamily":"'trebuchet ms'",
              "fontWeight":"",
              "fontStyle":""
           },
           "2":{
              "fontSize":62.666651,
              "fontFamily":"'trebuchet ms'",
              "fontWeight":"",
              "fontStyle":""
           },
           "3":{
              "fontSize":62.666651,
              "fontFamily":"'trebuchet ms'",
              "fontWeight":"",
              "fontStyle":""
           },
           "4":{
              "fontSize":62.666651,
              "fontFamily":"'trebuchet ms'",
              "fontWeight":"",
              "fontStyle":""
           },
           "5":{
              "fontSize":62.666651,
              "fontFamily":"'trebuchet ms'",
              "fontWeight":"",
              "fontStyle":""
           },
           "6":{
              "fontSize":62.666651,
              "fontFamily":"'trebuchet ms'",
              "fontWeight":"",
              "fontStyle":""
           }
        }
     },
     "minWidth":20
  },

加载json后,我使用此代码执行粗体和斜体效果。

canvas.getActiveObject().set("fontWeight", "bold");
canvas.renderAll();

哪个有效,但当我这样做时,

canvas.getActiveObject().set("fontWeight", "");
canvas.getActiveObject().set("fontWeight", "100");
canvas.getActiveObject().set("fontWeight", "normal");

它无法正常工作。经过一些调查后,我可以从json中找出导致问题的styles属性,如果我使用此代码从主json对象中删除样式,

delete index['styles'];

然后bolditalicnormal文字适合我。在删除styles HERE之前了解其行为方式。

可能的解决办法是什么?

1 个答案:

答案 0 :(得分:1)

目前使用当前的fabricjs版本存在这个问题。

每次设置一个新的时,您都可以删除fontWeight属性。

示例:

function cleanFontWeightStyle(obj) {
    if (obj.styles) {
        var style = obj.styles;
        for (rowIndex in style) {
            var row = style[rowIndex];
            for (letterIndex in row) {
                letter = row[letterIndex];
                if (letter.fontWeight && letter.fontWeight=='') {
                    delete letter['fontWeight'];
                }
            }
        }
    }
}

难看。这应该在库本身中修复,不应该创建无意义的样式对象。