当前位置:网站首页>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
边栏推荐
- 國產全中文-自動化測試軟件Apifox
- Fabric. JS compact JSON
- 黑马笔记---Set系列集合
- Gee series: Unit 5 remote sensing image preprocessing [GEE grid preprocessing]
- Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
- 案例分享|智慧化的西部机场
- Disable access to external entities in XML parsing
- National all Chinese Automatic Test Software apifox
- Fabric.js 3个api设置画布宽高
- Fabric.js IText 手动设置斜体
猜你喜欢

LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)

Fabric. JS upload local image to canvas background
![[common error] the DDR type of FPGA device is selected incorrectly](/img/f3/be66bcfafeed581add6d48654dfe34.jpg)
[common error] the DDR type of FPGA device is selected incorrectly

Ansible installation and use

Fabric.js 激活输入框

Fabric.js 右键菜单

Nodejs (02) - built in module

Collectors. Groupingby sort

Paddlepaddle project source code

6.网络-基础
随机推荐
Gee: explore the characteristics of precipitation change in the Yellow River Basin in the past 10 years [pixel by pixel analysis]
黑马笔记---Set系列集合
Simple and practical accounting software, so that accounts can be checked
Exercise notes 13 (effective letter ectopic words)
Creation and destruction of function stack frames
Gee series: Unit 1 Introduction to Google Earth engine
[quick view opencv] familiar with CV matrix operation with image splicing examples (3)
fastText文本分类
Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
Pyechats 1.19 generate a web version of Baidu map
Map in JS (including leetcode examples)
Collectors. Groupingby sort
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
Find the subscript with and as the target from the array
The El cascader echo only selects the questions that are not displayed
Collectors.groupingBy 排序
延时队列两种实现方式
Splice characters in {{}}
Fabric.js 背景不受视口变换影响
Fasttext text text classification