当前位置:网站首页>Fabric.js 元素被选中时保持原有层级
Fabric.js 元素被选中时保持原有层级
2022-07-02 11:08:00 【德育处主任】
本文简介
点赞 + 关注 + 收藏 = 学会了
<br>
不知道你在使用 Fabric.js
时有没有发现,如果一个画布存在多个元素,而且它们之前是层叠的,当你选中底层的元素时,那个元素会跳到最顶层;当你释放该元素后,它又跑回原有层级。这是 Fabric.js
的默认状态。
如果你不希望操作时出现这种情况,你希望在元素被选中时还保持它原有的层级,那么我推荐你在初始化画布时,设置 preserveObjectStacking
为 true
。
<br>
接下来我将举例说明该属性的效果和用法。
<br>
<br>
默认情况
默认情况是被选中的元素会跑到最顶层,当它被释放后又跑回原来的层级。
<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') // 圆形 circle = new fabric.Circle({ name: 'circle', top: 60, left: 60, radius: 30, // 圆的半径 30 fill: 'yellowgreen' }) // 矩形 rect = new fabric.Rect({ name: 'rect', top: 30, // 距离容器顶部 60px left: 100, // 距离容器左侧 200px fill: 'orange', // 填充a 橙色 width: 60, // 宽度 60px height: 60 // 高度 60px }) // 将矩形添加到画布中 canvas.add(circle, rect)</script>
上面的代码,我创建了一个绿色圆形和一个橙色矩形。
在 canvas.add(circle, rect)
中,先添加圆形,再添加矩形,所以矩形的层级会比圆形高。
我故意调整了两个图形的位置,让它们有一部分是重叠起来的。
所以最终出来的效果是圆形在矩形下面。
<br>
Fabric.js
默认情况下,被选中的元素会跳到顶层,所以你可以看看上图的操作效果。
<br>
<br>
保持原有层级的情况
如果你不想按照默认的情况来操作,尤其是画布中对象比较多的时候,希望被操作的对象一直保持在原有的层级,这样操作起来某些情况下会更直观。那么你可以在初始化画布时将 preserveObjectStacking
设为 true
// 省略部分代码const canvas = new fabric.Canvas('canvasBox', { // 元素对象被选中时保持在当前z轴,不会跳到最顶层 preserveObjectStacking: true // 默认false})
<br>
<br>
代码仓库
<br>
<br>
推荐阅读
点赞 + 关注 + 收藏 = 学会了
边栏推荐
- MySQL45讲——学习极客时间MySQL实战45讲笔记—— 05 | 深入浅出索引(下)
- Data Lake (11): Iceberg table data organization and query
- 《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持
- 浏览器驱动的下载
- Thymeleaf dependency
- PyQt5_ Qscrollarea content is saved as a picture
- Convolutional neural network (Introduction)
- Federated Search: all requirements in search
- Qt入门-制作一个简易的计算器
- Qt原代码基本知识
猜你喜欢
[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)
Who is better, Qianyuan projection Xiaoming Q1 pro or Jimi new play? Which configuration is higher than haqu K1?
MySQL 45 lecture - learning from the actual battle of geek time MySQL 45 Lecture Notes - 04 | easy to understand index (Part 1)
PHP linked list creation and traversal
Daily learning 2
测试框架TestNG的使用(二):testNG xml的使用
提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
自定义事件,全局事件总线,消息订阅与发布,$nextTick
HMS core machine learning service helps zaful users to shop conveniently
Custom events, global event bus, message subscription and publishing, $nexttick
随机推荐
QT - make a simple calculator - realize four operations
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 05 | 深入浅出索引(下)
Code implementation MNLM
MySQL -- convert rownum in Oracle to MySQL
P3008 [usaco11jan]roads and planes g (SPFA + SLF optimization)
2022 home projector preferred! Dangbei F5 brings the ultimate audio-visual experience with its powerful audio-visual effect
< schéma de développement de la machine d'exercice oral > machine d'exercice oral / trésor d'exercice oral / trésor de mathématiques pour enfants / lecteur LCD de calculatrice pour enfants IC - vk1621
Qt-制作一个简单的计算器-实现四则运算
Qt原代码基本知识
The evolution process of the correct implementation principle of redis distributed lock and the summary of redison's actual combat
Selenium element positioning method
Word frequency statistics & sorting
PHP linked list creation and traversal
Launcher启动过程
Qt-制作一个简单的计算器-实现四则运算-将结果以对话框的形式弹出来
Story point vs. Human Sky
Tencent cloud tstor unified storage passed the evaluation of the first batch of basic file storage capabilities of the ICT Institute
Use of swagger
Stone merging Board [interval DP] (ordinary stone Merging & Ring Stone merging)
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 04 | 深入浅出索引(上)