当前位置:网站首页>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代碼倉庫
边栏推荐
- openstack详解(二十二)——Neutron插件配置
- 远程办公最佳实践及策略
- 基于SIC32F911RET6设计的腕式血压计方案
- 市场上的服装ERP体系到底是哪些类型?
- SQL basic query
- 实现边充边OTG的PD芯片GA670-10
- Livedata and stateflow, which should I use?
- Pulsar job Plaza | Tencent, Huawei cloud, shrimp skin, Zhong'an insurance, streamnational and other hot jobs
- 876. 链表的中间结点
- 报错ModularNotFoundError: No module named ‘find_version’
猜你喜欢

Some learning records I=

报错RuntimeError: BlobReader error: The version of imported blob doesn‘t match graph_transformer

Matlab learning 7- linear smoothing filtering of image processing

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

Kubelet error getting node help

Textview text size auto fit and textview margin removal

Talk about reading the source code

Port occupancy problem, 10000 ports

Type-C蓝牙音箱单口可充可OTG方案

Android interview transcript (carefully sorted out)
随机推荐
SQL basic query
远程办公最佳实践及策略
Matlab learning 8- linear and nonlinear sharpening filtering and nonlinear smoothing filtering of image processing
2130. 链表最大孪生和
SAP ABAP field symbol
[C language - data storage] how is data stored in memory?
1493. 删掉一个元素以后全为 1 的最长子数组
Complexity analysis of matrix inversion operation (complexity analysis of inverse matrix)
Sword finger offer 31 Stack push and pop sequence
Intelligent control theory question bank
机器学习笔记 - 深度学习技巧备忘清单
Sword finger offer II 036 Postfix Expression
剑指 Offer 06. 从尾到头打印链表
Type-C蓝牙音箱单口可充可OTG方案
OpenCV CEO教你用OAK(五):基于OAK-D和DepthAI的反欺骗人脸识别系统
multiplication table
Blinn Phong reflection model
445. 两数相加 II
制作业信息化为什么难施行?
1721. exchange nodes in the linked list