How to identify a click outside the Canvas frame?

33 Views Asked by At

I'm creating a project in Fabricjs Canvas and I would like to know how to identify a click outside the canvas area, I want to deselect all objects with the canvas.discardActiveObject().renderAll() function; however it only works when the click occurs within the canvas, I need to activate it when I click outside, I even created a function to activate this function in the div that the canvas is inside but when I click on the canvas it is also activated, could someone tell me help I've looked everywhere and haven't found a solution that should be simple.

1

There are 1 best solutions below

0
On

I managed to use a class outside the canvas and identifying the class that is being clicked, so when you click on the canvas casse out it activates the deselection of all objects, the code is for those who need or want to improve.

 window.addEventListener("click", () => {
      window.addEventListener("click", (e) => {

        const { target } = e;

        if (target instanceof HTMLElement) {
        if(target.className=="canvas-outer"){
          console.log('clicou fora');
          this.utilService.canvasCommand('CLEAN_SELECT', {});
        }
      }

      });
    });

  }