当前位置:网站首页>Fabric. JS iText set italics manually
Fabric. JS iText set italics manually
2022-06-22 02:17:00 【InfoQ】
Brief introduction
Fabric.jsFabric.jsIText- Set italics when creating text
- Let the user set italics manually
Initialize canvas

<canvas id="c" width="300" height="300" style="border: 1px solid #ccc"></canvas>
<!-- introduce fabric.js -->
<script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/521/fabric.js"></script>
<script>
const canvas = new fabric.Canvas('c') // Create a canvas , And bind elements
const iText = new fabric.IText('hello world') // Create text
canvas.add(iText) // Add text to the canvas
</script>
Set italics when creating text
ITextitalicoblique italic// Omitted code
const iText = new fabric.IText('hello world', {
fontStyle: 'italic'
})
fontStyleitalicoblique
// Omitted code
const iText = new fabric.IText('hello world', {
styles: {
0: {
1: {
fontStyle: 'italic'
}
}
}
})

styles Subscript 00-1Set italics manually
- Full Text Italic / Restore the default
- The selected text is italicized / Restore the default

<button onclick="italic()"> Italics </button>
<canvas id="c" width="300" height="300" style="border: 1px solid #ccc"></canvas>
<!-- introduce fabric.js -->
<script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/521/fabric.js"></script>
<script>
const canvas = new fabric.Canvas('c') // Create a canvas , And bind elements
const iText = new fabric.IText('hello world') // Create text
canvas.add(iText) // Add text to the canvas
function italic() {
let activeTxt = canvas.getActiveObject()
if (!activeTxt) return
if (activeTxt.isEditing) {
// Edit state , Italicize the selected text or restore the default operation
const state = activeTxt.getSelectionStyles().find(item => item.fontStyle !== 'italic')
if (!state || (JSON.stringify(state) === '{}' && activeTxt['fontStyle'] === 'italic')) {
activeTxt.setSelectionStyles({ 'fontStyle': 'normal' })
} else {
activeTxt.setSelectionStyles({ 'fontStyle': 'italic' })
}
} else {
// Selection status , Italicize the full text or restore the default operation
if (activeTxt['fontStyle'] === 'italic') {
activeTxt.fontStyle = 'normal'
let s = activeTxt.styles
for(let i in s) {
for (let j in s[i]) {
s[i][j].fontStyle = 'normal'
}
}
} else {
activeTxt.fontStyle = 'italic'
let s = activeTxt.styles
for(let i in s) {
for (let j in s[i]) {
s[i][j].fontStyle = 'italic'
}
}
}
}
canvas.renderAll()
}
</script>
- adopt
canvas.getActiveObject()Method to get the currently selected object .
- If no text is currently selected , No operation .
- adopt
isEditingProperty to detect whether the text is in the editing state .
- In editing state , Italicize the selected text or restore the default operation .
- In the non editing state , Italicize the full text or restore the default operation .
Code warehouse
边栏推荐
- Intel history overview
- Atguigu---- collect form data
- 小孩子学什么编程?
- [Chapter 14 image compression and reconstruction based on principal component analysis -- matlab deep learning practical case]
- Software Test Engineer Interview interface test FAQ
- Intel written test questions DIY
- Minecraft 1.18.2 biochemical 8 module version 1.3 3D objects + more complex villages
- Illumination dependent shader
- Error reporting and table layout of latex on &
- Shell script syntax overview
猜你喜欢

Pdf to word PDF to picture picture to PDF modifying PDF files is as convenient as operating word (Introduction to acrobat DC)

手机app测试方法

excel常用快捷键excel快捷键汇总

1277_ Implementation analysis of vtaskdelay in FreeRTOS

Show you how to distinguish several kinds of parallelism

Atguigu---- list rendering

MySQL 递归查找树形结构,这个方法太实用了!

The "cloud" end of the 6th world intelligence conference will be held soon

微信小程序影視評論交流平臺系統畢業設計畢設(4)開題報告
![[chapter 06 MATLAB realizes lung cancer diagnosis based on watershed segmentation]](/img/2f/b2f141bf2f0b1f2f80444d37529a9b.png)
[chapter 06 MATLAB realizes lung cancer diagnosis based on watershed segmentation]
随机推荐
[Chapter 13 image compression and reconstruction based on Hoffman -- image processing application of MATLAB deep learning practice]
论文笔记: 多标签学习 ACkEL
MySQL 递归查找树形结构,这个方法太实用了!
Shell脚本语法概览
rt_ Thread thread management
rt_ Message queue of thread
Cmake common command category notes
GoLand启动时闪退
Appium interview questions
微信小程序影视评论交流平台系统毕业设计毕设(6)开题答辩PPT
PostgreSQL fetches data according to the size of the time field
Show you how to distinguish several kinds of parallelism
Chapter 19 signal lamp image simulation control technology based on speech recognition
Mysql database easy learning 06 - commonly used by data analysts: single table query of data query language DQL
Mba-day24 best value problem
Word document to markdown document?
Using OKR for HR digital transformation
使用 OKR 進行 HR 數字化轉型
Machine learning compilation lesson 1: overview of machine learning compilation
What is a neural network