当前位置:网站首页>Fabric.js IText设置指定文字的颜色和背景色
Fabric.js IText设置指定文字的颜色和背景色
2022-07-02 05:08:00 【德育处主任】
本文简介
点赞 + 关注 + 收藏 = 学会了
<br>
IText
是 Fabric.js
提供的一个 可编辑文本 的元素。
要设置文字颜色,可以设置 fill
。
但 fill
会设置所有文字的颜色,如果你只想修改指定文字的颜色,只用 fill
就不是那么容易实现了。
本文要讲的就是 设置指定文字的颜色和背景色。
<br>
设置文字颜色或背景色,需要分情况讨论的:
- 全文设置
- 设置指定文字颜色(单行)
- 设置指定文字颜色(多行)
接下来就将上述情况逐一讲解。
<br>
<br>
起步
<canvas id="c" width="600" height="400" style="border: 1px solid #ccc;"></canvas><!-- 引入 Fabric.js --><script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/521/fabric.js"></script><script> // 初始化画布 const canvas = new fabric.Canvas('c') // 创建文本 const iText = new fabric.IText('hello world') // 将文本添加到画布里 canvas.add(iText)</script>
首先把 Fabric.js
引入,然后初始化画布。如果对这个概念不太熟的话,可以看看 Fabric.js 从入门到膨胀。
最后通过 new fabric.IText
创建一段文字添加到画布中。
<br><br>
全文设置
// 省略部分代码const iText = new fabric.IText('hello world', { fill: 'pink'})
fill
可以设置文字的填充颜色。在 Fabric.js
里是使用这个属性设置颜色的,和 css
设置文字颜色使用 color
不一样~
<br>
<br>
单行:设置指定文字颜色
const iText = new fabric.IText('hello world', { styles: { 0: { 1: { fill: '#f00' // 文字颜色,#f00是红色 } } }})
第一次看到上面的代码时我也觉得有点奇怪,后来仔细看了下才发现这样设计的用意。
styles
是一个对象。
styles: { // 设置样式 0: { // 第1行 1: { // 第2个字符 // 要设置的样式 } }}
上面这段代码是这个意思。行号和字符位置都是从0开始算起,有点像数组下标的意思。
我们这个例子只有1行,所以行号是0。
e
的下标是 1
。所以上面的代码就把 e
设置成红色了。其他字符还是默认的颜色。
<br>
<br>
多行:设置指定文字颜色
const iText = new fabric.IText('hello\nworld', { styles: { // 设置样式 0: { // 第1行 1: { fill: '#f00' // 文字颜色 } }, 1: { // 第2行 2: { fill: 'hotpink' } } }})
IText
的换行是用 \n
来表达的。
这个例子要 修改第1行第2个字符的文字颜色为红色,第2行第3个字符为亮粉色 。
从代码里的注释应该可以看得懂本次操作。
<br>
<br>
设置文字背景色
const iText = new fabric.IText('hello world', { styles: { 0: { 1: { textBackgroundColor: 'yellowgreen', // 背景色 } },})
和设置文字颜色的原理一样,只是把关键字改一改就行。
textBackgroundColor
翻译成中文就是文本背景色。
<br>
<br>
代码仓库
<br>
<br>
推荐阅读
点赞 + 关注 + 收藏 = 学会了
边栏推荐
- Feign realizes file uploading and downloading
- 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
- Analyze the space occupied by the table according to segments, clusters and pages
- 案例分享|智慧化的西部机场
- 國產全中文-自動化測試軟件Apifox
- 6.30年终小结,学生时代结束
- A new attribute value must be added to the entity entity class in the code, but there is no corresponding column in the database table
- Change deepin to Alibaba image source
- The reason why sizeof (ARR) / sizeof (arr[0]) is used in the function to calculate the length of the array is incorrect
- 06 装饰(Decorator)模式
猜你喜欢
将光盘中的cda保存到电脑中
CubeMx DMA笔记
Mathematical knowledge (Euler function)
Virtual machine installation deepin system
[common error] the DDR type of FPGA device is selected incorrectly
Orthogonal test method and function diagram method for test case design
数学知识(欧拉函数)
Rhcsa --- work on the fourth day
Super detailed pycharm tutorial
Knowledge arrangement about steam Education
随机推荐
[bus interface] Axi interface
Fasttext text text classification
DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
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
Mysql重点难题(2)汇总
10 minute quick start UI automation ----- puppeter
AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
Case sharing | intelligent Western Airport
Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
Win10 disk management compressed volume cannot be started
leetcode两数相加go实现
Cannot activate CONDA virtual environment in vscode
Beginner crawler - biqu Pavilion crawler
Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
Record my pytorch installation process and errors
Online incremental migration of DM database
How to configure PostgreSQL 12.9 to allow remote connections
Briefly introduce chown command
Common errors of dmrman offline backup