当前位置:网站首页>删除元素(带过渡动画)
删除元素(带过渡动画)
2022-07-02 11:08:00 【德育处主任】
本文简介
Fabric.js
提供了2个方法删除对象。
一个是 直接删除,另一个方法是 带过渡动画 删除元素(淡出动画)。
本文重点讲 带过渡动画 删除元素的方法,还会讲到设置 过渡时长 、删除后的 回调函数 等知识点。
<br>
<br>
相关API
删除元素的2种方法:
canvas.remove(...object)
canvas.fxRemove(object, callbacksopt)
设置动画时长:
canvas.FX_DURATION
<br>
canvas.remove
remove
很好理解,就是删除的意思。里面传入元素对象即可。
canvas.remove(object)
在 canvas.renderOnAddRemove
为 true
的情况下,使用 canvas.remove
删除元素后,画布会自动刷新。
canvas.renderOnAddRemove
默认值是 true
<br>
如果 canvas.renderOnAddRemove = false
,使用 canvas.remove
后,元素是删除了,但画布上还能看到元素。
此时需要手动执行 canvas.renderAll()
或者其他刷新方法,刚刚被删的元素才会从画布上(视觉上)消失。
<br>
canvas.fxRemove
fxRemove
是一个带过渡动画的删除方法,使用该方法删除的对象会淡出,直至消失。
这个方法 第一个参数 是要删除的对象;
第二个参数 是回调对象,这个对象里有2个值,是类型函数。
分别是 onChange
和 onComplete
。
onChange
:在动画的每一步调用onComplete
:删除成功后调用
<br>
canvas.fxRemove(object, { onChange() { console.log('在动画的每一帧调用') }, onComplete() { console.log('删除成功后调用') }})
<br>
canvas.FX_DURATION
canvas.FX_DURATION
可以设置过渡动画的时长。
默认值是 500,单位是 毫秒
canvas.FX_DURATION = 1500
<br>
修改后 canvas.FX_DURATION
后,再试试 canvas.fxRemove
,删除元素的过渡时长就是你设置的时长。
<br>
<br>
示例代码
<div class="btn-x"> <button onclick="delCircle()">删除元素(圆型)</button> <button onclick="delRect()">带动画删除元素(方形)</button></div><canvas id="canvasBox" width="600" height="600"></canvas> <script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/460/fabric.min.js"></script><script> let canvas = null let circle = null let rect = null window.onload = function() { // 使用 元素id 创建画布,此时可以在画布上框选 canvas = new fabric.Canvas('canvasBox', { width: 400, height: 400 }) // 圆形 circle = new fabric.Circle({ name: 'circle', top: 60, left: 60, radius: 30, // 圆的半径 30 fill: 'yellowgreen' }) // 矩形 rect = new fabric.Rect({ name: 'rect', top: 60, // 距离容器顶部 60px left: 200, // 距离容器左侧 200px fill: 'orange', // 填充a 橙色 width: 60, // 宽度 60px height: 60 // 高度 60px }) // 将矩形添加到画布中 canvas.add(circle, rect) } // 删除圆形(没动画) function delCircle() { canvas.remove(circle) } // 删除矩形(有动画) function delRect() { canvas.FX_DURATION = 1500 // 设置动画时长,默认500毫秒 canvas.fxRemove(rect, { onChange() { console.log('在动画的每一步调用') }, onComplete() { console.log('删除成功后调用') } }) }</script>
<br>
<br>
代码仓库
<br>
<br>
推荐阅读
《Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)》
<br>
如果本文内容对你有所帮助,也请你帮我点个赞呗 点赞 + 关注 + 收藏 = 学会了
边栏推荐
- Design and implementation of car query system based on php+mysql
- <口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
- YOLOv3&YOLOv5输出结果说明
- < schematic diagram of oral arithmetic exercise machine program development> oral arithmetic exercise machine / oral arithmetic treasure / children's math treasure / children's calculator LCD LCD driv
- Getting started with QT - making a simple calculator
- QT - make a simple calculator - realize four operations
- NLA自然语言分析实现数据分析零门槛
- P1042 [NOIP2003 普及组] 乒乓球
- SystemServer进程
- BeanUtils -- shallow copy -- example / principle
猜你喜欢
万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单
YOLOv3&YOLOv5输出结果说明
MySQL 45 lecture - learning from the actual battle of geek time MySQL 45 Lecture Notes - 04 | easy to understand index (Part 1)
SystemServer进程
关于Flink框架窗口(window)函数最全解析
千元投影小明Q1 Pro和极米NEW Play谁更好?和哈趣K1比哪款配置更高?
每日学习2
Codeforces Round #803 (Div. 2)(A~D)
selenium,元素操作以及浏览器操作方法
每日学习3
随机推荐
Who is better, Qianyuan projection Xiaoming Q1 pro or Jimi new play? Which configuration is higher than haqu K1?
测试框架TestNG的使用(二):testNG xml的使用
Codeforces Round #803 (Div. 2)(A~D)
自定义事件,全局事件总线,消息订阅与发布,$nextTick
Error: eacces: permission denied, access to "/usr/lib/node_modules"
Penetrate the remote connection database through the Intranet
Origin plots thermogravimetric TG and differential thermogravimetric DTG curves
[Hongke technology sharing] how to test DNS server: DNS performance and response time test
Mysql5.7 installation super easy tutorial
每日学习2
Subcontracting configuration of uniapp applet subpackages
Default slot, named slot, scope slot
Everyone believes that the one-stop credit platform makes the credit scenario "useful"
Use of freemaker
In 2021, the global styrene butadiene styrene (SBS) revenue was about $3722.7 million, and it is expected to reach $5679.6 million in 2028
[development environment] StarUML tool (download software | StarUML installation | StarUML creation project)
How many knowledge points can a callable interface have?
Dingtalk send message
Development and design of animation surrounding mall sales website based on php+mysql
Code implementation MNLM