当前位置:网站首页>Fabric.js 元素被选中时保持原有层级
Fabric.js 元素被选中时保持原有层级
2022-06-10 16:03:00 【德育处主任】
不知道你在使用 Fabric.js 时有没有发现,如果一个画布存在多个元素,而且它们之前是层叠的,当你选中底层的元素时,那个元素会跳到最顶层;当你释放该元素后,它又跑回原有层级。这是 Fabric.js 的默认状态。
如果你不希望操作时出现这种情况,你希望在元素被选中时还保持它原有的层级,那么我推荐你在初始化画布时,设置 preserveObjectStacking 为 true 。
接下来我将举例说明该属性的效果和用法。
默认情况
默认情况是被选中的元素会跑到最顶层,当它被释放后又跑回原来的层级。
<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) 中,先添加圆形,再添加矩形,所以矩形的层级会比圆形高。
我故意调整了两个图形的位置,让它们有一部分是重叠起来的。
所以最终出来的效果是圆形在矩形下面。
Fabric.js 默认情况下,被选中的元素会跳到顶层,所以你可以看看上图的操作效果。
保持原有层级的情况
如果你不想按照默认的情况来操作,尤其是画布中对象比较多的时候,希望被操作的对象一直保持在原有的层级,这样操作起来某些情况下会更直观。那么你可以在初始化画布时将 preserveObjectStacking 设为 true
// 省略部分代码
const canvas = new fabric.Canvas('canvasBox', {
// 元素对象被选中时保持在当前z轴,不会跳到最顶层
preserveObjectStacking: true // 默认false
})
复制代码代码仓库
边栏推荐
- Docker安装Redis镜像详细步骤(简单易懂,适合新手快速上手)
- Flood control and drainage monitoring automatic monitoring and telemetering terminal for flood control and drainage
- postman常用断言
- VBA divides strings, stores them in an array, writes them to a file, and saves them
- Effect comparison and code implementation of three time series hybrid modeling methods
- 靠,嘉立创打板又降价
- 简易的站点备份 Shell 脚本
- 嵌入式开发:使用MCU进行无线更新面临的5大挑战
- Postman parameterization
- ahk函数命令大全
猜你喜欢

Research Report on the application field of Chinese antirust oil market and the prospect planning of the 14th five year plan (2022-2028)

Detailed steps for installing redis image in docker (easy to understand, suitable for novices to get started quickly)

Li Ling: in six years, how did I go from open source Xiaobai to Apache top project PMC

Li Ling: in six years, how did I go from open source Xiaobai to Apache top project PMC

互联网企业与芯片

Do you know the five GoLand shortcuts to improve efficiency?
![Basic settings of pycharm [detailed explanation with pictures and words]](/img/1b/2b8275bc75bea84b74251f3de05701.jpg)
Basic settings of pycharm [detailed explanation with pictures and words]

全链路追踪 & 性能监控工具 SkyWalking 实战

Basic use cases for jedis

Calling subroutines from other modules in VBA - call a subroutine from a different module in VBA
随机推荐
防洪排涝监控 防洪排涝自动监测遥测终端
Nat. Rev. Drug Discov. | AI在小分子药物发现中的应用:一个即将到来的浪潮?
Digital image processing: graying
AI video cloud: a good wife in the era of we media
Research Report on development potential analysis and investment development strategy of global and Chinese hydraulic oil industry (2022-2028)
Rethinking atlas revolution for semantic image segmentation (deeplobv3) personal summary
看先进科技如何改变人类生活
Li Ling: in six years, how did I go from open source Xiaobai to Apache top project PMC
Palm detection and finger counting based on OpenCV
隐私计算一体机的应用落地指南——《隐私计算一体机金融应用技术要求》正式发布,助力金融行业数据有序共享
Pytorch Foundation (I) -- anaconda and pytorch installation
简单实现文件上传
Hidden Markov model and its training (1)
解决idea打开某个项目卡住的问题
Webdypro layout control cannot be used_ SAP LIUMENG
PyTorch基础(一)-- Anaconda 和 PyTorch安装
MFC基础知识与课程设计思路
为 Chocolatey 设置代理
VBA judge whether the file exists and ask for the idea of file backup
Shit, jialichuang's price is reduced again