当前位置:网站首页>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
边栏推荐
- Story points vs. human days
- <口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
- Fabric.js 手动加粗文本iText
- Selenium, element operation and browser operation methods
- 线性dp求解 最长子序列 —— 小题三则
- 关于Flink框架窗口(window)函数最全解析
- P1908 逆序对
- 由粒子加速器产生的反中子形成的白洞
- mongodb的认识
- Tip: SQL Server blocked the state 'openrowset/opendatasource' of component 'ad hoc distributed queries'
猜你喜欢

瀏覽器驅動的下載

Launcher startup process

Qt新建项目

QT new project_ MyNotepad++

Available solution development oral arithmetic training machine / math treasure / children's oral arithmetic treasure / intelligent math treasure LCD LCD driver ic-vk1622 (lqfp64 package), original te

博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”

每日学习3

抓包工具fiddler学习

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

没有从远程服务器‘‘映射到本地用户‘(null)/sa‘的远程用户‘sa‘及服务主密码解密错误的解决办法
随机推荐
<口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
C语言高级用法--函数指针:回调函数;转换表
Whole house Wi Fi: a pain point that no one can solve?
Daily learning 2
Codeforces Round #803 (Div. 2)(A~D)
Golang quickly generates model and queryset of database tables
【虹科技术分享】如何测试 DNS 服务器:DNS 性能和响应时间测试
故事点 vs. 人天
Story point vs. Human Sky
Qt原代码基本知识
Word frequency statistics & sorting
Some interview suggestions for Android programmers "suggestions collection"
The use of TestNG, the testing framework (II): the use of TestNG XML
Method of creating linked server for cross server data access
你知道Oracle的数据文件大小有上限么?
PHP linked list creation and traversal
联合搜索:搜索中的所有需求
自定义事件,全局事件总线,消息订阅与发布,$nextTick
Pycharm连接远程服务器
线性dp求解 最长子序列 —— 小题三则