danzen / zimjs

ZIM JavaScript Canvas Framework - Code Creativity! Interactive Media For All.
Other
507 stars 47 forks source link

Events ignored when shapes are added to other shapes #27

Closed ghost closed 6 years ago

ghost commented 6 years ago

I've created a stage just like the slide example (http://zimjs.com/bits/view/slide.html) where I replace the image with another rectangle so that one rectangle is the mask of another. I consider the masked rectangle as the stage where other objects can be placed upon and when dragging this stage all objects are also moved. This is working as expected, except that shapes on the masked rectangle ignore events. What would be the issue?

var map = new Rectangle(frame.width, frame.height, frame.blue).addTo(stage).center();

var mask = new Rectangle(600, 400).addTo(stage, 0).pos(200, 100);

map.setMask(mask);

map.drag({
    mask:new createjs.Rectangle(200, 100, 600, 400),
    surround:true,
    slide:true,
    dragCursor:"pointer"
});

var line = new Rectangle(375, 50, frame.red).pos(300, 300).addTo(map); // added to map so it also moves when the map gets dragged.

line.on("mouseover", function() {
    console.log("test"); / /ignored
});
danzen commented 6 years ago

Hi Samvherck, ZIM basic shapes (Rectangle, Circle, Triangle) are Containers but they have their mouseChildren property set to false - so the container is dragged, etc. rather than the shape inside the container. So... you can use a Container that holds map and line - as I find it best not to think of the Rectangle as a container. Or you can leave the structure how you have it and then set map.mouseChildren = true; and then on your drag use currentTarget:true. Then dragging the map will drag the whole map (the event object's currentTarget) and not its contents (the event object's target) and the rollover will still work on the line. Hope this helps.

ghost commented 6 years ago

Hello danzen, thanks for the quick response. I like the idea of placing the line and map in a new container, but this still doesn't result in the expected behaviour where the line is also moved when dragging the map. I made a fiddle: https://jsfiddle.net/6dm2hpzr/2/

danzen commented 6 years ago

I suspect what you want is this? Where the Container is what is being masked. Then if you drag the container you would need to set currentTarget:true so you do not end up dragging the things inside individually. And note - the drag has a rect param (you had accidentally put a mask param).

var scaling = "fit";
var width = 1024;
var height = 768;

var frame = new Frame(scaling, width, height);
frame.on("ready", function () {

    var stage = frame.stage;
    var stageW = frame.width;
    var stageH = frame.height;
    frame.outerColor = "#444";
    frame.color = "#ddd";

    var container = new Container();
    var map = new Rectangle(stageW, stageH, frame.blue).addTo(container);
    container.center();

    var mask = new Rectangle(600, 400).pos(200, 100, stage, 0);

    container.setMask(mask);

    container.drag({
        rect:new createjs.Rectangle(200, 100, 600, 400),
        surround:true,
        slide:true,
        dragCursor:"pointer",
        currentTarget:true
    });

    var line = new Rectangle(375, 50, frame.red).pos(300, 300, container);

    line.on("mouseover", function() {
        console.log("test");
    });

    stage.update();
});
ghost commented 6 years ago

Thanks danzen, that worked. Is it somehow possible for an object to be a child of the container, meaning it will be moved when the container is dragged but still can be dragged separately?

My goal is to create a combination of the slide example (zimjs.com/bits/view/slide.html) and a drag (zimjs.com/bits/view/drag.html) where the stage can be larger than the screen and moved when dragging it (also repositioning child objects), but where it is also possible to drag object separately.

danzen commented 6 years ago

Hi Sam, sorry for the delay - just saw this. Put a drag() on the line (or object in the container you want to drag) and then add a stopPropagation() on the mousedown event object. I think this will stop the container from being dragged - note that this is not a stopImmediatePropagation() which would also stop the line from being dragged ;-).

    var line = new Rectangle(375, 50, frame.red)
        .pos(300, 300, container)
        .drag();
    line.on("mousedown", function(e) {e.stopPropagation();});

        // line.on("mouseover", function() {
        // console.log("test");
    // });
danzen commented 6 years ago

Oh... you will probably want to put bounds on the things you are dragging in the container - so use localBounds:true in the drag along with a rect:

var line = new Rectangle(375, 50, frame.red)
    .pos(300, 300, container)
    .drag({rect:new createjs.Rectangle(0,0,map.width-375,map.height-50), localBounds:true});
line.on("mousedown", function(e) {e.stopPropagation();});
danzen commented 6 years ago

Here the example I was testing http://zimjs.com/explore/dragdrag.html

ghost commented 6 years ago

Hello danzen, that is exactly what I was going for. Thanks!

danzen commented 6 years ago

Excellent!