KineticJS鼠标事件问题

时间:2012-09-04 12:14:04

标签: javascript html5 canvas mouseover kineticjs

我正在尝试与KineticJS制作某种互动地图,其中悬停的地方会加载其他图像。但我不能使鼠标事件正常工作。事实是,一切正常,但只有第一次鼠标悬停尝试,当我第二次悬停时, mouseout 触发器不起作用:(

即。当我第二次做“mouseout”时,我希望地图部分再次呈现浅蓝色(n_amer图像),但它会重新呈现深蓝色(n_amer_sel图像)。

以下是您可以看到此错误的链接:http://kinlibtst.elitno.net/

我非常感谢你的帮助!

代码:

  function loadImages(sources, callback) {
    var assetDir = 'PROJECT/';
    var images = {};
    var loadedImages = 0;
    var numImages = 0;
    for(var src in sources) {
      numImages++;
    }
    for(var src in sources) {
      images[src] = new Image();
      images[src].onload = function() {
        if(++loadedImages >= numImages) {
          callback(images);
        }
      };
      images[src].src = assetDir + sources[src];
    }
  }
  function initStage(images) {
    var stage = new Kinetic.Stage({
      container: 'container',
      width: 900,
      height: 452
    });

    var imagesLayer = new Kinetic.Layer();

    // img vars
    var n_amer = new Kinetic.Image({
      image: images.n_amer,
      x: 0,
      y: 0
    });

    var n_amer_sel = new Kinetic.Image({
      image: images.n_amer_sel,
      x: 0,
      y: 0
    });

    // mouse events
    n_amer.on('mouseover', function() {
        imagesLayer.add(n_amer_sel)
        stage.draw();
    });
    n_amer_sel.on('mouseout', function() {  
        imagesLayer.remove(n_amer_sel);
        stage.draw();
    });

    // imageBuffer for transparent pixels
    n_amer.createImageBuffer(function() {
      imagesLayer.drawBuffer();
    });
    n_amer_sel.createImageBuffer(function() {
      imagesLayer.drawBuffer();
    });        

    // add to stage
    imagesLayer.add(n_amer);
    stage.add(imagesLayer);
  }

  window.onload = function() {
    var sources = {
      n_amer: 'N-Amer.png',
      n_amer_sel: 'N-Amer_sel.png'
    };
    loadImages(sources, initStage);
  };

3 个答案:

答案 0 :(得分:2)

不使用新的图像对象,而是更改现有对象上的图像:

function loadImages(sources, callback) {
    var assetDir = 'http://kinlibtst.elitno.net/PROJECT/';
    var images = {};
    var loadedImages = 0;
    var numImages = 0;
    for (var src in sources) {
        numImages++;
    }
    for (var src in sources) {
        images[src] = new Image();
        images[src].onload = function() {
            if (++loadedImages >= numImages) {
                callback(images);
            }
        };
        images[src].src = assetDir + sources[src];
    }
}

function initStage(images) {
    var stage = new Kinetic.Stage({
        container: 'container',
        width: 900,
        height: 1000
    });

    var imagesLayer = new Kinetic.Layer();

    // img vars
    var n_amer = new Kinetic.Image({
        image: images.n_amer,
        x: 0,
        y: 0
    });

    // mouse events
    n_amer.on('mouseover', function() {
        this.setImage(images.n_amer_sel);
        stage.draw();
    });
    n_amer.on('mouseout', function() {
        this.setImage(images.n_amer);
        stage.draw();
    });

    // imageBuffer for transparent pixels
    n_amer.createImageBuffer(function() {
        imagesLayer.drawBuffer();
    });

    // add to stage
    imagesLayer.add(n_amer);
    stage.add(imagesLayer);
}

window.onload = function() {
    var sources = {
        n_amer: 'N-Amer.png',
        n_amer_sel: 'N-Amer_sel.png'
    };
    loadImages(sources, initStage);
};​

答案 1 :(得分:1)

尝试在未选择的图像上添加两个处理程序,例如:

n_amer.on('mouseover', function() {
    imagesLayer.add(n_amer_sel)
    stage.draw();
});
n_amer.on('mouseout', function() {  
    imagesLayer.remove(n_amer_sel);
    stage.draw();
});

答案 2 :(得分:1)

这是一个非常奇怪的行为......可能与图像缓冲区有关...你可能会尝试重绘监听器内的缓冲区。

无论如何,我建议添加两张图片并使用show()hide()。此操作通常比添加/删除子项更快。

var n_amer_sel = new Kinetic.Image({
    image: images.n_amer_sel,
    x: 0,
    y: 0,
    visible: false
});
n_amer.on('mouseover', function() {
    n_amer_sel.show();
    stage.draw();
});
n_amer_sel.on('mouseout', function() {  
    n_amer_sel.hide();
    stage.draw();
});

// add to stage
imagesLayer.add(n_amer);
imagesLayer.add(n_amer_sel);
stage.add(imagesLayer);

您可以在此fiddle

中看到它有效(除了受污染的画布问题)