如何在 FabricJS 中設定線條動畫的持續時間?
在本教程中,我們將學習如何在 FabricJS 中設定線條動畫的持續時間。線條元素是 FabricJS 提供的基本元素之一,用於建立直線。由於線條元素在幾何上是一維的,並且不包含內部,因此它們永遠不會被填充。我們可以透過建立 `fabric.Line` 的例項,指定線條的 x 和 y 座標並將其新增到畫布上來建立線條物件。為了更改動畫文字的持續時間,我們使用 `duration` 屬性。
語法
animate(property: String | Object, value: Number | Object)
引數
屬性 − 此屬性接受字串或物件值,用於確定要動畫化哪些屬性。
值 − 此屬性接受數字或物件值,用於確定要動畫化屬性的值。
選項鍵
duration − 此屬性接受一個數字。它可以用來更改動畫的持續時間。預設值為 500 毫秒。
使用duration屬性的預設值
示例
讓我們來看一個程式碼示例,看看當 `animate` 方法與 `duration` 屬性的預設值一起使用時,我們的線條物件是什麼樣子。在這種情況下,左側動畫的持續時間為 500 毫秒。
<!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 default value of duration property</h2> <p>You can see that the left animation happens for 500ms</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([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, }); // Using the animate method line.animate("left", "+=100", { onChange: canvas.renderAll.bind(canvas), duration: 500, }); line.animate("angle", "90", { onChange: canvas.renderAll.bind(canvas), }); // Add it to the canvas canvas.add(line); </script> </body> </html>
使用duration屬性的自定義值
示例
在這個例子中,我們將看到如何透過使用 `animate` 方法和 `duration` 屬性來控制動畫持續的時間。在這裡,我們指定持續時間為 1000 毫秒,這意味著我們的左側動畫將持續 1 秒。
<!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 a custom value for duration property</h2> <p>You can see that the left animation happens for 1000ms</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([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, }); // Using the animate method line.animate("left", "+=100", { onChange: canvas.renderAll.bind(canvas), duration: 1000, }); line.animate("angle", "90", { onChange: canvas.renderAll.bind(canvas), }); // Add it to the canvas canvas.add(line); </script> </body> </html>
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP