如何使用 FabricJS 在对象悬停时创建带有帮助光标的圆形?
在本教程中,我们将使用 FabricJS 创建一个在对象悬停时具有帮助光标的圆形。帮助是可用的原生光标样式之一,也可以在 FabricJS 画布中使用。FabricJS 提供各种类型的光标,如默认、全部滚动、十字线、列调整大小、行调整大小等,这些光标在底层重用了原生光标。hoverCursor 属性在将鼠标悬停在画布对象上时设置光标的样式。
语法
new fabric.Circle({ 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 a circle with help cursor on hover over objects using FabricJS</h2>
<p>Hover the mouse over the object to see the changed shape of the cursor. Here we have used the <b>help</b> cursor. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var circle = new fabric.Circle({
left: 115,
top: 100,
fill: "#b22222",
radius: 50,
stroke: "black",
strokeWidth: 5,
hoverCursor: "help"
});
// Adding it to the canvas
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
演示这仅影响实例
在此示例中,我们将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 a circle with help cursor on hover over objects using FabricJS</h2>
<p>Move the cursor over the objects. You will get to see the <b>help</b> cursor when you hover the mouse over the left circle, but not on the right circle. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var circle = new fabric.Circle({
left: 115,
top: 100,
fill: "#b22222",
radius: 50,
stroke: "black",
strokeWidth: 5,
hoverCursor: "help"
});
var circle2 = new fabric.Circle({
left: 315,
top: 100,
fill: "black",
radius: 50,
stroke: "#8b0000",
strokeWidth: 5
});
// Adding it to the canvas
canvas.add(circle);
canvas.add(circle2);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP