当前位置:网站首页>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>
推荐阅读
点赞 + 关注 + 收藏 = 学会了
边栏推荐
- Introduction to Luogu 3 [circular structure] problem list solution
- Fabric.js IText 上标和下标
- Use of typescript classes
- Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
- js中的Map(含leetcode例题)
- LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
- Map in JS (including leetcode examples)
- 農業生態領域智能機器人的應用
- 2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
- Future trend of automated testing ----- self healing technology
猜你喜欢
2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
How to recover deleted data in disk
Fabric.js IText设置指定文字的颜色和背景色
Dark horse notes -- Set Series Collection
Simple and practical accounting software, so that accounts can be checked
Cultivate primary and secondary school students' love for educational robots
Knowledge arrangement about steam Education
LeetCode 1175. 质数排列(质数判断+组合数学)
Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
Black Horse Notes - - set Series Collection
随机推荐
Dark horse notes -- Set Series Collection
[common error] the DDR type of FPGA device is selected incorrectly
农业生态领域智能机器人的应用
Detailed process of DC-1 range construction and penetration practice (DC range Series)
CubeMx DMA笔记
How to make an RPM file
The El cascader echo only selects the questions that are not displayed
案例分享|智慧化的西部机场
Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
Case sharing | intelligent Western Airport
LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
Pyechart1.19 national air quality exhibition
Cultivate primary and secondary school students' love for educational robots
VMware installation win10 reports an error: operating system not found
How to configure PostgreSQL 12.9 to allow remote connections
leetcode存在重复元素go实现
AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
Orthogonal test method and function diagram method for test case design
Preparation for writing SAP ui5 applications using typescript
Use of typescript classes