KineticJS icon indicating copy to clipboard operation
KineticJS copied to clipboard

Group mouse events

Open lazerblade01 opened this issue 14 years ago • 4 comments

I've found that using setX and setY while gathering mousePosition data seems to work more fluidly than draggable, and is especially handy when clipping. However, I have run into an issue where mouseleave gets triggered when moving between objects within a group, even though the group is listening for mouseleave. This may be related to the touch event issue, although mousedown, mouseup, mousemove and mouseleave all seem to continue working. I'm not sure how to fix the issue, as I'd prefer not to bind event listeners to all of the children of the group as an alternative. I would guess that would cause quite a performance hit.

Here's some code to show how to duplicate the issue.

group.on('mousedown touchstart', function() {
    cvsObj.mS = cvsObj.stage.getUserPosition();
});
group.on('mousemove touchmove', function() {
    if (typeof cvsObj.mS.x !== 'undefined' && (cvsObj.mS.x !== 0 || cvsObj.mS.y !== 0)) {
        cvsObj.dragging = true;
        cvsObj.mB = cvsObj.stage.getUserPosition();
        if (axis === 'horiz') {
            var mV = parseInt((cvsObj.mB.x - cvsObj.mS.x) / cvsObj.scale);
            var bV = base.getX();
            if (bV + mV > gmaxx || bV + mV < gminx) {
                mV = (bV + mV > gmaxx) ? gmaxx - bV : gminx - bV;
            }
            cvsObj.mS.x = cvsObj.mB.x;
            if (hasSlide) {
                var smv = (((bV + mV) / gminx) * (smaxx - sminx)) + sminx;
                slider.setX(smv);
            }
        } else {
            var mV = parseInt((cvsObj.mB.y - cvsObj.mS.y) / cvsObj.scale);
            var bV = base.getY();
            if (bV + mV > gmaxy || bV + mV < gminy) {
                mV = (bV + mV > gmaxy) ? gmaxy - bV : gminy - bV;
            }
            cvsObj.mS.y = cvsObj.mB.y;
            if (hasSlide) {
                var smv = (((bV + mV) / gminy) * (smaxy - sminy)) + sminy;
                slider.setY(smv);
            }
        }
        clipMove(mV, group, axis, clip);
    }
});
group.on('mouseup touchend', function() {
    cvsObj.mS.x = cvsObj.mB.x = cvsObj.mS.y = cvsObj.mB.y = 0;
    setTimeout(function() {
        cvsObj.dragging = false;
    }, 200);
});
    // issue occurs in this block
group.on('mouseleave', function() {
    if (cvsObj.dragging) {
        console.log(group);
        group.fire('mouseup');
    }
});

EDIT : I just did some additional testing, and it seems the issue only occurs when the group listening contains groups of shapes, but not when the group listening only contains objects.

NOTE : Just to provide a little more info, group (above), where it fails, contains subgroups, each of which contains text, image, and rect nodes. group also contains a background rect that sits behind all of the subgroups. When I mousedown on a subgroup node OR the background rect, then move, the mousemove function runs, but if I mouseover any OTHER subgroup node (or in the case of mousedown on the background, ANY subgroup node), mouseleave is triggered. This behavior seems very inconsistent with mouseleave, especially when compared to a group whose children are all non-group nodes.

lazerblade01 avatar Nov 21 '12 14:11 lazerblade01

problem is not in a group event

     on: function(typesStr, handler) {
            var types = typesStr.split(' ');
            var len = types.length;

           // as it is simply a enumeration in cycle
            for(var n = 0; n < len; n++) {
                var type = types[n];
                var event = type;
                var parts = event.split('.');
                var baseEvent = parts[0];
                var name = parts.length > 1 ? parts[1] : '';

                if(!this.eventListeners[baseEvent]) {
                    this.eventListeners[baseEvent] = [];
                }

                this.eventListeners[baseEvent].push({
                    name: name,
                    handler: handler
                });
            }
        },

pavelpower avatar Nov 30 '12 10:11 pavelpower

Sorry but I don't follow. Why would a bound event work fine on a group that only contains objects (Kinetic shapes with draw functions) but not on a group that contains groups, each of which contains shapes?

lazerblade01 avatar Nov 30 '12 12:11 lazerblade01

and in this I agree with you

pavelpower avatar Nov 30 '12 13:11 pavelpower

clicking on a shape inside of a group, which is inside of a group, which is inside of a layer, which is inside of the stage, works for me:

http://jsfiddle.net/8gAW4/

closing this ticket.

ericdrowell avatar May 09 '13 05:05 ericdrowell