当前位置:网站首页>Fabric. JS dynamically set font size
Fabric. JS dynamically set font size
2022-06-11 09:11:00 【InfoQ】
Brief introduction
Fabric.jsITextFabric.js
analysis
- The font size can be modified only after the text is selected
- Overall modification of font size
- Modify the font size of the selected words , If it is not selected, it will not be modified
- Get the selected text :
canvas.getActiveObject().
- Whether to select only part of the text :
- I use the editing status to determine whether only part of the text is selected :
isEditing.
- Change the style of the selected text :
setSelectionStyles({...}).
- modify
fontSizeattribute .
- If it is a full text modification , It is also necessary to determine whether some characters are set in themselves
fontSize, If the text is set by itselffontSize, The weight set in the full text is not as high as that set independently .
code
<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) {
// Edit state
activeTxt.setSelectionStyles({ 'fontSize': value})
} else {
activeTxt.fontSize = value
let s = activeTxt.styles
// Traversing rows and columns
for(let i in s) {
for (let j in s[i]) {
s[i][j].fontSize = value // Set the font size for each word
}
}
activeTxt.dirty = true
}
canvas.renderAll()
}
</script>
isEditingfor(let i in s) {
for (let j in s[i]) {
s[i][j].fontSize = value
}
}
ijsetSelectionStylesfontSizeCode warehouse
边栏推荐
- 报错ModularNotFoundError: No module named ‘find_version’
- [C language - function stack frame] analyze the whole process of function call from the perspective of disassembly
- Sword finger offer 06 Print linked list from end to end
- Why is the string class final decorated
- 山东大学增强现实实验四
- C language printing heart
- Pulsar job Plaza | Tencent, Huawei cloud, shrimp skin, Zhong'an insurance, streamnational and other hot jobs
- 【芯片方案】红外人体测温仪方案设计
- SAP ABAP data types and data objects
- Exclusive interview with PMC member Liu Yu: female leadership in Apache pulsar community
猜你喜欢

Complexity analysis of matrix inversion operation (complexity analysis of inverse matrix)

Install jupyter in the specified environment

Machine learning notes - spatial transformer network using tensorflow
![[image processing] spatial domain image enhancement](/img/d9/6afee1006570651ccf93ed8da95dd3.png)
[image processing] spatial domain image enhancement

Exclusive interview with PMC member Liu Yu: female leadership in Apache pulsar community

Pulsar job Plaza | Tencent, Huawei cloud, shrimp skin, Zhong'an insurance, streamnational and other hot jobs

Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC

C language printing diamond

Machine learning notes - convolutional neural network memo list

Redis source code analysis hash object (z\u hash)
随机推荐
Blinn Phong reflection model
Why is it difficult to implement informatization in manufacturing industry?
企业需要考虑的远程办公相关问题
Machine learning notes - the story of master kaggle Janio Martinez Bachmann
Erreur de démarrage MySQL "BIND on TCP / IP Port: Address already in use"
Sword finger offer II 036 Postfix Expression
1400. 构造 K 个回文字符串
移动端页面使用rem来做适配
How win10 Home Edition connects to remote desktop
PHP solves Chinese display garbled code
[FAQ for novices on the road] about data visualization
Award winning survey streamnational sponsored 2022 Apache pulsar user questionnaire
报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s
C language printing heart
Question d'entrevue 02.02. Renvoie l'avant - dernier noeud K
剑指 Offer II 036. 后缀表达式
File system check of the root filesystem failed
Clothing ERP: how do enterprises carry out implementation planning?
MSF给正常程序添加后门
Sword finger offer 31 Stack push and pop sequence