简体   繁体   中英

fabric js bold italic not working because of font styles object in json

I am facing 1 typical issue in fabric js, I am converting canvas to json using canvas.toJson and saved it into database. Later i am loading that json into canvas using loadFromJSON which all working ok.

Now issue is, i have json which has many layers and one layer is textbox which has some styles on fonts, so json of that layer is like,

 {
     "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
  },

After load json i am doing bold and italic effect using this code.

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

Which is working but when i do this,

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

It will not working. After some investigation i can figure that styles attribute from json causing the issue, If i remove styles from main json object using this code,

delete index['styles'];

Then bold , italic and normal text works for me. See how it behaving before removing and after removing styles HERE .

What is the possible solution for this?

as of now with current fabricjs version there is this problem.

You can just delete the fontWeight property every time you set one new.

example:

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'];
                }
            }
        }
    }
}

Ugly. This should be fixed in the library itself that should not create meaningless styles objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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