当前位置:网站首页>Fabric. JS iText set italics manually
Fabric. JS iText set italics manually
2022-07-02 05:18:00 【Director of Moral Education Department】
Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 29 God , Click to see the event details
Brief introduction
give the thumbs-up + Focus on + Collection = Learned to
<br>
Whether in the rich text editor or in Word in , There are “ Italics ” function . and Fabric.js No exception . This article mainly explains in Fabric.js Use in IText Italicize the created text .
These include :
- Set italics when creating text
- Let the user set italics manually
<br>
<br>
Initialize canvas
It is necessary to initialize the canvas , The content explained later will be developed based on the code in this section .

<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>The initialization code is very simple , If you don't understand the above code, you can take a look first 《Fabric.js From entry to expansion 》
<br>
<br>
Set italics when creating text
IText To set italics, use italic perhaps oblique These two keywords , You can use either , This article USES the italic Explain .
// Omitted code const iText = new fabric.IText('hello world', { fontStyle: 'italic'}) To set italics during initialization , Just put the fontStyle Set to italic perhaps oblique that will do .

<br>
The above code is italicized in full text , If you just want to set a character , You can use the following method
// Omitted code const iText = new fabric.IText('hello world', { styles: { 0: { 1: { fontStyle: 'italic' } } }})
I will “e” Set to Italic .
Use styles It can be set line by line and word by word .“e” Located at 1 Xing di 2 Characters , The rows and columns are calculated from Subscript 0 At the beginning , therefore “e” The position is 0-1 .
<br>
<br>
Set italics manually
Manually set the italic division 2 In this case :
- Full Text Italic / Restore the default
- The selected text is italicized / Restore the default
<br>
So that you can set it manually , I added a button to the page .
The operation is as shown in the figure

<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>The idea and steps of the above code are :
- 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 .
<br>
The above is the main content of this article .
<br>
<br>
Code warehouse
Fabric.js IText Set italics manually
<br>
<br>
Recommended reading
《Fabric.js Control element level 》
《Fabric.js Top line 、 Center line ( Delete line )、 Underline 》
《Fabric.js Activate the input box 》
《Fabric.js Output reduced JSON》
《Fabric.js Dynamically set the font size 》 give the thumbs-up + Focus on + Collection = Learned to
边栏推荐
- Paddlepaddle project source code
- el form 表单validate成功后没有执行逻辑
- 设置滚动条默认样式 谷歌浏览器
- Disable access to external entities in XML parsing
- ERP management system development and design existing source code
- National all Chinese Automatic Test Software apifox
- Fabric. JS compact JSON
- Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
- The reason why sizeof (ARR) / sizeof (arr[0]) is used in the function to calculate the length of the array is incorrect
- Domestic all Chinese automatic test software apifox
猜你喜欢

2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
![Gee series: unit 7 remote sensing image classification using GEE [random forest classification]](/img/01/ba9441b7b1efaed85c464316740edb.jpg)
Gee series: unit 7 remote sensing image classification using GEE [random forest classification]

Gee series: Unit 2 explore datasets

Disable access to external entities in XML parsing

About PROFIBUS: communication backbone network of production plant
![[opencv] image binarization](/img/7e/b56a59ffae3bf6cac9c0bb7e090b85.jpg)
[opencv] image binarization

Record my pytorch installation process and errors

CubeMx DMA笔记

Fabric.js 渐变

Black Horse Notes - - set Series Collection
随机推荐
Domestic all Chinese automatic test software apifox
Database batch insert data
2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
Pyechart1.19 national air quality exhibition
php/js cookie共享跨域的问题
Johnson–Lindenstrauss Lemma(2)
Fabric.js IText 上标和下标
Fabric. JS three methods of changing pictures (including changing pictures in the group and caching)
7.TCP的十一种状态集
Fabric. JS round brush
LeetCode 1175. 质数排列(质数判断+组合数学)
【pyinstaller】_get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists‘
Mapping settings in elk (8) es
Gee: use of common mask functions in remote sensing image processing [updatemask]
Gee: find the spatial distribution and corresponding time of the "greenest" in the Yellow River Basin in 2020 [pixel by pixel analysis]
ERP management system development and design existing source code
Global and Chinese market of insulin pens 2022-2028: Research Report on technology, participants, trends, market size and share
Gee series: unit 7 remote sensing image classification using GEE [random forest classification]
Global and Chinese market of cell culture freezers 2022-2028: Research Report on technology, participants, trends, market size and share
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas