当前位置:网站首页>Fabric.js 手动加粗文本iText
Fabric.js 手动加粗文本iText
2022-07-02 11:08:00 【德育处主任】
本文简介
点赞 + 关注 + 收藏 = 学会了
<br>
本文介绍 Fabric.js
的 IText
在画布上如何让用户手动加粗文本。
效果如图所示:
<br>
要实现2种操作
- 全文加粗
- 只加粗选中的文字
<br>
如果你还不清楚 Fabric.js
有什么用,我强烈推荐你阅读 《Fabric.js 从入门到目中无人》 。该文能让你的前端技术再点亮一个技能点。
<br>
<br>
全文加粗
只需把 IText
的 fontWeight
属性设置成 bold
即可实现加粗。
如果想变回默认样式,可以将 fontWeight
设为 normal
。
修改完文字样式后,需要执行 canvas.renderAll()
重新渲染一下画布内容。
<button onclick="bold()">加粗</button><canvas id="c2" width="250" height="300" style="border: 1px solid #ccc"></canvas><!-- 引入fabric.js --><script src="js/fabric.js"></script><script> // 创建画布 const canvas = new fabric.Canvas('c2') // 创建文本 const iText = new fabric.IText('hello world') canvas.add(iText) function bold() { // 当前选中的元素 let activeTxt = canvas.getActiveObject() // 只有选中文本才执行以下操作 if (activeTxt) { activeTxt.fontWeight = 'bold' // 将字体加粗 canvas.renderAll() // 重新渲染画布 } }</script>
<br>
<br>
加粗选中的文字
可以用 setSelectionStyles
设置被选中的文字样式,里面传一个样式对象即可。
<button onclick="bold()">加粗</button><canvas id="c2" width="250" height="300" style="border: 1px solid #ccc"></canvas><!-- 引入fabric.js --><script src="js/fabric.js"></script><script> // 创建画布 const canvas = new fabric.Canvas('c2') // 创建文本 const canvas = new fabric.Canvas('c2') // 创建文本 const iText = new fabric.IText('hello world') canvas.add(iText) function bold() { // 当前选中的元素 let activeTxt = canvas.getActiveObject() if (activeTxt) { activeTxt.setSelectionStyles({ 'fontWeight': 'bold' }) // 设置选中文字的样式 canvas.renderAll() // 重新渲染画布 } }</script>
<br>
<br>
仓库
<br>
<br>
推荐阅读
文章 | 简介 |
---|---|
《Fabric.js 橡皮擦的用法(包含恢复功能)》 | 橡皮擦需要下载一个工具包,详情可参考该文。 |
《Fabric.js 基础画笔的用法 BaseBrush》 | 画笔的基础用法 |
《Fabric.js 自由绘制圆形》 | 将“框选”动作改造成自由绘制圆形 |
《Fabric.js 3个api设置画布宽高》 | 宽高设置并不是在初始化是才能进行的,本文介绍3种方法设置画布宽高,让你的画布更容易适配不同的使用场景 |
《Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)》 | 如果你的项目需要动态更换画布上的图片,那我也给你总结了3中方法 |
《Fabric.js 摆正元素的4种方法(带过渡动画)》 | 一键摆正被你旋转过的元素 |
《Fabric.js 将本地图像上传到画布背景》 | 除了在初始化时设置画布背景外,我还做了本地上传背景的功能,让画布在运行时也能修改背景图 |
《在 Vue3中使用Fabric.js实现渐变(Gradient)效果,包括径向渐变radial》 | 官方入门教程也只有线性渐变,以至于某些文章说 Fabric.js 只支持线性渐变。但其实径向渐变也完全支持 |
《Fabric.js 从入门到目中无人》 | Fabric.js 入门指南,学完能应付简单业务 |
《Fabric.js 右键菜单》 | Fabric.js 暂时还没右键事件,如果你想实现右键菜单的功能,可直接复制该文章的代码~ |
点赞 + 关注 + 收藏 = 学会了
边栏推荐
- freemarker的使用
- YOLOv3&YOLOv5输出结果说明
- PyQt5_QScrollArea内容保存成图片
- Some interview suggestions for Android programmers "suggestions collection"
- 没有从远程服务器‘‘映射到本地用户‘(null)/sa‘的远程用户‘sa‘及服务主密码解密错误的解决办法
- The global special paper revenue in 2021 was about $27 million, and it is expected to reach $35 million in 2028. From 2022 to 2028, the CAGR was 3.8%
- Daily learning 2
- BeanUtils -- shallow copy -- example / principle
- Quarkus learning IV - project development to deployment
- Dangbei projection 4K laser projection X3 Pro received unanimous praise: 10000 yuan projector preferred
猜你喜欢
[to be continued] [UE4 notes] l5ue4 model import
c# 水晶报表打印
自定义事件,全局事件总线,消息订阅与发布,$nextTick
Whole house Wi Fi: a pain point that no one can solve?
Tencent cloud tstor unified storage passed the evaluation of the first batch of basic file storage capabilities of the ICT Institute
QT new project_ MyNotepad++
万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单
Use of swagger
Simple introduction to ENSP
[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)
随机推荐
Daily learning 2
Everyone believes that the one-stop credit platform makes the credit scenario "useful"
Dangbei projection 4K laser projection X3 Pro received unanimous praise: 10000 yuan projector preferred
Launcher startup process
selenium 元素定位方法
Tencent cloud tstor unified storage passed the evaluation of the first batch of basic file storage capabilities of the ICT Institute
Quarkus学习四 - 项目开发到部署
抓包工具fiddler学习
Data consistency between redis and database
路由(二)
如何设置Qt手工布局
Start to write a small demo - three piece chess
瀏覽器驅動的下載
MySQL 45 lecture - learning the actual battle of MySQL in Geek time 45 Lecture Notes - 05 | easy to understand index (Part 2)
Daily learning 3
关于Flink框架窗口(window)函数最全解析
腾讯云 TStor 统一存储通过信通院首批文件存储基础能力评测
Talk about idempotent design
The global special paper revenue in 2021 was about $27 million, and it is expected to reach $35 million in 2028. From 2022 to 2028, the CAGR was 3.8%
数据湖(十一):Iceberg表数据组织与查询