当前位置:网站首页>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
边栏推荐
- 由粒子加速器产生的反中子形成的白洞
- In 2021, the global revenue of structural bolts was about $796.4 million, and it is expected to reach $1097.6 million in 2028
- 无主灯设计:如何让智能照明更加「智能」?
- Selenium installing selenium in pycharm
- Launcher startup process
- Story point vs. Human Sky
- TeamTalk源码分析之win-client
- Story points vs. human days
- 什么是 eRDMA?丨科普漫画图解
- 全屋Wi-Fi:一个谁也解决不好的痛点?
猜你喜欢
跨服务器数据访问的创建链接服务器方法
QT new project_ MyNotepad++
Dangbei projection 4K laser projection X3 Pro received unanimous praise: 10000 yuan projector preferred
Penetrate the remote connection database through the Intranet
BeanUtils -- shallow copy -- example / principle
千元投影小明Q1 Pro和极米NEW Play谁更好?和哈趣K1比哪款配置更高?
Code implementation MNLM
[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)
每日学习3
没有从远程服务器‘‘映射到本地用户‘(null)/sa‘的远程用户‘sa‘及服务主密码解密错误的解决办法
随机推荐
QT new project
删除元素(带过渡动画)
QT new project_ MyNotepad++
Dangbei projection 4K laser projection X3 Pro received unanimous praise: 10000 yuan projector preferred
< 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
Yolov3 & yolov5 output result description
Fabric.js 元素被选中时保持原有层级
Golang 快速生成数据库表的 model 和 queryset
博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”
抓包工具fiddler学习
Pycharm连接远程服务器
Design and implementation of car query system based on php+mysql
NLA自然语言分析,让数据分析更智能
Tencent cloud tstor unified storage passed the evaluation of the first batch of basic file storage capabilities of the ICT Institute
Thymeleaf dependency
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 05 | 深入浅出索引(下)
MQ教程 | Exchange(交换机)
Selenium installing selenium in pycharm
线性dp求解 最长子序列 —— 小题三则
瀏覽器驅動的下載