在本教程中,我們將學(xué)習(xí)如何使用 FabricJS 向三角形添加描邊。三角形是 FabricJS 提供的各種形狀之一。為了創(chuàng)建一個(gè)三角形,我們必須創(chuàng)建一個(gè) Fabric.Triangle 類的實(shí)例并將其添加到畫(huà)布中。
我們的三角形對(duì)象可以通過(guò)多種方式自定義,例如更改其尺寸、添加背景顏色或更改圍繞對(duì)象繪制的線條的顏色。我們可以通過(guò)使用中風(fēng)屬性來(lái)做到這一點(diǎn)。
語(yǔ)法
new fabric.Triangle({ stroke : String }: Object)
登錄后復(fù)制
參數(shù)
選項(xiàng)(可選) – 此參數(shù)是一個(gè)對(duì)象 為我們的三角形提供額外的定制。使用此參數(shù),可以更改與描邊作為屬性的對(duì)象相關(guān)的顏色、光標(biāo)、描邊寬度和許多其他屬性等屬性。
Options Keys
Stroke – 此屬性接受一個(gè) String 來(lái)確定該對(duì)象邊框的顏色。
示例 1
將筆劃作為具有十六進(jìn)制值的鍵進(jìn)行傳遞 strong>
讓我們看一個(gè)代碼示例來(lái)了解使用 lines 屬性時(shí)三角形對(duì)象的顯示方式。十六進(jìn)制顏色代碼以“#”開(kāi)頭,后跟代表顏色的六位數(shù)字。在本例中,我們使用了“#3cb371”,即中海綠色。
<!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>Passing stroke as key with a hexadecimal value</h2>
<p>You can see that the stroke around the triangle is of medium sea-green colour</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 triangle object
var triangle = new fabric.Triangle({
left: 120,
top: 30,
width: 90,
height: 80,
fill: "#ecebbd",
stroke: "#3cb371",
strokeWidth: 7,
});
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>
登錄后復(fù)制
示例 2
將 rgba 值傳遞給描邊屬性
在此示例中,我們將了解如何為描邊屬性指定一個(gè) rgba 值。我們可以使用 RGBA 值來(lái)代替十六進(jìn)制顏色代碼,它代表:紅色、綠色、藍(lán)色和 alpha。 alpha 參數(shù)指定顏色的不透明度。在此示例中,我們使用了 rgba 值 (46,139,87,0.8),它是不透明度為 0.8 的海綠色。
<!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>Passing an rgba value to the stroke property</h2>
<p>You can see that the stroke colour is coming from the RGBA value now</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 triangle object
var triangle = new fabric.Triangle({
left: 120,
top: 30,
width: 90,
height: 80,
fill: "#ecebbd",
stroke: "rgba(46,139,87,0.8)",
strokeWidth: 7,
});
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>
登錄后復(fù)制
以上就是如何使用 FabricJS 向三角形添加描邊?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!






