当前位置:网站首页>Fabric.js 動態設置字號大小
Fabric.js 動態設置字號大小
2022-06-11 09:11: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代碼倉庫
边栏推荐
- 19. 删除链表的倒数第 N 个结点
- Talk about how to customize data desensitization
- E. Zoom in and zoom out of X (operator overloading)
- 机器学习笔记 - 深度学习技巧备忘清单
- Textview text size auto fit and textview margin removal
- 206. 反转链表
- openstack详解(二十一)——Neutron组件安装与配置
- openstack详解(二十四)——Neutron服务注册
- Sword finger offer 18 Delete the node of the linked list
- 203. remove linked list elements
猜你喜欢

ArcGIS 10.9.1 geological and meteorological volume metadata processing and service publishing and calling

Kubelet error getting node help

OpenCV OAK-D-W广角相机测试

报错ModularNotFoundError: No module named ‘find_version’

Matlab learning 9- nonlinear sharpening filter for image processing
![[FAQ for novices on the road] about data visualization](/img/a1/d15e286c3c886a8d3a4ac3eb165748.png)
[FAQ for novices on the road] about data visualization

山东大学项目实训(四)—— 微信小程序扫描web端二维码实现web端登录
![[C language - function stack frame] analyze the whole process of function call from the perspective of disassembly](/img/c5/40ea5571f187e525b2310812ff2af8.png)
[C language - function stack frame] analyze the whole process of function call from the perspective of disassembly

MSF给正常程序添加后门

ArcGIS 10.9.1 地质、气象体元数据处理及服务发布调用
随机推荐
Design of wrist sphygmomanometer based on sic32f911ret6
What are the types of garment ERP system in the market?
MSF基于SMB的信息收集
Interview question 02.02 Return the penultimate node
Why is it difficult to implement informatization in manufacturing industry?
When the enterprise makes a decision, which part should lead the ERP project?
【新手上路常见问答】关于数据可视化
剑指 Offer 31. 栈的压入、弹出序列
SAP ABAP data types and data objects
Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC
报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s
OpenCV OAK相机对比及介绍
206. 反转链表
Kubelet error getting node help
MySQL啟動報錯“Bind on TCP/IP port: Address already in use”
2095. 删除链表的中间节点
Livedata and stateflow, which should I use?
[C language - function stack frame] analyze the whole process of function call from the perspective of disassembly
剑指 Offer II 036. 后缀表达式
Machine learning notes - convolutional neural network memo list