当前位置:网站首页>Fabric. JS iText sets the color and background color of the specified text
Fabric. JS iText sets the color and background color of the specified text
2022-07-02 05:18:00 【Director of Moral Education Department】
Brief introduction
give the thumbs-up + Focus on + Collection = Learned to
<br>
IText
yes Fabric.js
One of the Editable text The elements of .
To set the text color , You can set fill
.
but fill
Will set the color of all text , If you only want to change the color of the specified text , Only fill
It is not so easy to realize .
This article is about Set the specified text color and background color .
<br>
Set the text color or background color , It needs to be discussed according to the situation :
- Full text settings
- Sets the specified text color ( A single )
- Sets the specified text color ( Multiple lines )
Next, I will explain the above situations one by one .
<br>
<br>
start
<canvas id="c" width="600" height="400" style="border: 1px solid #ccc;"></canvas><!-- introduce Fabric.js --><script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/521/fabric.js"></script><script> // Initialize canvas const canvas = new fabric.Canvas('c') // Create text const iText = new fabric.IText('hello world') // Add text to the canvas canvas.add(iText)</script>
First turn on the Fabric.js
introduce , Then initialize the canvas . If you are not familiar with this concept , You can see Fabric.js From entry to expansion .
Finally through new fabric.IText
Create a piece of text and add it to the canvas .
<br><br>
Full text settings
// Omitted code const iText = new fabric.IText('hello world', { fill: 'pink'})
fill
You can set the fill color of the text . stay Fabric.js
This attribute is used to set the color in , and css
Set the text color to use color
Dissimilarity ~
<br>
<br>
A single : Sets the specified text color
const iText = new fabric.IText('hello world', { styles: { 0: { 1: { fill: '#f00' // Text color ,#f00 It's red } } }})
When I first saw the above code, I also felt a little strange , After a careful look, I found the intention of this design .
styles
It's an object .
styles: { // Set the style 0: { // The first 1 That's ok 1: { // The first 2 Characters // The style to set } }}
The above code means . The line number and character position are from 0 From the beginning , A bit like array subscript .
Our example only has 1 That's ok , So the line number is 0.
e
The subscript of is 1
. So the code above puts e
Set to red . Other characters are the default colors .
<br>
<br>
Multiple lines : Sets the specified text color
const iText = new fabric.IText('hello\nworld', { styles: { // Set the style 0: { // The first 1 That's ok 1: { fill: '#f00' // Text color } }, 1: { // The first 2 That's ok 2: { fill: 'hotpink' } } }})
IText
The newline of is \n
To express the .
This example should Amendment No 1 Xing di 2 The text color of characters is red , The first 2 Xing di 3 Characters are bright pink .
You can understand this operation from the comments in the code .
<br>
<br>
Set the text background color
const iText = new fabric.IText('hello world', { styles: { 0: { 1: { textBackgroundColor: 'yellowgreen', // Background color } },})
The same principle as setting the text color , Just change the keyword .
textBackgroundColor
Translated into Chinese is the text background color .
<br>
<br>
Code warehouse
Fabric Set up IText Specify character color and background color
<br>
<br>
Recommended reading
《Fabric.js How to use the brush ?》
《 pure CSS Red brick background wall 》
《Fabric.js Draw ellipses freely 》
give the thumbs-up + Focus on + Collection = Learned to
边栏推荐
- Gee series: unit 8 time series analysis in Google Earth engine [time series]
- el form 表单validate成功后没有执行逻辑
- Two implementation methods of delay queue
- Simple and practical accounting software, so that accounts can be checked
- Global and Chinese market of travel data recorder (VDR) 2022-2028: Research Report on technology, participants, trends, market size and share
- CubeMx DMA笔记
- Global and Chinese market of commercial fish tanks 2022-2028: Research Report on technology, participants, trends, market size and share
- 黑馬筆記---Set系列集合
- Mathematical knowledge (Euler function)
- Fabric.js 基础笔刷
猜你喜欢
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
Gee: create a new feature and set corresponding attributes
Pyechats 1.19 generate a web version of Baidu map
Using QA band and bit mask in Google Earth engine
The El cascader echo only selects the questions that are not displayed
Gee series: unit 7 remote sensing image classification using GEE [random forest classification]
视差特效的原理和实现方法
Preparation for writing SAP ui5 applications using typescript
el form 表单validate成功后没有执行逻辑
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
随机推荐
创新永不止步——nVisual网络可视化平台针对Excel导入的创新历程
Fabric.js 渐变
7.1模擬賽總結
Collectors. Groupingby sort
Exercise notes 13 (effective letter ectopic words)
C # picture display occupancy problem
Black Horse Notes - - set Series Collection
Principle and implementation of parallax effect
Disable access to external entities in XML parsing
案例分享|智慧化的西部机场
Gee: explore the change of water area in the North Canal basin over the past 30 years [year by year]
leetcode两数相加go实现
Basic differences between Oracle and MySQL (entry level)
Pyechart1.19 national air quality exhibition
CubeMx DMA笔记
Ansible installation and use
Using QA band and bit mask in Google Earth engine
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
Save the CDA from the disc to the computer
Global and Chinese market of travel data recorder (VDR) 2022-2028: Research Report on technology, participants, trends, market size and share