当前位置:网站首页>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代碼倉庫
边栏推荐
- 203. 移除链表元素
- typescript高阶特性一 —— 合并类型(&)
- Success and failure of ERP software from the perspective of enterprise evaluation
- 面試題 02.02. 返回倒數第 k 個節點
- MySQL startup error "bind on tcp/ip port: address already in use"
- Leveldb simple use example
- SAP ABAP data types and data objects
- 机器学习笔记 - 使用TensorFlow的Spatial Transformer网络
- 2130. maximum twin sum of linked list
- openstack详解(二十二)——Neutron插件配置
猜你喜欢

shell脚本之sed详解 (sed命令 , sed -e , sed s/ new / old / ... )

ArcGIS 10.9.1 地质、气象体元数据处理及服务发布调用

Talk about how to customize data desensitization

openstack详解(二十三)——Neutron其他配置、数据库初始化与服务启动

typescript高阶特性一 —— 合并类型(&)

Which Apple devices support this system update? See if your old apple device supports the latest system

File system check of the root filesystem failed

Port occupancy problem, 10000 ports

Typescript high level feature 1 - merge type (&)

报错RuntimeError: BlobReader error: The version of imported blob doesn‘t match graph_transformer
随机推荐
SAP ABAP field symbol
[software] ERP model selection method for large enterprises
Exclusive interview with PMC member Liu Yu: female leadership in Apache pulsar community
报错RuntimeError: BlobReader error: The version of imported blob doesn‘t match graph_transformer
E. Zoom in and zoom out of X (operator overloading)
【软件】大企业ERP选型的方法
ArcGIS 10.9.1 地质、气象体元数据处理及服务发布调用
【方案开发】血压计方案压力传感器SIC160
Blinn Phong reflection model
远程办公最佳实践及策略
[FAQ for novices on the road] about data visualization
SAP ABAP data types and data objects
Award winning survey streamnational sponsored 2022 Apache pulsar user questionnaire
Which Apple devices support this system update? See if your old apple device supports the latest system
19. delete the penultimate node of the linked list
[scheme development] scheme of infrared thermometer
Pulsar job Plaza | Tencent, Huawei cloud, shrimp skin, Zhong'an insurance, streamnational and other hot jobs
Matlab learning 8- linear and nonlinear sharpening filtering and nonlinear smoothing filtering of image processing
报错Output image is bigger(1228800B) than maximum frame size specified in properties(1048576B)
How win10 Home Edition connects to remote desktop