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

149
Visualizações
Local Image not loading in Canvas in React

I was writing a code to create a certificate generator using React. My approach is to input the name from the user and it gets drawn over the Certificate template using HTML Canvas. The Problem I am encountering is that the image never loads if I use an image from the local directory. However, when I use a link for the Image src like Lorem Picsum, the image loads. Here is the code I wrote

function Canvas(){
    const canvasRef = useRef(null);

    useEffect(()=> {
        const canvas = canvasRef.current;
        const ctx = canvas.getContext('2d');
        let imgUrl = {CertificateTemplate}
        const image = new Image();
        image.crossOrigin = "Anonymous";
        image.src = imgUrl;
        image.onload = function() {
            ctx.drawImage(image, 0, 0);
            ctx.font = "54px Norwester"
            ctx.fillText("Your Name", 210, 75)
        }
        image.onerror = function(err) {
            console.log("err", err);
        };
        
    })

    return (
        <>
            <canvas ref={canvasRef} height={1024} width={512} style={{border: '1px solid black'}}/>
        </>
    );
}

Here is my import statement for the image

import CertificateTemplate from "../images/certificate-template.png";

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
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