当前位置:网站首页>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>
推荐阅读
点赞 + 关注 + 收藏 = 学会了
边栏推荐
- 培养中小学生对教育机器人的热爱之心
- DMA Porter
- About PROFIBUS: communication backbone network of production plant
- Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
- [Yu Yue education] autumn 2021 reference materials of Tongji University
- Feign realizes file uploading and downloading
- Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
- LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)
- Oracle stored procedure and job task setting
- Implementation of go language for deleting duplicate items in sorting array
猜你喜欢

Go Chan's underlying principles

Preparation for writing SAP ui5 applications using typescript

Here comes the chicken soup! Keep this quick guide for data analysts

Virtual machine installation deepin system

Orthogonal test method and function diagram method for test case design

C# 基于MQTTNet的服务端与客户端通信案例
![[opencv] image binarization](/img/7e/b56a59ffae3bf6cac9c0bb7e090b85.jpg)
[opencv] image binarization

How to configure PostgreSQL 12.9 to allow remote connections

数学问题(数论)试除法做质数的判断、分解质因数,筛质数

农业生态领域智能机器人的应用
随机推荐
Getting started with pytest ----- confitest Application of PY
Super detailed pycharm tutorial
Beginner crawler - biqu Pavilion crawler
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
Paddlepaddle project source code
TypeScript类的使用
Mapping location after kotlin confusion
将光盘中的cda保存到电脑中
国产全中文-自动化测试软件Apifox
Implementation of go language for deleting duplicate items in sorting array
函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
Mouse events in JS
fastText文本分类
Case sharing | intelligent Western Airport
Pytest learning ----- pytest assertion of interface automation testing
解析少儿编程中的动手搭建教程
How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
初学爬虫-笔趣阁爬虫
Video multiple effects production, fade in effect and border background are added at the same time
Leetcode 18 problem [sum of four numbers] recursive solution