FabricJS5中动态设置文本字体粗细与样式的方法
FabricJS 更新后修改文本字体粗细的方案
在FabricJS API更新后,原obj.set("fontWeight", "bold")方法失效,可通过以下两种方式修改选中文本的字体粗细:
使用官方推荐的
setFontWeight()方法
直接对文本对象调用该方法,传入目标粗细值(支持'bold'/'normal'这类关键字,或700/400这类数字权重),最后刷新画布使修改生效:const activeObj = this.canvas.getActiveObject(); // 先确保选中的是文本对象 if (activeObj && activeObj.type === 'text') { activeObj.setFontWeight('bold'); this.canvas.renderAll(); }直接修改
fontWeight属性并刷新画布
也可以直接给文本对象的fontWeight属性赋值,同样需要调用renderAll()更新视图:const activeObj = this.canvas.getActiveObject(); if (activeObj && activeObj.type === 'text') { activeObj.fontWeight = 'bold'; this.canvas.renderAll(); }
注意:操作前最好判断选中对象的类型为text,避免非文本对象引发报错。
内容的提问来源于stack exchange,提问作者KevinTale
相关产品推荐
相关产品推荐

