当前位置:网站首页>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>
推荐阅读
点赞 + 关注 + 收藏 = 学会了
边栏推荐
- JS interview collection test question 1
- Pyechats 1.19 generate a web version of Baidu map
- Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
- Pyechart1.19 national air quality exhibition
- 函数栈帧的创建和销毁
- Go implements leetcode rotation array
- 数学知识(欧拉函数)
- Comp 250 parsing
- LM09丨费雪逆变换反转网格策略
- Embedded-c language-9-makefile/ structure / Consortium
猜你喜欢

Differential identities (help find mean, variance, and other moments)

农业生态领域智能机器人的应用

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

Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market

数据库问题汇总

Rhcsa --- work on the fourth day

LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)

Johnson–Lindenstrauss Lemma(2)

Rhcsa --- work on the third day

DMA Porter
随机推荐
[bus interface] Axi interface
Johnson–Lindenstrauss Lemma(2)
画波形图_数字IC
fastText文本分类
List of common bugs in software testing
6.30 year end summary, end of student age
Super detailed pycharm tutorial
4. Flask cooperates with a tag to link internal routes
ansible安装与使用
Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
Summary of common string processing functions in C language
Cubemx DMA notes
Set the default style of scroll bar Google browser
[Yu Yue education] autumn 2021 reference materials of Tongji University
LM09丨费雪逆变换反转网格策略
初学爬虫-笔趣阁爬虫
Exercise notes 13 (effective letter ectopic words)
Domestic all Chinese automatic test software apifox
國產全中文-自動化測試軟件Apifox
Future trend of automated testing ----- self healing technology