当前位置:网站首页>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
边栏推荐
- Yolov3 & yolov5 output result description
- Selenium element positioning method
- c# 水晶报表打印
- P1908 reverse sequence pair
- HMS core machine learning service helps zaful users to shop conveniently
- Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)
- Launcher启动过程
- Mysql5.7 installation super easy tutorial
- Browser driven Download
- freemarker的使用
猜你喜欢

Who is better, Qianyuan projection Xiaoming Q1 pro or Jimi new play? Which configuration is higher than haqu K1?

c# 水晶报表打印

The global special paper revenue in 2021 was about $27 million, and it is expected to reach $35 million in 2028. From 2022 to 2028, the CAGR was 3.8%

Selenium installing selenium in pycharm

Default slot, named slot, scope slot

STM32-DAC实验&高频DAC输出测试

Qt新建项目

Daily learning 3

Everyone believes that the one-stop credit platform makes the credit scenario "useful"

Use bloc to build a page instance of shutter
随机推荐
STM32-DAC实验&高频DAC输出测试
Quick analysis: easy to share the Internet
每天坚持20分钟go的基础二
万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单
BeanUtils -- shallow copy -- example / principle
删除元素(带过渡动画)
Analysis of CPU surge in production environment service
[to be continued] [UE4 notes] l5ue4 model import
uni-app中使用computed解决了tab切换中data()值显示的异常
Qt原代码基本知识
Pycharm连接远程服务器
Packet capturing tool Fiddler learning
Daily learning 2
selenium 元素定位方法
2022 home projector preferred! Dangbei F5 brings the ultimate audio-visual experience with its powerful audio-visual effect
篇9:XShell免费版安装
Convolutional neural network (Introduction)
Pychart connects to the remote server
The evolution process of the correct implementation principle of redis distributed lock and the summary of redison's actual combat
OpenHarmony笔记-----------(四)