HTML5 Canvas 更改鼠标光标样式

要使用 Konva 框架更改鼠标光标,您只需监听需要更改光标的事件,并手动为 Stage 容器应用新样式。

说明:将鼠标悬停在每个五边形上,查看光标如何变化

Konva 鼠标光标演示view raw
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/konva@9.3.18/konva.min.js"></script>
<meta charset="utf-8" />
<title>Konva Mouse Cursor Styles Demo</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f0f0f0;
}
</style>
</head>

<body>
<div id="container"></div>
<script>
var width = window.innerWidth;
var height = window.innerHeight;

var stage = new Konva.Stage({
container: 'container',
width: width,
height: height,
});
var layer = new Konva.Layer();

var shape1 = new Konva.RegularPolygon({
x: 80,
y: stage.height() / 2,
sides: 5,
radius: 70,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
draggable: true,
});

var shape2 = new Konva.RegularPolygon({
x: 220,
y: stage.height() / 2,
sides: 5,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
draggable: true,
});

var shape3 = new Konva.RegularPolygon({
x: 360,
y: stage.height() / 2,
sides: 5,
radius: 70,
fillLinearGradientStartPoint: { x: -50, y: -50 },
fillLinearGradientEndPoint: { x: 50, y: 50 },
fillLinearGradientColorStops: [0, 'red', 1, 'yellow'],
stroke: 'black',
strokeWidth: 4,
draggable: true,
});

shape1.on('mouseenter', function () {
stage.container().style.cursor = 'pointer';
});

shape1.on('mouseleave', function () {
stage.container().style.cursor = 'default';
});

shape2.on('mouseenter', function () {
stage.container().style.cursor = 'move';
});

shape2.on('mouseleave', function () {
stage.container().style.cursor = 'default';
});

shape3.on('mouseenter', function () {
stage.container().style.cursor = 'crosshair';
});

shape3.on('mouseleave', function () {
stage.container().style.cursor = 'default';
});

layer.add(shape1);
layer.add(shape2);
layer.add(shape3);
stage.add(layer);
</script>
</body>
</html>