当前位置:网站首页>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个网站能让你的页面背景炫酷起来》点赞 + 关注 + 收藏 = 学会了
边栏推荐
- Golang quickly generates model and queryset of database tables
- Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
- 每日学习2
- PHP linked list creation and traversal
- C语言高级用法--函数指针:回调函数;转换表
- Launcher启动过程
- The most complete analysis of Flink frame window function
- [template] longest common subsequence ([DP or greedy] board)
- 快解析:轻松实现共享上网
- P1908 逆序对
猜你喜欢
每日学习3
PHP linked list creation and traversal
The conference on the growth of all things was held in Hangzhou, and dangbei was selected into the top 100 list of future unicorns in China in 2022
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 04 | 深入浅出索引(上)
[Hongke technology sharing] how to test DNS server: DNS performance and response time test
QT new project
Packet capturing tool Fiddler learning
Just 1000 fans, record it
Systemserver process
SystemServer进程
随机推荐
selenium,元素操作以及浏览器操作方法
Just 1000 fans, record it
2022家用投影仪首选!当贝F5强悍音画效果带来极致视听体验
MySQL 45 lecture - learning the actual battle of MySQL in Geek time 45 Lecture Notes - 05 | easy to understand index (Part 2)
In 2021, the global revenue of structural bolts was about $796.4 million, and it is expected to reach $1097.6 million in 2028
Qt如何设置固定大小
Use bloc to build a page instance of shutter
Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
How to set QT manual layout
Yolov3 & yolov5 output result description
自定义事件,全局事件总线,消息订阅与发布,$nextTick
Adhere to the foundation of 20 minutes go every day II
Essential elements of science fiction 3D scenes - City
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
Some interview suggestions for Android programmers "suggestions collection"
MySQL -- convert rownum in Oracle to MySQL
Getting started with QT - making a simple calculator
Default slot, named slot, scope slot
腾讯云 TStor 统一存储通过信通院首批文件存储基础能力评测
[to be continued] [UE4 notes] l5ue4 model import