0

I'm trying to puzzle out the event dispatching in Javascript. The example below uses Fabricjs library.

AS IS: if we mouseover the redBox, it becomes black.

TO BE: I'm trying to invoke the redBox mouseover event from greyBox mouseover event. So if we mouseover the greyBox, it must work as if we mousever the redBox. As a result the redBox must become black.

But I don't understand what to write instead of interrogation mark.

var greyBox = new fabric.Rect({
    fill: 'grey',
    width: 100,
    height: 100
});

var redBox = new fabric.Rect({
    left:300,
    top:300,
    fill: 'red',
    width: 100,
    height: 100
});

function createCanvas(id){
    canvas = new fabric.Canvas(id);
    canvas.add(greyBox);
    canvas.add(redBox);

    redBox.on("mouseover", function(e){
        redBox.set({fill: 'black'});
    })

    greyBox.on("mouseover", function(e){
        // ????
    })

    return canvas;
}

2 Answers2

1

You could move the logic to a separate function and invoke it when necessary:

const makeRedBoxBlack = () => redBox.set({fill: 'black'});

redBox.on("mouseover", makeRedBoxBlack);
greyBox.on("mouseover", makeRedBoxBlack);
Luc
  • 544
  • 3
  • 9
0

You only have to add that red box set statement, so it should something like :-

greyBox.on("mouseover", function(e){
        redBox.set({fill: 'black'});
    })
  • no, it's only example, it's not the point. it's about event dispatching - on event from another event – AntonAntonoff Jan 09 '22 at 14:26
  • oh so i think that case you should add a listener to that event maybe `redbox.addEventListener('mouseover', greyBox.set({fill: 'black'}))` if i understood correctly – Abdusalam mohamed Jan 09 '22 at 14:32