当前位置:网站首页>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
linethrough
Method 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
iText
Enter 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
边栏推荐
- Use of freemaker
- < 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
- Daily learning 2
- Dangbei projection 4K laser projection X3 Pro received unanimous praise: 10000 yuan projector preferred
- freemarker的使用
- NLA自然语言分析实现数据分析零门槛
- P1908 逆序对
- How kaggle uses utility script
- In 2021, the global revenue of structural bolts was about $796.4 million, and it is expected to reach $1097.6 million in 2028
- 瀏覽器驅動的下載
猜你喜欢
提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
TeamTalk源码分析之win-client
kaggle如何使用utility script
Everyone believes that the one-stop credit platform makes the credit scenario "useful"
Launcher启动过程
Do you know that there is an upper limit on the size of Oracle data files?
< 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
Design of non main lamp: how to make intelligent lighting more "intelligent"?
MQ教程 | Exchange(交换机)
[deep learning] simple implementation of neural network forward propagation
随机推荐
万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单
当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选
什么是 eRDMA?丨科普漫画图解
Qt新建项目
Default slot, named slot, scope slot
线性dp求解 最长子序列 —— 小题三则
跨服务器数据访问的创建链接服务器方法
There is no solution to the decryption error of the remote user 'sa' and the service master password mapped from the remote server 'to the local user' (null) /sa '
[development environment] StarUML tool (download software | StarUML installation | StarUML creation project)
关于Flink框架窗口(window)函数最全解析
Advanced usage of C language -- function pointer: callback function; Conversion table
故事点 vs. 人天
Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
Whole house Wi Fi: a pain point that no one can solve?
The use of TestNG, the testing framework (II): the use of TestNG XML
Who is better, Qianyuan projection Xiaoming Q1 pro or Jimi new play? Which configuration is higher than haqu K1?
php链表创建和遍历
篇9:XShell免费版安装
提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
docker mysql