如何在 FabricJS 中設定文字動畫的持續時間?
在本教程中,我們將學習如何使用 FabricJS 設定動畫文字的持續時間。我們可以透過新增 fabric.Text 例項來在畫布上顯示文字。它不僅允許我們移動、縮放和更改文字的尺寸,還提供額外的功能,例如文字對齊、文字修飾、行高,這些功能可以透過 textAlign、underline 和 lineHeight 屬性分別獲得。同樣,我們也可以使用 duration 屬性更改動畫文字的持續時間。
語法
animate(property: String | Object, value: Number | Object, { duration: Number })
引數
屬性 (property) − 此屬性接受字串 (String) 或物件 (Object) 值,用於確定我們要動畫化的屬性。
值 (value) − 此屬性接受數字 (Number) 或物件 (Object) 值,用於確定要動畫化屬性的值。
選項鍵
持續時間 (duration) − 此屬性接受數字 (Number)。它可以用來更改動畫的持續時間。預設值為 500 毫秒。
示例 1
使用 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 skewY animation occurs 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 text object var text = new fabric.Text("ADD SAMPLE
text here.", { width: 300, left: 60, top: 70, fill: "#ccccff", stroke: "black", strokeWidth: 2, fontSize: 50, }); // Using the animate method text.animate("skewY", "-=10", { onChange: canvas.renderAll.bind(canvas), duration: 500, }); // Add it to the canvas canvas.add(text); </script> </body> </html>
示例 2
使用 animate 方法並傳遞 duration 選項
在這個例子中,我們將看到如何透過使用 animate 屬性以及 duration 選項來控制動畫的持續時間。在本例中,我們將持續時間設定為 2000 毫秒,這就是為什麼 skewY 動畫持續 2000 毫秒。
<!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 animate method and passing the duration option</h2> <p>You can see that the skewY animation occurs for 2000ms</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 text object var text = new fabric.Text("ADD SAMPLE
text here.", { width: 300, left: 60, top: 70, fill: "#ccccff", stroke: "black", strokeWidth: 2, fontSize: 50 }); // Using the animate method text.animate('skewY', '-=10', { onChange: canvas.renderAll.bind(canvas), duration: 2000 }); // Add it to the canvas canvas.add(text); </script> </body> </html>
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP