1

我在这里没有什么问题(我显然错过了一些东西)。我从我更大的应用程序中对其进行了简化:当我单击蓝色矩形时,我在舞台上添加了另一个层,其中包括红色矩形(可点击),当我单击这个红色矩形时,它会删除带有红色矩形的第二层。

问题:当我第二次单击蓝色矩形时,什么也没有发生:(即应用程序只能运行一次,我需要重复添加/删除第二层(红色矩形)。出了什么问题?:)

你可以在这里看到它,小提琴http://jsfiddle.net/max8r/

代码:

<!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.3.js">
</script>
<script>
window.onload = function() {
var stage = new Kinetic.Stage({
container: 'container',
width: 578,
height: 200
});

var layerBlue = new Kinetic.Layer();
var layerRed = new Kinetic.Layer();

var rectBlue = new Kinetic.Rect({
x: 100,
y: 75,
width: 100,
height: 50,
fill: 'blue',
stroke: 'black',
strokeWidth: 4
});
var rectRed = new Kinetic.Rect({
x: 300,
y: 75,
width: 100,
height: 50,
fill: 'red',
stroke: 'black',
strokeWidth: 4
});

// mouse events
rectBlue.on('click', function() {
stage.add(layerRed);
stage.draw();
});
rectRed.on('click', function() {
layerRed.remove();
stage.draw();
});

// add the shape to the layer
layerBlue.add(rectBlue);
layerRed.add(rectRed);
// add the layer to the stage
stage.add(layerBlue);

};

</script>
</head>
<body>
<div id="container"></div>
</body>
</html>

​</p>

4

1 回答 1

1

要隐藏和显示形状,我们可以使用 hide() 和 show() 方法。试试这个 JSFIDDLE http://jsfiddle.net/max8r/5/。你可以看到下面的示例代码

<!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.3.js"></script>
    <script>
        var layerBlue;
        var layerRed;
        var rectBlue;
        var rectRed;
      window.onload = function() {
        var stage = new Kinetic.Stage({
          container: 'container',
          width: 578,
          height: 200
        });

        layerBlue = new Kinetic.Layer();
        layerRed = new Kinetic.Layer();

        rectBlue = new Kinetic.Rect({
          x: 100,
          y: 75,
          width: 100,
          height: 50,
          fill: 'blue',
          stroke: 'black',
          strokeWidth: 4
        });
        rectRed = new Kinetic.Rect({
          x: 300,
          y: 75,
          width: 100,
          height: 50,
          fill: 'red',
          stroke: 'black',
          strokeWidth: 4
        });

        // mouse events
        rectBlue.on('click', function() {
          rectRed.show();
          stage.draw();
        });
        rectRed.on('click', function() {
          rectRed.hide();
          stage.draw();
        });

        // add the shape to the layer
        layerBlue.add(rectBlue);
        layerRed.add(rectRed);

        // add the layer to the stage
        stage.add(layerBlue);
        stage.add(layerRed);
        rectRed.hide();
        stage.draw();
      };

    </script>
  </head>
  <body>
    <div id="container"></div>
  </body>
</html>

请参阅此网址http://www.html5canvastutorials.com/kineticjs/html5-canvas-hide-and-show-shape-with-kineticjs/了解 HTML5 画布隐藏和显示形状

于 2012-10-22T09:01:29.127 回答