当前位置:网站首页>Fabric.js IText 手动设置斜体
Fabric.js IText 手动设置斜体
2022-07-02 05:08:00 【德育处主任】
持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第29天,点击查看活动详情
本文简介
点赞 + 关注 + 收藏 = 学会了
<br>
不管是在富文本编辑器还是在Word里,都有“斜体”功能。而 Fabric.js 也不例外。本文主要讲解在 Fabric.js 中使用 IText 创建的文本进行斜体设置。
其中包括:
- 在创建文字时就设置斜体
- 让用户手动设置斜体
<br>
<br>
初始化画布
初始化画布是必须的,之后讲解的内容都会基于这小节的代码进行开发。

<canvas id="c" width="300" height="300" 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 从入门到膨胀》
<br>
<br>
创建文本时设置斜体
IText 要设置斜体可以用 italic 或者 oblique 这两个关键字,用哪个都行,本文使用 italic 进行讲解。
// 省略部分代码const iText = new fabric.IText('hello world', { fontStyle: 'italic'})要在初始化时设置斜体,只需将 fontStyle 设置成 italic 或者 oblique 即可。

<br>
上面的代码是全文进行斜体设置的,如果你只是想设置某个字符,可以使用下面的方法
// 省略部分代码const iText = new fabric.IText('hello world', { styles: { 0: { 1: { fontStyle: 'italic' } } }})
我将 “e” 设置成斜体。
使用 styles 可以逐行逐字进行设置。“e” 位于第1行第2个字符,而计算行和列是从下标0 开始的,所以 “e” 的位置是 0-1 。
<br>
<br>
手动设置斜体
手动设置斜体分2种情况:
- 全文斜体 / 恢复默认
- 被选中的文本进行斜体 / 恢复默认
<br>
为了可以手动设置,我在页面上添加了一个按钮。
操作如图所示

<button onclick="italic()">斜体</button><canvas id="c" width="300" height="300" 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) // 将文本添加进画布中 function italic() { let activeTxt = canvas.getActiveObject() if (!activeTxt) return if (activeTxt.isEditing) { // 编辑状态,将被选中的文本进行斜体或恢复默认的操作 const state = activeTxt.getSelectionStyles().find(item => item.fontStyle !== 'italic') if (!state || (JSON.stringify(state) === '{}' && activeTxt['fontStyle'] === 'italic')) { activeTxt.setSelectionStyles({ 'fontStyle': 'normal' }) } else { activeTxt.setSelectionStyles({ 'fontStyle': 'italic' }) } } else { // 选择状态,全文进行斜体或恢复默认操作 if (activeTxt['fontStyle'] === 'italic') { activeTxt.fontStyle = 'normal' let s = activeTxt.styles for(let i in s) { for (let j in s[i]) { s[i][j].fontStyle = 'normal' } } } else { activeTxt.fontStyle = 'italic' let s = activeTxt.styles for(let i in s) { for (let j in s[i]) { s[i][j].fontStyle = 'italic' } } } } canvas.renderAll() }</script>以上代码的思路和步骤是:
- 通过
canvas.getActiveObject()方法获取当前被选中的对象。 - 如果当前没选中任何文本,就不做操作。
- 通过
isEditing属性检测文本是否处于编辑状态。 - 编辑状态下,将被选中的文本进行斜体或恢复默认的操作。
- 非编辑状态下,全文进行斜体或恢复默认操作。
<br>
以上就是本文主要想讲解的内容。
<br>
<br>
代码仓库
<br>
<br>
推荐阅读
《Fabric.js 动态设置字号大小》点赞 + 关注 + 收藏 = 学会了
边栏推荐
- 6.30年终小结,学生时代结束
- C # picture display occupancy problem
- Getting started with pytest ----- confitest Application of PY
- 删除排序数组中的重复项go语言实现
- Essence and physical meaning of convolution (deep and brief understanding)
- Typescript function details
- Application of intelligent robot in agricultural ecology
- DJB Hash
- 2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
- Video multiple effects production, fade in effect and border background are added at the same time
猜你喜欢

黑马笔记---Set系列集合

Summary of main account information of zhengdaliu 4

LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
![[common error] the DDR type of FPGA device is selected incorrectly](/img/f3/be66bcfafeed581add6d48654dfe34.jpg)
[common error] the DDR type of FPGA device is selected incorrectly

Dark horse notes -- map set system

2022-003arts: recursive routine of binary tree

Rhcsa --- work on the fourth day

4. Flask cooperates with a tag to link internal routes

DMA Porter

Dark horse notes -- Set Series Collection
随机推荐
Summary of MySQL key challenges (2)
Mysql重点难题(2)汇总
国产全中文-自动化测试软件Apifox
10 minute quick start UI automation ----- puppeter
Go Chan's underlying principles
7.1模擬賽總結
Go implements leetcode rotation array
Leetcode basic programming: array
Save the CDA from the disc to the computer
黑马笔记---Map集合体系
Record my pytorch installation process and errors
案例分享|智慧化的西部机场
國產全中文-自動化測試軟件Apifox
Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
Steam教育的实际问题解决能力
Learn BeanShell before you dare to say you know JMeter
List of common bugs in software testing
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
el form 表单validate成功后没有执行逻辑
LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)