当前位置:网站首页>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
边栏推荐
- 案例分享|智慧化的西部机场
- Black Horse Notes - - set Series Collection
- No logic is executed after the El form is validated successfully
- 创新永不止步——nVisual网络可视化平台针对Excel导入的创新历程
- Pyechats 1.19 generate a web version of Baidu map
- Leetcode basic programming: array
- Gee: explore the characteristics of precipitation change in the Yellow River Basin in the past 10 years [pixel by pixel analysis]
- Feign realizes file uploading and downloading
- Gee dataset: chirps pentad high resolution global grid rainfall dataset
- [quick view opencv] familiar with CV matrix operation with image splicing examples (3)
猜你喜欢

Gee series: Unit 1 Introduction to Google Earth engine

Storage of data

Gee series: Unit 4 data import and export in Google Earth engine

Mysql基础---查询(1天学会mysql基础)
![Gee series: unit 8 time series analysis in Google Earth engine [time series]](/img/a6/648ff959af93c22dc8605215a90535.jpg)
Gee series: unit 8 time series analysis in Google Earth engine [time series]

Cubemx DMA notes

Nodejs (02) - built in module

How matlab marks' a 'in the figure and how matlab marks points and solid points in the figure

Black Horse Notes - - set Series Collection

Nodejs (03) -- custom module
随机推荐
Leetcode 18 problem [sum of four numbers] recursive solution
C case of communication between server and client based on mqttnet
Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
Creation and destruction of function stack frames
Detailed explanation of Pointer use
Collectors. Groupingby sort
Using QA band and bit mask in Google Earth engine
Gee: explore the characteristics of precipitation change in the Yellow River Basin in the past 10 years [pixel by pixel analysis]
【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘
Save the CDA from the disc to the computer
2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
Fabric.js 自由绘制矩形
Johnson–Lindenstrauss Lemma(2)
There are duplicate elements in leetcode. Go implementation
Online English teaching app open source platform (customized)
Fabric. JS 3 APIs to set canvas width and height
Fabric. JS round brush
黑马笔记---Set系列集合
画波形图_数字IC
Fabric.js 3个api设置画布宽高