Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!