当前位置:网站首页>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>
推荐阅读
点赞 + 关注 + 收藏 = 学会了
边栏推荐
- C# 基于MQTTNet的服务端与客户端通信案例
- Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
- TypeScript函数详解
- Solution: the agent throws an exception error
- 06 decorator mode
- Practical problem solving ability of steam Education
- Rhcsa --- work on the third day
- Summary of common string processing functions in C language
- Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
- 农业生态领域智能机器人的应用
猜你喜欢
LM09丨费雪逆变换反转网格策略
Future trend of automated testing ----- self healing technology
2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
Collectors.groupingBy 排序
Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
Johnson–Lindenstrauss Lemma(2)
VMware installation win10 reports an error: operating system not found
Rhcsa --- work on the fourth day
Summary of database problems
数学知识——快速幂的理解及例题
随机推荐
Embedded-c language-9-makefile/ structure / Consortium
从数组中找出和为目标的下标
C case of communication between server and client based on mqttnet
6.30 year end summary, end of student age
7.1模拟赛总结
Summary of database problems
[common error] the DDR type of FPGA device is selected incorrectly
面试会问的 Promise.all()
How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
js面试收藏试题1
4. Flask cooperates with a tag to link internal routes
Paddlepaddle project source code
函数栈帧的创建和销毁
奠定少儿编程成为基础学科的原理
Leetcode basic programming: array
Basic differences between Oracle and MySQL (entry level)
2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
Orthogonal test method and function diagram method for test case design
Typescript function details
農業生態領域智能機器人的應用