如何使用 FabricJS 在悬停对象时创建带有辅助光标的椭圆?
在本教程中,我们将学习如何使用 FabricJS 创建一个椭圆,并在悬停对象时显示辅助光标。“help”是可用于 FabricJS 画布的原生光标样式之一。FabricJS 提供各种类型的光标,例如 default、all-scroll、crosshair、col-resize、row-resize 等,这些光标在底层重用了原生光标。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 help cursor on hover over objects using FabricJS</h2>
<p>Hover the mouse over the ellipse to see the <b>help</b> 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: 115,
top: 100,
fill: "#b22222",
rx: 80,
ry: 50,
stroke: "black",
strokeWidth: 5,
hoverCursor: "help",
});
// Adding it to the canvas
canvas.add(ellipse);
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 an Ellipse with help cursor on hover over objects using FabricJS</h2>
<p>Hover the mouse over the objects. On the left ellipse, you would get to see <b>help</b> cursor. We haven't applied the <b>hoverCursor</b> property to the right ellipse. </p>
<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: 115,
top: 100,
fill: "#b22222",
rx: 80,
ry: 50,
stroke: "black",
strokeWidth: 5,
hoverCursor: "help",
});
// Initiate another ellipse instance
var ellipseTwo = new fabric.Ellipse({
rx: 80,
ry: 50,
left: 335,
top: 100,
fill: "black",
stroke: "#8b0000",
strokeWidth: 5,
});
// Add them to the canvas
canvas.add(ellipseOne);
canvas.add(ellipseTwo);
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