如何使用 FabricJS 在物件懸停時建立帶有十字準線游標的橢圓?
在本教程中,我們將使用 FabricJS 建立一個橢圓,並在懸停在物件上時顯示十字準線游標。十字準線是可用的原生游標樣式之一,也可以在 FabricJS 畫布中使用。FabricJS 提供了各種型別的滑鼠游標,例如預設、全部滾動、十字準線、列調整大小、行調整大小等,這些游標在底層重用了原生游標。hoverCursor 屬性在將滑鼠懸停在畫布物件上時設定游標的樣式。
語法
new fabric.Ellipse({ hoverCursor: String }: Object引數
options (可選) - 此引數是一個物件,它為我們的橢圓提供了額外的自定義。使用此引數,可以更改與物件相關的顏色、游標、描邊寬度以及許多其他屬性,其中hoverCursor 是一個屬性。
選項鍵
hoverCursor - 此屬性接受一個字串,該字串確定在將滑鼠懸停在畫布物件上時要使用的游標的名稱。透過使用此屬性,我們可以在將滑鼠懸停在畫布上橢圓物件時設定預設的游標值。
示例 1
將hoverCursor 鍵傳遞給類
預設情況下,當我們將滑鼠懸停在畫布中的橢圓物件上時,游標型別為移動。讓我們看看一段程式碼,該程式碼建立一個畫布,並在使用 FabricJS 將滑鼠懸停在橢圓物件上時顯示十字準線游標。
<!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>Creating an ellipse with crosshair cursor on hover over objects using FabricJS?</h2>
<p> Hover the mouse over the ellipse to see the crosshair cursor.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an ellipse instance
var ellipse = new fabric.Ellipse({
left: 100,
top: 100,
fill: "#a2006d",
rx: 80,
ry: 50,
stroke: "#c154c1",
strokeWidth: 5,
hoverCursor: "crosshair",
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
證明hoverCursor 應用於特定物件
在此示例中,我們將 hoverCursor 鍵傳遞給橢圓類,這意味著hoverCursor 屬性不會更改畫布中的每個物件。更改只會發生在該單個物件上。
<!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>Creating an Ellipse with crosshair cursor on hover over objects using FabricJS?</h2>
<p>Hover the mouse over the left ellipse to see the crosshair cursor. We haven't applied the hoverCursor property to the right ellipse. </p2>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an ellipse instance
var ellipseOne = new fabric.Ellipse({
left: 90,
top: 100,
fill: "white",
rx: 80,
ry: 50,
stroke: "#c154c1",
strokeWidth: 5,
hoverCursor: "crosshair",
});
// Initiate another ellipse instance
var ellipseTwo = new fabric.Ellipse({
left: 280,
top: 100,
fill: "#a2006d",
rx: 80,
ry: 50,
});
// Add them to the canvas
canvas.add(ellipseOne);
canvas.add(ellipseTwo);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP