当我们在图像之前添加 Rectangle(Transparent) 时,KineticJS Moverover 工作。请参阅下面的代码,它的工作。
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
canvas {
border: 1px solid #9C9898;
}
</style>
<script src="http://www.html5canvastutorials.com/libraries/kinetic-v4.0.0.js"></script>
<script>
window.onload = function() {
var stage = new Kinetic.Stage({
container: "container",
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var imageObj = new Image();
imageObj.onload = function() {
var yoda = new Kinetic.Image({
x: 140,
y: stage.getHeight() / 2 - 59,
image: imageObj,
width: 106,
height: 118
});
var rect = new Kinetic.Rect({
x: 0,
y: 0,
width: stage.getWidth(),
height: stage.getHeight()
});
layer.add(rect);
layer.add(yoda);
stage.add(layer);
stage.on('mousemove', function(){
var mousePos = stage.getUserPosition();
if (mousePos.x >= 289)
{
yoda.move(5, 0);
}
else
{
yoda.move(-5, 0);
}
layer.draw();
});
};
imageObj.src = "http://www.html5canvastutorials.com/demos/assets/yoda.jpg";
};
</script>
</head>
<body>
<div id="container"></div>
</body>
</html>