KineticJS删除和添加图层

时间:2013-04-02 21:30:43

标签: javascript canvas kineticjs

我觉得自己很难以愚蠢,但由于某些原因我无法解决这个问题。

我想要3个单独的图层,每个图层可以有多个对象/形状,然后点击我希望删除或隐藏可见图层,然后显示下一个图层。

我认为我的问题是死于逻辑并调用函数。这是函数和jsfiddle:

var version = 0;    
function layerVersion() {
  if (version === 1) {
    stage.add(layerBlue);
    layerBlue.on('click', function() {
      layerOrange.hide;
      version = 2;
    });
  } else if (version === 2) {
    stage.add(layerOrange);
  } else {
    stage.add(layerPink);
    layerpink.on('click', function() {
      layerPink.hide;
      version = 1;
    });
  }
}

以下是jsFiddle链接:http://jsfiddle.net/TJ96r/2/

任何帮助都会非常感激我因为无法理解而感到愚蠢。

1 个答案:

答案 0 :(得分:1)

检查一下。 http://jsfiddle.net/TJ96r/3/

var stage = new Kinetic.Stage({
    container: 'container',
    width: 300,
    height: 300
});
var layerPink = new Kinetic.Layer();
layerPink.hide();
var layerBlue = new Kinetic.Layer();
var layerOrange = new Kinetic.Layer();
layerOrange.hide();

  // pink box
  var pink = new Kinetic.Rect({
    x: 50,
    y: 50,
    width: 100,
    height: 100,
    fill: 'pink',
    stroke: 'black',
    strokeWidth: 2
  });
  // blue box
  var blue = new Kinetic.Rect({
    x: 100,
    y: 100,
    width: 100,
    height: 100,
    fill: 'blue',
    stroke: 'black',
    strokeWidth: 2
  });
  // orange box
  var orange = new Kinetic.Rect({
    x: 150,
    y: 150,
    width: 100,
    height: 100,
    fill: 'orange',
    stroke: 'black',
    strokeWidth: 2
  });

  layerPink.add(pink);
  layerBlue.add(blue);
  layerOrange.add(orange);

  var version = 0;
      stage.add(layerBlue);
      stage.add(layerOrange);
      stage.add(layerPink);

layerBlue.on('click', function() {
        layerBlue.hide();
        layerOrange.show();
        layerPink.hide();
    stage.draw();
      });
layerOrange.on('click', function() {
        layerBlue.hide();
        layerOrange.hide();
        layerPink.show();
    stage.draw();
      });
layerPink.on('click', function() {
        layerPink.hide();
        layerOrange.hide();
        layerBlue.show();
    stage.draw();
      });