gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:30:38 27 4
gpt4 key购买 nike

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

现在的问题是,我有很多层的 json,一层是文本框,它有一些字体样式,所以该层的 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'];

粗体斜体普通 文本适合我。在删除 styles 之前和之后查看它的行为 HERE .

可能的解决方案是什么?

最佳答案

截至目前,当前的 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'];
}
}
}
}
}

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

关于javascript - 由于 json 中的字体样式对象,fabric js 粗体斜体不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35285129/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com