当前位置:网站首页>Fabric.js 上划线、中划线(删除线)、下划线
Fabric.js 上划线、中划线(删除线)、下划线
2022-07-02 11:08:00 【德育处主任】
本文简介
点赞 + 关注 + 收藏 = 学会了
<br>
在 HTML 也好,Word 也好,基本都有下划线和删除线(中划线)。
Fabric.js 作为一个老牌 canvas 库,当然也提供中划线和下划线的配置啦,除此之外还提供了上划线的配置。

本文要讲解的就是这3种装饰线在 fabric.js 中的使用方法。除了初始化配置外,还支持项目运行时,让用户动态配置。
本文案例使用了 IText ,该组件支持编辑功能。
<br>
<br>
初始化时设置
这是上划线、中划线和下划线的文档:
<br>

<canvas id="c" width="300" height="300" style="border: 1px solid #ccc"></canvas><script src="../../script/fabric.5.2.1.js"></script><script> const canvas = new fabric.Canvas('c') const iText = new fabric.IText('aaa',{ styles: { 0: { 0: { overline: true }, // 上划线 1: { linethrough: true }, // 中划线 2: { underline: true } // 下划线 } } }) canvas.add(iText)</script>本例只有3个字母 a ,而且都在同一行。
styles 第一个元素的 key 为 0 的意思是第一行,行号下标从0开始。
大概这个意思
styles: { 0: { // 第1行 0:, // 第1行 第1个字 1:, // 第1行 第2个字 2: // 第1行 第3个字 }}如果需要换行,那就要根据行号来定义了。换行的方法是文本内容里使用 \n 做换行。
动态设置
除了在初始化时可以设置装饰线外,Fabric.js 也提供了在运行时用户手动设置。不过代码会稍微复杂一点。
其实动态设置上划线、中划线和下划线的操作都是一样的,唯一要变的就是属性名。
我以中划线为例,之后上划线和下划线只需改个属性名。

<button onclick="linethrough()">中划线</button><canvas id="c" width="300" height="300" style="border: 1px solid #ccc"></canvas><canvas id="c" width="300" height="300" style="border: 1px solid #ccc"></canvas><!-- 引入fabric --><script src="../../script/fabric.5.2.1.js"></script><script> const canvas = new fabric.Canvas('c') // 初始化画布 const iText = new fabric.IText('hello wor\nld') // 创建文字 canvas.add(iText) function linethrough() { let activeTxt = canvas.getActiveObject() // 获取当前选中的文字 // 如果当前没选中文字,那什么都不操作 if (!activeTxt) return // 判断当前是否进入编辑状态 if (activeTxt.isEditing) { // 编辑状态 const state = activeTxt.getSelectionStyles().find(item => item.linethrough !== true) // 如果当前 if (!state || (JSON.stringify(state) === '{}' && activeTxt['linethrough'] === true)) { // 如果当前已经设置了中划线,那就把全局中划线取消 activeTxt.setSelectionStyles({ 'linethrough': false }) } else { // 如果当前没设置中划线,那就添加上中划线 activeTxt.setSelectionStyles({ 'linethrough': true }) } } else { // 选择状态 if (activeTxt['linethrough'] === true) { activeTxt.linethrough = false activeTxt.dirty = true; let s = activeTxt.styles for(let i in s) { for (let j in s[i]) { s[i][j].linethrough = false } } } else { activeTxt.linethrough = true activeTxt.dirty = true; let s = activeTxt.styles for(let i in s) { for (let j in s[i]) { s[i][j].linethrough = true } } } } canvas.renderAll() }</script>上面的代码主要做这几步操作:
- 初始化画布
- 创建文字
- 将文字添加到画布中
linethrough方法添加或取消中划线
<br>
最主要的操作逻辑写在 linethrough 方法里了。
linethrough 的逻辑如下
- 获取当前选中的文字
- 如果没选中就不做任何操作
- 如果选中了,判断是否进入编辑状态
- 编辑状态
- 获取当前被选中文字的中划线状态
- 如果被选中文字设置了中划线,就把中划线取消掉
- 如果被选中文字没设置中划线,就添加中划线
- 如果不是编辑状态,只是单击了
iText进入框选状态- 如果需要全局取消中划线
- 全局取消
- 循环每个字符,并取消每个字符的中划线
- 需要全局设置中划线
- 全局设置
- 再逐个字符单独设置
- 如果需要全局取消中划线
- 重新渲染画布
<br>
<br>
代码仓库
<br>
<br>
推荐阅读
《这18个网站能让你的页面背景炫酷起来》点赞 + 关注 + 收藏 = 学会了
边栏推荐
- 万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单
- PyQt5_ Qscrollarea content is saved as a picture
- kaggle如何使用utility script
- 数据湖(十一):Iceberg表数据组织与查询
- Stone merging Board [interval DP] (ordinary stone Merging & Ring Stone merging)
- 如何设置Qt手工布局
- <口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
- 你知道Oracle的数据文件大小有上限么?
- P3008 [usaco11jan]roads and planes g (SPFA + SLF optimization)
- < schematic diagram of oral arithmetic exercise machine program development> oral arithmetic exercise machine / oral arithmetic treasure / children's math treasure / children's calculator LCD LCD driv
猜你喜欢

Do you know that there is an upper limit on the size of Oracle data files?

YOLOv3&YOLOv5输出结果说明

MySQL45讲——学习极客时间MySQL实战45讲笔记—— 04 | 深入浅出索引(上)

Everyone believes that the one-stop credit platform makes the credit scenario "useful"

The most complete analysis of Flink frame window function

Daily learning 2

无主灯设计:如何让智能照明更加「智能」?

selenium 在pycharm中安装selenium
![[development environment] Dell computer system reinstallation (download Dell OS recovery tool | use Dell OS recovery tool to make USB flash disk system | install system)](/img/e0/e9cb42f241a60995d4598ba6a1a2fe.jpg)
[development environment] Dell computer system reinstallation (download Dell OS recovery tool | use Dell OS recovery tool to make USB flash disk system | install system)

每日学习3
随机推荐
MySQL 45 lecture - learning the actual battle of MySQL in Geek time 45 Lecture Notes - 05 | easy to understand index (Part 2)
c# 水晶报表打印
Use of UIC in QT
Codeforces Round #803 (Div. 2)(A~D)
Story point vs. Human Sky
关于Flink框架窗口(window)函数最全解析
P1347 sorting (topology + SPFA judgment ring or topology [inner judgment ring])
当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选
测试框架TestNG的使用(二):testNG xml的使用
mongodb的认识
Launcher startup process
The global special paper revenue in 2021 was about $27 million, and it is expected to reach $35 million in 2028. From 2022 to 2028, the CAGR was 3.8%
Development skills of rxjs observable custom operator
【虹科技术分享】如何测试 DNS 服务器:DNS 性能和响应时间测试
Development and design of animation surrounding mall sales website based on php+mysql
Quarkus learning IV - project development to deployment
Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)
OpenHarmony笔记-----------(四)
Story points vs. human days
无主灯设计:如何让智能照明更加「智能」?