Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

190
Visualizações
How to simulate click on canvas with coordinates?

In web game I wanted to simulate clicks with coordinates. Game interface using canvas

I tried several things, but nothing help.

I tried to make new MouseEvent but it returns me "true" and nothing happened.

const e1 = new MouseEvent("click", {
   clientX: 1673,
   clientY: 866
});

canvas.dispatchEvent(e1)

And this returns me error: canvas.elementFromPoint is not a function

canvas.elementFromPoint(1673,866);

What im doing wrong or canvas may have another methods to simulate clicks?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The purpose of the .elementFromPoint() method is to return a HTML element e.g. a <div>. As such this method is chained to the Document object. That means you can't call it on an instance of a canvas element as it simply ain't available there - thus the error message: canvas.elementFromPoint is not a function

Here's an example:

const e1 = new MouseEvent("click", {
  clientX: 50,
  clientY: 50
});

const canvas = document.getElementById("canvas");
canvas.addEventListener("click", (e) => {
  console.log(document.elementFromPoint(e.clientX, e.clientY));
});

canvas.dispatchEvent(e1);
<canvas id="canvas"></canvas>

about 4 years ago · Juan Pablo Isaza Relatório

0

The main game canvas should be inside of an html div that could be clicked. That main containing div would be what gets resized, and therefore you could add specific divs to it with appendChild and absolute position them for specific buttons on top of the canvas. Or get the specific cords of clicks from the container div.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda