如何使用 FabricJS 為折線新增進入和退出動畫?
我們可以透過建立`fabric.Polyline`的例項來建立一個折線物件。折線物件可以由一組連線的直線段來表徵。由於它是 FabricJS 的基本元素之一,我們也可以透過應用角度、不透明度等屬性來輕鬆地自定義它。
為了新增進入和退出動畫,我們可以結合使用`left`屬性和`animate`方法。
語法
animate(property: String | Object, value: Number | Object): fabric.Object | fabric.AnimationContext | Array.<fabric.AnimationContext>
引數
property − 此屬性接受字串或物件值,用於確定要動畫化的屬性。
value − 此屬性接受數字或物件值,用於確定要動畫化的屬性值。
選項鍵
left − 此屬性接受一個數字,允許我們控制物件在畫布左側的位置。
示例 1:為折線新增進入動畫
讓我們來看一個程式碼示例,瞭解如何使用`animate`方法和`left`屬性新增進入動畫。為了建立進入效果,我們需要將`left`從“-50”設定為“200”。由於我們將`duration`屬性的值設定為 5000,因此此動畫將持續 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 transition-in animation to the polyline</h2>
<p>You can see the transition-in effect has been added to the polyline</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 polyline object
var polyline = new fabric.Polyline(
[
{ x: 50, y: 0 },
{ x: 25, y: 43.30},
{ x: -25, y: 43.301 },
{ x: -50, y: 0},
{ x: -25, y: -43.301},
{ x: 25, y: -43.301 },
{ x: 50, y: 0 },
],
{
fill: "teal",
stroke: "orange",
strokeWidth: 5,
top: 50,
left: -50,
scaleX: 0.5,
scaleY: 0.5,
}
);
// Adding it to the canvas
canvas.add(polyline);
// Using the animate method
polyline.animate("left", "200", {
onChange: canvas.renderAll.bind(canvas),
duration: 5000,
});
</script>
</body>
</html>
示例 2:為折線新增退出動畫
在這個例子中,我們將看到如何使用`animate`方法和`left`屬性建立退出動畫。為了建立退出效果,我們需要將`left`從 300 設定為 1000。由於我們將`duration`屬性的值設定為 5000,因此此動畫將持續 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 transition-out animation to the polyline</h2>
<p>You can see the transition-out effect has been added to the Polyline</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 polyline object
var polyline = new fabric.Polyline(
[
{ x: 50, y: 0 },
{ x: 25, y: 43.30},
{ x: -25, y: 43.301 },
{ x: -50, y: 0},
{ x: -25, y: -43.301},
{ x: 25, y: -43.301 },
{ x: 50, y: 0 },
],
{
fill: "teal",
stroke: "orange",
strokeWidth: 5,
top: 50,
left: 300,
scaleX: 0.5,
scaleY: 0.5,
}
);
// Adding it to the canvas
canvas.add(polyline);
// Using the animate method
polyline.animate("left", "1000", {
onChange: canvas.renderAll.bind(canvas),
duration: 5000,
});
</script>
</body>
</html>
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP