当前位置:网站首页>Fabric.js 动态设置字号大小
Fabric.js 动态设置字号大小
2022-07-02 11:08:00 【德育处主任】
本文简介
点赞 + 关注 + 收藏 = 学会了
<br>
本文使用 Fabric.js 的 IText 演示。如果你还不懂 Fabric.js ,我墙裂推荐你阅读 《Fabric.js从入门到精通》。
<br>
一图胜千言,先看看图我们再写作文~

<br>
<br>
分析
需求
从上图可以看出以下功能需求:
- 文本被选中后才能修改字号
- 整体修改字号
- 修改被选中的几个字的字号,没被选中的不进行修改
<br>
解决思路
- 获取被选中的文字:
canvas.getActiveObject()。 - 是否只选中一部分文字:
- 我通过编辑状态来判断是否只选中一部分文字:
isEditing。 - 修改被选中文字的样式:
setSelectionStyles({...})。
- 我通过编辑状态来判断是否只选中一部分文字:
- 修改
fontSize属性。 - 如果是全文修改,还要判断是否有些字符在自身设置了
fontSize,如果文字自己设置了fontSize,那全文设置的权重没独立设置的那么高。
<br>
<br>
编码
<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) { // 编辑状态 activeTxt.setSelectionStyles({ 'fontSize': value}) } else { activeTxt.fontSize = value let s = activeTxt.styles // 遍历行和列 for(let i in s) { for (let j in s[i]) { s[i][j].fontSize = value // 针对每个字设置字号 } } activeTxt.dirty = true } canvas.renderAll() }</script>使用 isEditing 判断当前是否进入编辑状态,我是根据编辑状态下是否有选中文字来修改字号大小的。
最后有2层循环:
for(let i in s) { for (let j in s[i]) { s[i][j].fontSize = value }}第一层循环 i 是行数的遍历;第二层循环 j 是当前行的文字的遍历。
这么做是因为如果只使用 setSelectionStyles 设置字号大小是无法覆盖每个字本身被设置的 fontSize 。所以还是需要循环一遍,保证每个字都修改到位。
<br>
设置字号大小的例子还可以用在上标和下标里,比如 《Fabric.js 上标和下标的使用偏方》 一文中的用法。上标和小标适应用在合在数学公式和化学符号等场景。
<br>
<br>
代码仓库
<br>
<br>
推荐阅读
《Fabric.js 锁定背景图,不受缩放和拖拽的影响》点赞 + 关注 + 收藏 = 学会了
边栏推荐
- Data consistency between redis and database
- Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
- 腾讯云 TStor 统一存储通过信通院首批文件存储基础能力评测
- Design of non main lamp: how to make intelligent lighting more "intelligent"?
- 路由(二)
- 2022家用投影仪首选!当贝F5强悍音画效果带来极致视听体验
- Openharmony notes --------- (4)
- 途家木鸟美团夏日折扣对垒,门槛低就一定香吗?
- Who is better, Qianyuan projection Xiaoming Q1 pro or Jimi new play? Which configuration is higher than haqu K1?
- Dingtalk send message
猜你喜欢

MySQL45讲——学习极客时间MySQL实战45讲笔记—— 04 | 深入浅出索引(上)

Development and design of animation surrounding mall sales website based on php+mysql
![[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)](/img/de/7d70f513577e93f1bde1969935a29e.jpg)
[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)

瀏覽器驅動的下載

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

The use of TestNG, the testing framework (II): the use of TestNG XML

当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选

How kaggle uses utility script

快解析:轻松实现共享上网

Penetrate the remote connection database through the Intranet
随机推荐
当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选
Chinese science and technology from the Winter Olympics (III): the awakening and evolution of digital people
每日学习2
Whole house Wi Fi: a pain point that no one can solve?
C crystal report printing
【虹科技术分享】如何测试 DNS 服务器:DNS 性能和响应时间测试
Use of freemaker
uniapp自动化测试学习
数据湖(十一):Iceberg表数据组织与查询
Using computed in uni app solves the abnormal display of data () value in tab switching
uni-app中使用computed解决了tab切换中data()值显示的异常
MySQL 45 lecture - learning from the actual battle of geek time MySQL 45 Lecture Notes - 04 | easy to understand index (Part 1)
什么是 eRDMA?丨科普漫画图解
Basic knowledge of QT original code
docker mysql
How many knowledge points can a callable interface have?
Code implementation MNLM
Use of UIC in QT
Fabric.js 手动加粗文本iText
一般来讲,如果频繁出现inconsistent tab and space的报错