如何使用 FabricJS 在畫布選擇區域的邊框上新增虛線?
在本文中,我們將學習如何使用 FabricJS 在畫布選擇區域的邊框上新增虛線。我們可以透過使用 `selectionDashArray` 屬性來實現這一點。它允許我們將選擇區域的邊框設定為虛線。
語法
new fabric.Canvas(element: HTMLElement|String, { selectionDashArray: Array }: Object)引數
element − 此引數是<canvas> 元素本身,可以使用 `document.getElementById()` 或<canvas> 元素的 ID 獲取。FabricJS 畫布將在此元素上初始化。
options (可選) − 此引數是一個物件,它為我們的畫布提供額外的自定義選項。使用此引數可以更改畫布相關的許多屬性,例如顏色、游標、邊框寬度等等,其中 `selectionDashArray` 是一個屬性。它接受一個數組,該陣列決定我們想要的虛線圖案。
示例 1
將 `selectionDashArray` 作為類的鍵傳遞
`selectionDashArray` 允許我們將選擇區域的邊框設定為虛線。定義虛線圖案的方法是在陣列中指定虛線的長度。在下面的示例中,我們使用了 `[7,6]` 陣列。這意味著將會有一個 7px 長的線,然後是一個 6px 的間隙,依此類推。
<!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 dashes to the border of a selection area on a canvas</h2>
<p>Select an area around the object. The border of the selection area would have dashed lines.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas", {
selectionDashArray: [7, 6],
selectionBorderColor: "red"
});
// Creating an instance of the fabric.Rect class
var circle = new fabric.Circle({
left: 200,
top: 100,
radius: 40,
fill: "blue",
});
// Adding it to the canvas
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
結合使用 `selectionDashArray`、`selectionLineWidth` 和 `selectionBorderColor`
`selectionDashArray` 屬性可以以多種方式使用。一種方法是將其與 `selectionLineWidth` 和 `selectionBorderColor` 結合使用,分別指定選擇邊框的寬度和顏色。
<!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 dashes to the border of a selection area on a canvas</h2>
<p>Select an area around the object and observe the outline of the selection area. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas", {
selectionDashArray: [13, 16],
selectionLineWidth: 5,
selectionBorderColor: "green",
});
// Creating an instance of the fabric.Rect class
var circle = new fabric.Circle({
left: 200,
top: 100,
radius: 40,
fill: "blue",
});
// Adding it to the canvas
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP