如何使用 FabricJS 設定圓形描邊的寬度?
在本教程中,我們將學習如何使用 FabricJS 為圓形新增虛線描邊。圓形是 FabricJS 提供的各種形狀之一。為了建立一個圓形,我們將必須建立一個fabric.Circle類的例項並將其新增到畫布中。strokeWidth屬性允許我們指定物件描邊的寬度。
語法
new fabric.Circle( { strokeWidth: Number }: Object)引數
options(可選) - 此引數是一個Object,它為我們的圓形提供額外的自定義選項。使用此引數,可以更改與物件相關的屬性,例如顏色、游標、描邊寬度以及許多其他屬性,其中strokeWidth就是一個屬性。
選項鍵
strokeWidth - 此屬性接受一個Number值,允許我們指定物件的描邊寬度。其預設值為 1。
示例 1
物件的描邊預設外觀
讓我們來看一段程式碼,它描述了圓形物件的描邊預設外觀。由於我們沒有使用strokeWidth屬性,因此渲染的是預設寬度。
<!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>Setting the width of stroke of circle using FabricJS</h2>
<p>Notice the outline border of the circle. This is the default thickness of outline. Here we have not used the <b>strokeWidth</b> property, but by default, it is set to 1. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var cir = new fabric.Circle({
left: 215,
top: 40,
fill: "#adff2f",
radius: 100,
stroke: "#228b22",
//strokeWidth: 1
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
將strokeWidth屬性作為鍵傳遞
在此示例中,我們傳遞了值為 5 的strokeWidth屬性。這將確保我們的圓形物件以寬度為 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>Setting the width of stroke of circle using FabricJS</h2>
<p>Notice the outline border of the circle. Here we have used the <b>strokeWidth</b> property and assigned it a value of 5.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var cir = new fabric.Circle({
left: 215,
top: 40,
fill: "#adff2f",
radius: 100,
stroke: "#228b22",
strokeWidth: 5
});
// Adding it to the canvas
canvas.add(cir);
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