使用 FabricJS 為多邊形物件新增收縮和展開動畫
我們可以透過建立fabric.Polygon的例項來建立一個多邊形物件。多邊形物件可以由任何由一組連線的直線段組成的封閉形狀來表徵。由於它是 FabricJS 的基本元素之一,因此我們還可以透過應用角度、不透明度等屬性輕鬆地對其進行自定義。為了新增收縮和展開動畫,我們可以結合使用scaleX和scaleY屬性以及animate方法。
語法
animate(property: String | Object, value: Number | Object): fabric.Object | fabric.AnimationContext | Array.<fabric.AnimationContext>
引數
property − 此屬性接受字串或物件值,用於確定我們要動畫化的屬性。
value − 此屬性接受數字或物件值,用於確定要為屬性設定動畫的值。
選項鍵
scaleX:此屬性接受數字值。分配的值確定水平物件縮放因子。其預設值為 1。
scaleY:此屬性接受數字值。分配的值確定垂直物件縮放因子。其預設值為 1。
示例 1:為多邊形新增收縮動畫
讓我們看一個程式碼示例,瞭解如何使用 animate 方法新增收縮動畫。我們將 scaleX 和 scaleY 屬性傳遞值 0.5,這使得多邊形在水平和垂直方向上都縮小到其原始大小的一半。
<!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>Adding shrink animation to the polygon</h2>
<p>You can see that shrink animation has been added to the polygon</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 polygon object
var polygon = new fabric.Polygon(
[
{ x: 60, y: 0 },
{ x: 60, y: 60 },
{ x: 0, y: 60 },
{ x: 0, y: 0 },
],
{
fill: "#ffe4e1",
stroke: "green",
strokeWidth: 5,
top: 90,
left: 100,
}
);
// Adding it to the canvas
canvas.add(polygon);
// Using the animate method and passing scaleX property
polygon.animate("scaleX", "0.5", {
onChange: canvas.renderAll.bind(canvas),
easing: fabric.util.ease.easeInCubic,
duration: 1000,
});
// Using the animate method and passing scaleY property
polygon.animate("scaleY", "0.5", {
onChange: canvas.renderAll.bind(canvas),
easing: fabric.util.ease.easeInCubic,
duration: 1000,
});
</script>
</body>
</html>
示例 2:為多邊形新增展開動畫
在此示例中,我們將瞭解如何使用animate方法新增展開動畫。由於我們將 scaleX 和 scaleY 屬性傳遞值 1.5,因此多邊形物件將在水平和垂直方向上縮放 1.5 倍。
<!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>Adding expand animation to the polygon</h2>
<p>You can see that expand animation has been added to the polygon</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 polygon object
var polygon = new fabric.Polygon(
[
{ x: 60, y: 0 },
{ x: 60, y: 60 },
{ x: 0, y: 60 },
{ x: 0, y: 0 },
],
{
fill: "#ffe4e1",
stroke: "green",
strokeWidth: 5,
top: 90,
left: 100,
}
);
// Adding it to the canvas
canvas.add(polygon);
// Using the animate method and passing scaleX property
polygon.animate("scaleX", "1.5", {
onChange: canvas.renderAll.bind(canvas),
easing: fabric.util.ease.easeInCubic,
duration: 1000,
});
// Using the animate method and passing scaleY property
polygon.animate("scaleY", "1.5", {
onChange: canvas.renderAll.bind(canvas),
easing: fabric.util.ease.easeInCubic,
duration: 1000,
});
</script>
</body>
</html>
結論
在本教程中,我們使用兩個簡單的示例演示瞭如何使用 FabricJS 為多邊形物件新增收縮和展開動畫。
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP