Events are lost after moving layer from within a “dragmove” callback

情到浓时终转凉″ 提交于 2020-01-15 06:14:05

问题


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



来源:https://stackoverflow.com/questions/18676906/events-are-lost-after-moving-layer-from-within-a-dragmove-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!