当前位置:网站首页>Fabric.js 激活输入框
Fabric.js 激活输入框
2022-07-02 05:08:00 【德育处主任】
本文简介
点赞 + 关注 + 收藏 = 学会了
<br>
在 HTML
中,input
输入框有2种方法自动聚焦,最简单的是在 input
标签上添加 autofocus
属性。
再次点的方法是使用 JS
获取输入框元素,再调用 focus
事件选中输入框。
在 Fabric.js
中,IText
组件也提供了类似的方法。
<br>
<br>
实操
Fabric.js
的 IText
是一个可编辑文本框,可以在项目运行时输入文字,功能其实和 HTML
的 input
差不多,都是可以让用户输入。
<br>
默认情况
在 Fabric.js
中,如果在创建 IText
时没有添加文字,在画布上你是很难用肉眼观察出你的输入框在哪的。
<canvas id="canvasBox" width="600" height="600" 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('canvasBox') const iText = new fabric.IText('', { left: 100, // 输入框离画布左侧距离 top: 100, // 输入框离画布顶部距离 padding: 7 // 设置输入框内边距 }) canvas.add(iText) // 将输入框添加到画布中</script>
此时画布和输入框是被创建出来了,但凭肉眼是很难找到输入框在哪。
<br>
激活输入框
想要解决上述问题,可以激活输入框。
// 省略部分代码iText.enterEditing()
用 enterEditing()
方法可以激活输入框。此时可以看到光标所在的位置一闪一闪的。
<br>
再进一步
使用 enterEditing()
后输入框是激活了,也能看到光标一闪一闪。
但这就完了吗?
我们还可以再进一步,配合 Fabric Canvas
的 setActiveObject()
方法,激活被选中的对象。
这样选中输入框的效果会更加明显。
// 省略部分代码canvas.add(iText).setActiveObject(iText)iText.enterEditing()
从上图可以对比出,加上 setActiveObject()
后,呈现出来的效果更加明显了。
<br>
<br>
总结
Fabric.js
其实并不难,它比起直接使用原生 Canvas
要简单多了。
使用 Fabric.js
最主要是多看文档,然后把多个 api
结合起来使用,就能创造出多种效果~
<br>
<br>
代码仓库
<br>
<br>
推荐阅读
点赞 + 关注 + 收藏 = 学会了
边栏推荐
- Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
- paddle: ValueError:quality setting only supported for ‘jpeg‘ compression
- Here comes the chicken soup! Keep this quick guide for data analysts
- Summary of MySQL key challenges (2)
- Win10 disk management compressed volume cannot be started
- Line by line explanation of yolox source code of anchor free series network (7) -- obj in head_ loss、Cls_ Loss and reg_ Calculation and reverse transmission of loss I
- Application d'un robot intelligent dans le domaine de l'agroécologie
- 农业生态领域智能机器人的应用
- Fabric.js IText 上标和下标
- Pytest learning ----- pytest assertion of interface automation testing
猜你喜欢
Hcip day 17
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
数学知识——快速幂的理解及例题
Getting started with pytest ----- confitest Application of PY
Save the CDA from the disc to the computer
Change deepin to Alibaba image source
C# 基于MQTTNet的服务端与客户端通信案例
Collectors. Groupingby sort
DMA Porter
CubeMx DMA笔记
随机推荐
國產全中文-自動化測試軟件Apifox
How to make an RPM file
Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
Solution of DM database unable to open graphical interface
画波形图_数字IC
Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy
黑馬筆記---Set系列集合
Black Horse Notes - - set Series Collection
Win10 disk management compressed volume cannot be started
How to configure PostgreSQL 12.9 to allow remote connections
Leetcode18题 【四数之和】递归解法
leetcode存在重复元素go实现
删除排序数组中的重复项go语言实现
Case sharing | intelligent Western Airport
C# 图片显示占用问题
Dark horse notes -- Set Series Collection
Super detailed pycharm tutorial
Mapping settings in elk (8) es
[opencv] image binarization
06 decorator mode