当前位置:网站首页>Fabric. JS upper dash, middle dash (strikethrough), underline
Fabric. JS upper dash, middle dash (strikethrough), underline
2022-07-02 14:26:00 【Director of Moral Education Department】
Brief introduction
give the thumbs-up + Focus on + Collection = Learned to
<br>
stay HTML Good ,Word Good , Basically, there are underscores and strikeouts ( Center line ).
Fabric.js As an old card canvas library , Of course, it also provides Center line and Underline The configuration of , In addition, it also provides Top line Configuration of .

This article is about this 3 This kind of decorative thread is in fabric.js How to use in . In addition to initializing the configuration , It also supports project runtime , Let users dynamically configure .
This case uses IText , This component supports editing functions .
<br>
<br>
Set on initialization
This is an overline 、 Underlined and underlined documents :
<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 }, // Top line 1: { linethrough: true }, // Center line 2: { underline: true } // Underline } } }) canvas.add(iText)</script> In this case, only 3 Letters a , And all in the same line .
styles Of the first element key by 0 It means the first line , Line number subscript from 0 Start .
About that
styles: { 0: { // The first 1 That's ok 0:, // The first 1 That's ok The first 1 A word 1:, // The first 1 That's ok The first 2 A word 2: // The first 1 That's ok The first 3 A word }} If you need a new line , Then it should be defined according to the line number . The method of line feed is to use... In the text content \n Do a new line .
Dynamic setting
In addition to setting the decorative line during initialization ,Fabric.js It also provides user manual settings at runtime . But the code is a little more complicated .
In fact, you can dynamically set the overline 、 The operation of underline and underline is the same , The only thing to change is the attribute name .
I take the middle line as an example , After that, you just need to change the property name .

<button onclick="linethrough()"> Center line </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><!-- introduce fabric --><script src="../../script/fabric.5.2.1.js"></script><script> const canvas = new fabric.Canvas('c') // Initialize canvas const iText = new fabric.IText('hello wor\nld') // Create text canvas.add(iText) function linethrough() { let activeTxt = canvas.getActiveObject() // Get the currently selected text // If no text is currently selected , Then do nothing if (!activeTxt) return // Judge whether to enter the editing status if (activeTxt.isEditing) { // Edit state const state = activeTxt.getSelectionStyles().find(item => item.linethrough !== true) // If at present if (!state || (JSON.stringify(state) === '{}' && activeTxt['linethrough'] === true)) { // If the middle dash line is currently set , Then cancel the line in the overall situation activeTxt.setSelectionStyles({ 'linethrough': false }) } else { // If the middle dash is not currently set , Then add an upper and middle dash activeTxt.setSelectionStyles({ 'linethrough': true }) } } else { // Selection status 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>The above code mainly does these steps :
- Initialize canvas
- Create text
- Add text to the canvas
linethroughMethod to add or remove a dash line
<br>
The main operating logic is written in linethrough There's a way .
linethrough The logic is as follows
- Get the currently selected text
- If it is not selected, no operation will be performed
- If you choose , Determine whether to enter the editing state
- Edit state
- Gets the underline status of the currently selected text
- If the selected text has a middle dash , Just cancel the middle line
- If the selected text does not have a middle dash , Just add a middle dash
- If it is not in editing status , Just click
iTextEnter the box selection status- If you need to cancel the dash line globally
- Global cancel
- Loop each character , And cancel the middle dash of each character
- You need to underline in the global settings
- Global settings
- Then set it individually character by character
- If you need to cancel the dash line globally
- Re render the canvas
<br>
<br>
Code warehouse
IText Dynamically set the decorative line
<br>
<br>
Recommended reading
《Fabric.js Dynamically set the font size 》
《Fabric.js Activate the input box 》
《Fabric.js Output reduced JSON》
《 this 18 A website can make your page background cool 》 give the thumbs-up + Focus on + Collection = Learned to
边栏推荐
- Multi rotor aircraft control using PID and LQR controllers
- 2022 home projector preferred! Dangbei F5 brings the ultimate audio-visual experience with its powerful audio-visual effect
- OpenHarmony笔记-----------(四)
- Qt原代码基本知识
- Start to write a small demo - three piece chess
- < 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
- [development environment] StarUML tool (download software | StarUML installation | StarUML creation project)
- uniapp自动化测试学习
- Simple introduction to ENSP
- Go operation redis
猜你喜欢

Error: eacces: permission denied, access to "/usr/lib/node_modules"
![[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)

Fabric.js 上划线、中划线(删除线)、下划线

联合搜索:搜索中的所有需求

<口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持

PHP linked list creation and traversal

千元投影小明Q1 Pro和极米NEW Play谁更好?和哈趣K1比哪款配置更高?

Essential elements of science fiction 3D scenes - City

Selenium, element operation and browser operation methods

SystemServer进程
随机推荐
提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
php链表创建和遍历
Start to write a small demo - three piece chess
c# 水晶报表打印
QT new project
《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持
Use bloc to build a page instance of shutter
2022家用投影仪首选!当贝F5强悍音画效果带来极致视听体验
抓包工具fiddler学习
PyQt5_ Qscrollarea content is saved as a picture
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
Essential elements of science fiction 3D scenes - City
Systemserver process
Golang 快速生成数据库表的 model 和 queryset
Word frequency statistics & sorting
Code implementation MNLM
P1908 reverse sequence pair
Understanding of mongodb
没有从远程服务器‘‘映射到本地用户‘(null)/sa‘的远程用户‘sa‘及服务主密码解密错误的解决办法
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 05 | 深入浅出索引(下)