当前位置:网站首页>Fabric.js 上划线、中划线(删除线)、下划线
Fabric.js 上划线、中划线(删除线)、下划线
2022-06-26 09:49:00 【InfoQ】
本文简介
HTMLWordFabric.jscanvas
fabric.jsIText初始化时设置
- 上划线 overline
- 中划线 linethrough
- 下划线 underline

<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>
astyleskey0styles: {
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方法添加或取消中划线
linethroughlinethrough- 获取当前选中的文字
- 如果没选中就不做任何操作
- 如果选中了,判断是否进入编辑状态
- 编辑状态
- 获取当前被选中文字的中划线状态
- 如果被选中文字设置了中划线,就把中划线取消掉
- 如果被选中文字没设置中划线,就添加中划线
- 如果不是编辑状态,只是单击了
iText进入框选状态
- 如果需要全局取消中划线
- 全局取消
- 循环每个字符,并取消每个字符的中划线
- 需要全局设置中划线
- 全局设置
- 再逐个字符单独设置
- 重新渲染画布
代码仓库
边栏推荐
- Reshape a two-dimensional array with 3 rows and 3 columns to find the sum of the diagonals
- Global and Chinese market of aluminum sunshade systems 2022-2028: Research Report on technology, participants, trends, market size and share
- String constant pool, class constant pool, and runtime constant pool
- The sixth MySQL job - query data - multiple conditions
- Yarn package management tool
- Oracle sqlplus query result display optimization
- Use of exec series functions (EXECL, execlp, execle, execv, execvp)
- How to start the learning journey of webrtc native cross platform development?
- 【LeetCode】59. Spiral matrix II
- Flutter and native communication (Part 1)
猜你喜欢

Reshape a two-dimensional array with 3 rows and 3 columns to find the sum of the diagonals

Quantitative investment learning - Introduction to classic books

Record the handling of oom problems caused by too many threads at one time

Problems encountered in the application and development of Hongmeng and some roast

MySQL第十次作业-视图

【软件项目管理】期末复习知识点整理

MySQL job 11 - application de la vue

The fourteenth MySQL operation - e-mall project

What is in the method area - class file, class file constant pool, runtime constant pool

Establishment of smart dialogue platform for wechat official account
随机推荐
Global and Chinese markets of children's electronic thermometers 2022-2028: Research Report on technology, participants, trends, market size and share
What is in the method area - class file, class file constant pool, runtime constant pool
What is LSP
Problems encountered in the application and development of Hongmeng and some roast
Jar version conflict resolution
Développeur, quelle est l'architecture des microservices?
Global and Chinese market of cryogenic bulk tanks 2022-2028: Research Report on technology, participants, trends, market size and share
看我在Map<String, String>集合中,存入Integer类型数据
Record the handling of oom problems caused by too many threads at one time
Developers, what is the microservice architecture?
MySQL Chapter 5 Summary
【软件项目管理】期末复习知识点整理
MySQL backup and restore command
CentOS installs redis multi master multi slave cluster
SQL Server 基础介绍整理
Cmake / set command
Notes - simple but adequate series_ KVM quick start
Establishment of smart dialogue platform for wechat official account
sysbench基础介绍
MySQL 11th job - view application