如何使用 FabricJS 為折線新增淡入淡出動畫?
我們可以透過建立fabric.Polyline例項來建立一個折線物件。折線物件可以由一組連線的直線段來表徵。由於它是 FabricJS 的基本元素之一,我們也可以透過應用角度、不透明度等屬性來輕鬆定製它。
由於 FabricJS 本身不支援模糊效果,我們將使用 CSS 為折線新增淡入淡出動畫。
語法
filter: blur(Xpx)
這裡,“X”是接受一個數值的屬性,該數值決定了要應用的模糊量。
示例 1:為折線新增淡入動畫
讓我們來看一個程式碼示例,瞭解如何使用filter屬性新增淡入動畫。為了建立淡入效果,我們需要建立一個動畫,該動畫從模糊值為 0px 開始,到模糊值為 5px 結束,如下面的程式碼示例所示,我們添加了 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 blur-in animation to the polyline</h2>
<p>You can see the blur-in effect has been added to the polyline</p>
<canvas id="canvas"></canvas>
<style>
@-webkit-keyframes blur {
0% { filter: blur(0px);}
100% { filter: blur(5px);}
}
#canvas {
animation: blur 5s;
}
</style>
<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: "skyblue",
stroke: "orange",
strokeWidth: 5,
top: 50,
left: 300,
}
);
// Adding it to the canvas
canvas.add(polyline);
</script>
</body>
</html>
示例 2:為折線新增淡出動畫
讓我們來看一個程式碼示例,瞭解如何使用 filter 屬性新增淡出動畫。為了建立淡出效果,我們需要建立一個動畫,該動畫從模糊值為 5px 開始,到模糊值為 0px 結束,如下面的程式碼示例所示,我們添加了 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 blur-out animation to the polyline</h2>
<p>You can see the blur-out effect has been added to the polyline</p>
<canvas id="canvas"></canvas>
<style>
@-webkit-keyframes blur {
0% { filter: blur(5px);}
100% { filter: blur(0px);}
}
#canvas {
animation: blur 5s;
}
</style>
<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: "skyblue",
stroke: "orange",
strokeWidth: 5,
top: 50,
left: 300,
}
);
// Adding it to the canvas
canvas.add(polyline);
</script>
</body>
</html>
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP