Events are lost after moving layer from within a “

2019-07-20 01:54发布

问题:

I have a scrollbar that moves a layer, so the layer is moved while in the scrollbar's "dragmove" callback. This causes all bound events to be disconnected on the moved layer!

Please see this fiddle: http://jsfiddle.net/NY4QK/10/

var stage = new Kinetic.Stage({
    container: 'container',
    width: 200,
    height: 200,
});

var fixedLayer = new Kinetic.Layer();
stage.add(fixedLayer);

var old_x = 100;
var old_y = 100;

var scroller = new Kinetic.Circle({
    x: old_x,
    y: old_y,
    radius: 10,
    fill: '#00F',
    stroke: 'black',
    strokeWidth: 4,
    draggable: true
});

scroller.on('dragmove', function(event){
    var pos = scroller.getAbsolutePosition();
    layer.move(pos.x - old_x, pos.y - old_y);
    old_x = pos.x;
    old_y = pos.y;
    layer.draw();
});

fixedLayer.add(scroller);

var layer = new Kinetic.Layer();
stage.add(layer);

var rect = new Kinetic.Rect({
    x: 10,
    y: 10,
    width: 50,
    height: 50,
    fill: '#0F0',
    stroke: 'black',
    strokeWidth: 4
});


rect.on('click', function(){
    rect.remove();
    layer.draw();
});

layer.add(rect);

layer.draw();
fixedLayer.draw();

Is this a bug or am I doing something wrong?

回答1:

When you use a drag event, KineticJS create a temporary layer on top as a result of which your events where not getting registered after the dragmove.

Just add another handler for dragend like this:

scroller.on('dragend', function(event){
    layer.moveToTop();
    layer.draw();
});

And here is the updated fiddle.

For more details/explanation on the problem you faced, check this: https://github.com/ericdrowell/KineticJS/issues/219