当前位置:网站首页>Fabric.js IText 上标和下标
Fabric.js IText 上标和下标
2022-07-02 05:08:00 【德育处主任】
本文简介
点赞 + 关注 + 收藏 = 学会了
<br>
对于数学化学这方面的项目,“上标” 和 “下标” 功能是很常用的,比如 次方 、化学元素 等表达方式,都需要用到上标或者下标。
<br>
在 Fabric.js 中,文本元素有 Text 、IText 和 Textbox 。本文主要讲解 IText 的上标和下标如何实现。在 Text 和 Textbox 中的实现方式也是一样的。
<br>
<br>
上标

const iText = new fabric.IText('32=9', { styles: { 0: { // 第1行 1: { // 第2个字符 deltaY: -14, // 向下偏移 fontSize: 24, // 设置字体大小 } }})本例展示的是 3的平方等于9,要设置 deltaY 为负值。
deltaY 的作用是定义文本基线,基线向上移动就设置正值,向下移动就设置负值,默认是0 。
styles 的设置比较特殊,要针对某个字的话,需要通过行号和字符下标来定位。
行号和字符下标都是从0开始,和 js 的数组一样。
IText 换行是使用 \n 来操作。
本例只有1行,所以行号为0。
2 所在的位置下标是 1。
如果对这方面设置不太理解的话,可以看看 Fabric.js IText设置指定字符颜色和背景色。
<br>
<br>
下标

const iText = new fabric.IText('H2O', { styles: { 0: { 1: { deltaY: 0, fontSize: 24 } } }})在本例中,下标我并不打算向下偏移,因为这样看起来不是那么好看,所以我只是设置了 fontSize 为24,将字号改小一点了。
<br>
<br>
总结
其实本文设置上标和下标的方法都是一样的,主要流程是以下3步:
- 通过行号和文字下标找到对应的字符
- 通过
deltaY设置指定字符的文本基线 - 修改
fontSize,让指定字符的字号比其他字符小一点
完成以上3步就能实现上标和下标的功能。
<br>
除了上述方式,其实还有另一种方式设置上标和下标的,但那种方式的应用场景稍微有点不同,下一篇再说说那种方法。
<br>
<br>
源码仓库
<br>
<br>
推荐阅读
点赞 + 关注 + 收藏 = 学会了
边栏推荐
- Future trend of automated testing ----- self healing technology
- Leetcode basic programming: array
- 洛谷入门3【循环结构】题单题解
- LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
- Leetcode 18 problem [sum of four numbers] recursive solution
- DMA Porter
- Briefly introduce chown command
- 从数组中找出和为目标的下标
- Rhcsa --- work on the fourth day
- Cubemx DMA notes
猜你喜欢

Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy

Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster

C# 基于MQTTNet的服务端与客户端通信案例

Pyechats 1.19 generate a web version of Baidu map

Mathematical knowledge (Euler function)

Getting started with pytest ----- confitest Application of PY

VMware installation win10 reports an error: operating system not found

10 minute quick start UI automation ----- puppeter

C case of communication between server and client based on mqttnet

数学问题(数论)试除法做质数的判断、分解质因数,筛质数
随机推荐
Summary of main account information of zhengdaliu 4
How to configure PostgreSQL 12.9 to allow remote connections
The reason why sizeof (ARR) / sizeof (arr[0]) is used in the function to calculate the length of the array is incorrect
Domestic all Chinese automatic test software apifox
DMA Porter
The underlying principle of go map (storage and capacity expansion)
go实现leetcode旋转数组
函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
Summary of database problems
Go GC garbage collection notes (three color mark)
Cannot activate CONDA virtual environment in vscode
Comp 250 parsing
Pyechats 1.19 generate a web version of Baidu map
Fasttext text text classification
Detailed process of DC-1 range construction and penetration practice (DC range Series)
Application d'un robot intelligent dans le domaine de l'agroécologie
Starting from the classification of database, I understand the map database
Win10 disk management compressed volume cannot be started
洛谷入门3【循环结构】题单题解
Essence and physical meaning of convolution (deep and brief understanding)