0

我已经尽一切努力让这个工作以硬编码数字结束,但是当我将鼠标悬停在舞台上时,什么也没有发生。鼠标悬停(或鼠标移动)仅在我实际位于图像本身上方时才会激活。谁能告诉我当我将鼠标移到舞台上时如何使图像向左或向右移动?

stage.on('mouseover', function(){ var mousePos = stage.getUserPosition(); if (mousePos.x >= 289) { rect.move(5, 0); } else { rect.move(-5,0); } stage.add(layer); });

4

1 回答 1

1

当我们在图像之前添加 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>
于 2012-08-30T10:41:59.173 回答