当前位置:网站首页>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
fontSizeattribute . - 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
边栏推荐
- Qt原代码基本知识
- The most complete analysis of Flink frame window function
- uniapp自动化测试学习
- P1042 [noip2003 popularization group] Table Tennis
- Development skills of rxjs observable custom operator
- Development and design of animation surrounding mall sales website based on php+mysql
- Use of UIC in QT
- 你知道Oracle的数据文件大小有上限么?
- Basic knowledge of QT original code
- 当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选
猜你喜欢

Quick analysis: easy to share the Internet

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

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

无主灯设计:如何让智能照明更加「智能」?

Chinese science and technology from the Winter Olympics (III): the awakening and evolution of digital people

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

Browser driven Download

Pycharm连接远程服务器

< 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

php链表创建和遍历
随机推荐
Daily learning 3
MySQL -- convert rownum in Oracle to MySQL
In 2021, the global revenue of structural bolts was about $796.4 million, and it is expected to reach $1097.6 million in 2028
Launcher startup process
[to be continued] [UE4 notes] l5ue4 model import
Understanding of mongodb
关于Flink框架窗口(window)函数最全解析
Basic knowledge of QT original code
Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)
2022家用投影仪首选!当贝F5强悍音画效果带来极致视听体验
[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)
Design of non main lamp: how to make intelligent lighting more "intelligent"?
篇9:XShell免费版安装
uni-app中使用computed解决了tab切换中data()值显示的异常
Code implementation MNLM
Data consistency between redis and database
Golang quickly generates model and queryset of database tables
提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
删除元素(带过渡动画)
< schéma de développement de la machine d'exercice oral > machine d'exercice oral / trésor d'exercice oral / trésor de mathématiques pour enfants / lecteur LCD de calculatrice pour enfants IC - vk1621