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

128
Visualizações
How do you register onMouseMove event on a canvas ctx in React?

I just finished a course on React and am porting a previous vanilla JS project to React as a learning exercise.

In my previous code (which works properly) I have a stack of 2 HTML canvases with an image loaded into the bottom ctx and a white rectangle loaded into the top ctx. I then use the JS 'mousemove' event listener to erase the white rectangle on top to reveal the image below.

I think I am very close to the solution in my React code. (If you follow the link you'll just see a white page - that's the white rectangle on top of the image. I've verified that the image is indeed under there.)

I've loaded the bottom image and the top rectangle within useEffect() so that React doesn't get any undefined canvas ctx's when the component first renders. However, I'm having difficulty connecting my eraser functionality in the 'eraseImageHandler'. Any ideas what I may be doing wrong? Thanks.

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

0

On the initial render it doesn't know how big the image is so it can't calculate the canvas sizes correctly. If the image had an intrinsic size as below then it would be able to calculate it.

    <Fragment>
      <div className={classes.stack}>
        <canvas ref={bottomCanvasRef} />
        <canvas ref={topCanvasRef} onMouseMove={eraseImageHandler} />
      </div>
      <img
        ref={imageRef}
        hidden
        width={300}
        height={300}
        src="https://upload.wikimedia.org/wikipedia/commons/4/41/Sunflower_from_Silesia2.jpg"
        alt="Flower"
      />
    </Fragment>
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