当前位置:网站首页>Fabric. JS dynamically set font size
Fabric. JS dynamically set font size
2022-07-02 14:26:00 【Director of Moral Education Department】
Brief introduction
give the thumbs-up + Focus on + Collection = Learned to
<br>
This article USES the Fabric.js
Of IText
demonstration . If you don't understand Fabric.js
, I recommend you to read 《Fabric.js From entry to mastery 》.
<br>
A picture is worth a thousand words , Look at the pictures before we write the composition ~
<br>
<br>
analysis
demand
The following functional requirements can be seen from the above figure :
- The font size can be modified only after the text is selected
- Overall modification of font size
- Modify the font size of the selected words , If it is not selected, it will not be modified
<br>
Solutions
- Get the selected text :
canvas.getActiveObject()
. - Whether to select only part of the text :
- I use the editing status to determine whether only part of the text is selected :
isEditing
. - Change the style of the selected text :
setSelectionStyles({...})
.
- I use the editing status to determine whether only part of the text is selected :
- modify
fontSize
attribute . - If it is a full text modification , It is also necessary to determine whether some characters are set in themselves
fontSize
, If the text is set by itselffontSize
, The weight set in the full text is not as high as that set independently .
<br>
<br>
code
<input type="range" min="5" max="150" value="40" id="size" onchange="changeSize(value)"><canvas id="c" width="300" height="300" style="border: 1px solid #ccc"></canvas><script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/521/fabric.js"></script><script> const canvas = new fabric.Canvas('c') const iText = new fabric.IText('hello world') canvas.add(iText) function changeSize(value) { let activeTxt = canvas.getActiveObject() if (!activeTxt) return if (activeTxt.isEditing) { // Edit state activeTxt.setSelectionStyles({ 'fontSize': value}) } else { activeTxt.fontSize = value let s = activeTxt.styles // Traversing rows and columns for(let i in s) { for (let j in s[i]) { s[i][j].fontSize = value // Set the font size for each word } } activeTxt.dirty = true } canvas.renderAll() }</script>
Use isEditing
Judge whether to enter the editing status , I modify the font size according to whether there is selected text in the editing state .
In the end 2 Layer cycle :
for(let i in s) { for (let j in s[i]) { s[i][j].fontSize = value }}
Layer 1 cycle i
Is a traversal of the number of rows ; The second cycle j
Is the traversal of the current line of text .
This is because if only setSelectionStyles
Setting the font size cannot override the setting of each word itself fontSize
. So it still needs to be cycled , Make sure every word is correct .
<br>
The example of setting font size can also be used in Superscript and Subscript in , such as 《Fabric.js The use of superscripts and subscripts 》 The usage in a passage . Superscript and subscript are applicable to the combination of mathematical formulas and chemical symbols .
<br>
<br>
Code warehouse
IText Dynamically modify font size
<br>
<br>
Recommended reading
《Fabric.js Activate the input box 》
《Fabric.js Output reduced JSON》
《Fabric.js Lock background image , Not affected by zooming and dragging 》 give the thumbs-up + Focus on + Collection = Learned to
边栏推荐
- selenium 元素定位方法
- Tencent cloud tstor unified storage passed the evaluation of the first batch of basic file storage capabilities of the ICT Institute
- Using computed in uni app solves the abnormal display of data () value in tab switching
- Chinese science and technology from the Winter Olympics (III): the awakening and evolution of digital people
- 什么是 eRDMA?丨科普漫画图解
- <口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
- 无主灯设计:如何让智能照明更加「智能」?
- Openharmony notes --------- (4)
- Advanced usage of C language -- function pointer: callback function; Conversion table
- In 2021, the global styrene butadiene styrene (SBS) revenue was about $3722.7 million, and it is expected to reach $5679.6 million in 2028
猜你喜欢
默认插槽,具名插槽,作用域插槽
Systemserver process
In 2021, the global revenue of structural bolts was about $796.4 million, and it is expected to reach $1097.6 million in 2028
Default slot, named slot, scope slot
MQ教程 | Exchange(交换机)
万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单
The use of TestNG, the testing framework (II): the use of TestNG XML
How kaggle uses utility script
Penetrate the remote connection database through the Intranet
Téléchargement par navigateur
随机推荐
The use of TestNG, the testing framework (II): the use of TestNG XML
联合搜索:搜索中的所有需求
抓包工具fiddler学习
Chapter 9: xshell free version installation
Go operation redis
Selenium element positioning method
uni-app中使用computed解决了tab切换中data()值显示的异常
Pychart connects to the remote server
Fabric.js 元素被选中时保持原有层级
NLA自然语言分析,让数据分析更智能
MySQL -- convert rownum in Oracle to MySQL
Teamtalk source code analysis win client
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 04 | 深入浅出索引(上)
< 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
《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持
How many knowledge points can a callable interface have?
什么是 eRDMA?丨科普漫画图解
Whole house Wi Fi: a pain point that no one can solve?
瀏覽器驅動的下載
Codeforces Round #803 (Div. 2)(A~D)