当前位置:网站首页>Fabric.js 动态设置字号大小
Fabric.js 动态设置字号大小
2022-06-11 09:09:00 【InfoQ】
本文简介
Fabric.jsITextFabric.js
分析
- 文本被选中后才能修改字号
- 整体修改字号
- 修改被选中的几个字的字号,没被选中的不进行修改
- 获取被选中的文字:
canvas.getActiveObject()。
- 是否只选中一部分文字:
- 我通过编辑状态来判断是否只选中一部分文字:
isEditing。
- 修改被选中文字的样式:
setSelectionStyles({...})。
- 修改
fontSize属性。
- 如果是全文修改,还要判断是否有些字符在自身设置了
fontSize,如果文字自己设置了fontSize,那全文设置的权重没独立设置的那么高。
编码
<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>
isEditingfor(let i in s) {
for (let j in s[i]) {
s[i][j].fontSize = value
}
}
ijsetSelectionStylesfontSize代码仓库
边栏推荐
- Port occupancy problem, 10000 ports
- Machine learning notes - the story of master kaggle Janio Martinez Bachmann
- MySQL startup error "bind on tcp/ip port: address already in use"
- 【方案开发】红外体温计测温仪方案
- 20. 有效的括号
- Matlab learning 7- linear smoothing filtering of image processing
- How win10 Home Edition connects to remote desktop
- CUMT学习日记——ucosII理论解析—任哲版教材
- 203. remove linked list elements
- 83. 删除排序链表中的重复元素
猜你喜欢

【新手上路常见问答】关于数据可视化

MSF基于SMB的信息收集

Matlab learning 8- linear and nonlinear sharpening filtering and nonlinear smoothing filtering of image processing

Award winning survey streamnational sponsored 2022 Apache pulsar user questionnaire
![[FAQ for novices on the road] about data visualization](/img/a1/d15e286c3c886a8d3a4ac3eb165748.png)
[FAQ for novices on the road] about data visualization

Talk about reading the source code

机器学习笔记 - 使用TensorFlow的Spatial Transformer网络

山东大学项目实训(四)—— 微信小程序扫描web端二维码实现web端登录

Textview text size auto fit and textview margin removal

Install jupyter in the specified environment
随机推荐
[software] ten skills to maximize the value of ERP system
876. 链表的中间结点
实现边充边OTG的PD芯片GA670-10
[image processing] spatial domain image enhancement
[ERP system] how much do you know about the professional and technical evaluation?
ArcGIS 10.9.1 地质、气象体元数据处理及服务发布调用
剑指 Offer II 041. 滑动窗口的平均值
Printf correlation of C
[237. delete nodes in the linked list]
基于SIC32F911RET6设计的腕式血压计方案
86. 分隔链表
[C language - data storage] how is data stored in memory?
自动化运维文章大合集
kubelet Error getting node 问题求助
19. delete the penultimate node of the linked list
Matlab learning 9- nonlinear sharpening filter for image processing
Sword finger offer 18 Delete the node of the linked list
206. reverse linked list
1493. 删掉一个元素以后全为 1 的最长子数组
Talk about how to customize data desensitization