我正在尝试用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);
};
不要使用新的图像对象,而是更改现有对象上的图像:
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);
};
尝试在未选择的图像上添加两个处理程序,例如:
n_amer.on('mouseover', function() {
imagesLayer.add(n_amer_sel)
stage.draw();
});
n_amer.on('mouseout', function() {
imagesLayer.remove(n_amer_sel);
stage.draw();
});
这真是一种奇怪的行为。。。也许与图像缓冲区有关。。。您可以尝试重新绘制侦听器内部的缓冲区。
在任何情况下,我都建议添加两个图像,并使用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);
你可以看到它在这个小提琴中工作(除了受污染的画布问题)