当前位置:网站首页>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>
推荐阅读
点赞 + 关注 + 收藏 = 学会了
边栏推荐
- MySQL 45 lecture - learning from the actual battle of geek time MySQL 45 Lecture Notes - 04 | easy to understand index (Part 1)
- docker mysql
- Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
- 没有从远程服务器‘‘映射到本地用户‘(null)/sa‘的远程用户‘sa‘及服务主密码解密错误的解决办法
- NLA自然语言分析,让数据分析更智能
- uni-app中使用computed解决了tab切换中data()值显示的异常
- Qt-制作一个简单的计算器-实现四则运算
- Use of swagger
- Slashgear shares 2021 life changing technology products, which are somewhat unexpected
- 路由(二)
猜你喜欢

SystemServer进程

Default slot, named slot, scope slot

2022 home projector preferred! Dangbei F5 brings the ultimate audio-visual experience with its powerful audio-visual effect

Selenium installing selenium in pycharm

Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022

Will your sleep service dream of the extra bookinfo on the service network

Who is better, Qianyuan projection Xiaoming Q1 pro or Jimi new play? Which configuration is higher than haqu K1?

Just 1000 fans, record it

The conference on the growth of all things was held in Hangzhou, and dangbei was selected into the top 100 list of future unicorns in China in 2022

《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持
随机推荐
Daily learning 2
提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
故事點 vs. 人天
Error: eacces: permission denied, access to "/usr/lib/node_modules"
2022家用投影仪首选!当贝F5强悍音画效果带来极致视听体验
Selenium, element operation and browser operation methods
Generally speaking, if the error of inconsistent tab and space occurs frequently
Golang 快速生成数据库表的 model 和 queryset
Codeforces Round #803 (Div. 2)(A~D)
篇9:XShell免费版安装
Data consistency between redis and database
The evolution process of the correct implementation principle of redis distributed lock and the summary of redison's actual combat
没有从远程服务器‘‘映射到本地用户‘(null)/sa‘的远程用户‘sa‘及服务主密码解密错误的解决办法
selenium 元素定位方法
Development skills of rxjs observable custom operator
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 04 | 深入浅出索引(上)
Qt如何设置固定大小
<口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
How to set QT manual layout
kaggle如何使用utility script