当前位置:网站首页>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
边栏推荐
- Global and Chinese market of insulin pens 2022-2028: Research Report on technology, participants, trends, market size and share
- Map in JS (including leetcode examples)
- Simple and practical accounting software, so that accounts can be checked
- MySQL foundation --- query (learn MySQL foundation in 1 day)
- Mapping settings in elk (8) es
- Fabric. JS 3 APIs to set canvas width and height
- LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
- Gee series: Unit 2 explore datasets
- Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
- Record my pytorch installation process and errors
猜你喜欢
Storage of data
Mathematical knowledge (Euler function)
Simple and practical accounting software, so that accounts can be checked
LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
Gee series: unit 6 building various remote sensing indexes in Google Earth engine
Mysql基础---查询(1天学会mysql基础)
Differential identities (help find mean, variance, and other moments)
7. Eleven state sets of TCP
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
摆正元素(带过渡动画)
随机推荐
Cubemx DMA notes
Fabric.js IText 上标和下标
设置滚动条默认样式 谷歌浏览器
Collectors. Groupingby sort
Mathematical knowledge -- understanding and examples of fast power
Gee series: unit 7 remote sensing image classification using GEE [random forest classification]
Summary of MySQL key challenges (2)
Map in JS (including leetcode examples)
Using QA band and bit mask in Google Earth engine
How matlab marks' a 'in the figure and how matlab marks points and solid points in the figure
函数栈帧的创建和销毁
There are duplicate elements in leetcode. Go implementation
Fabric.js 激活输入框
Detailed explanation of Pointer use
C case of communication between server and client based on mqttnet
Line by line explanation of yolox source code of anchor free series network (7) -- obj in head_ loss、Cls_ Loss and reg_ Calculation and reverse transmission of loss I
ERP management system development and design existing source code
从数组中找出和为目标的下标
paddle: ValueError:quality setting only supported for ‘jpeg‘ compression
No logic is executed after the El form is validated successfully