当前位置:网站首页>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
isEditing
Property 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
边栏推荐
- Dark horse notes -- Set Series Collection
- 案例分享|智慧化的西部机场
- 7.1 simulation summary
- 6.网络-基础
- Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
- Foreign trade marketing website system development function case making
- fastText文本分类
- 运维工作的“本手、妙手、俗手”
- Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
- Black Horse Notes - - set Series Collection
猜你喜欢
Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
Disable access to external entities in XML parsing
No logic is executed after the El form is validated successfully
Fabric.js IText 手动设置斜体
MySQL foundation --- query (learn MySQL foundation in 1 day)
数据的储存
Fabric.js 居中元素
Mathematical knowledge (Euler function)
Johnson–Lindenstrauss Lemma(2)
Fabric.js 渐变
随机推荐
Database batch insert data
在{{}}中拼接字符
Gee: create a new feature and set corresponding attributes
案例分享|智慧化的西部机场
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
Fabric. JS upload local image to canvas background
Gee: remote sensing image composite and mosaic
leetcode两数相加go实现
Feign realizes file uploading and downloading
Fabric.js 渐变
Preparation for writing SAP ui5 applications using typescript
The El cascader echo only selects the questions that are not displayed
数据库批量插入数据
[quick view opencv] familiar with CV matrix operation with image splicing examples (3)
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
设置滚动条默认样式 谷歌浏览器
LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
Online English teaching app open source platform (customized)
运维工作的“本手、妙手、俗手”