如何使用 FabricJS 在线取消正在运行的动画?
在本教程中,我们将学习如何使用 FabricJS 取消在线运行的动画。线元素是 FabricJS 中提供的基本元素之一,用于创建直线。由于线元素在几何上是一维的并且没有内部,因此它们永远不会被填充。我们可以通过创建 fabric.Line 的实例、指定线的 X 和 Y 坐标并将其添加到画布来创建一个线对象。要取消正在运行的动画,我们使用 dispose 方法。
语法
dispose()
没有使用 dispose 方法
示例
让我们看一个代码示例,了解当没有使用 dispose 方法时线对象的动画如何工作的。在这里,我们创建了一个简单的动画,线对象完成一个完整的旋转并向外扩展。
<!DOCTYPE html> <html> <head> <!-- Adding the Fabric JS Library--> <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script> </head> <body> <h2>Without using dispose method</h2> <p>You can see the animation is happening properly</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiate a Line object var line = new fabric.Line([250, 100, 310, 100], { stroke: "blue", strokeWidth: 10, }); // Using the animate method line.animate("angle", "+=360", { onChange: canvas.renderAll.bind(canvas), duration: 1700, }); // Add it to the canvas canvas.add(line); </script> </body> </html>
使用 dispose 方法
示例
在此示例中,我们使用 dispose 方法来取消正在运行的动画。因此,将取消该线实例的所有正在运行的动画。
<!DOCTYPE html> <html> <head> <!-- Adding the Fabric JS Library--> <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script> </head> <body> <h2>Using the dispose method</h2> <p>You can see that the animation no longer happens</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiate a Line object var line = new fabric.Line([250, 100, 310, 100], { stroke: "blue", strokeWidth: 10, }); // Using the animate method line.animate("angle", "+=360", { onChange: canvas.renderAll.bind(canvas), duration: 1700, }); // Add it to the canvas canvas.add(line); // Using dispose method line.dispose() </script> </body> </html>
广告
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP