Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

518
Vistas
Failed to Execute DrawImage in Next.js

I'm new to Next.js. I'm trying to load image in html canvas and do some work on it.

When I try to draw on canvas I'm getting this error

Unhandled Runtime Error
TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLCanvasElement or HTMLImageElement or HTMLVideoElement or ImageBitmap or OffscreenCanvas or SVGImageElement or VideoFrame)'.

This is my code

import Image from "next/image";

function tileImage() {
  return <Image src="/tiles.png" alt="tiles" />;
}
           
const draw = (ctx) => {
    for (var c = 0; c < map.cols; c++) {
      for (var r = 0; r < map.rows; r++) {
        var tile = map.getTile(c, r);
        if (tile !== 0) {
          // 0 => empty tile
          ctx.drawImage(
            tileImage, // image
            (tile - 1) * map.tsize, // source x
            0, // source y
            map.tsize, // source width
            map.tsize, // source height
            c * map.tsize, // target x
            r * map.tsize, // target y
            map.tsize, // target width
            map.tsize // target height
          );
        }
      }
    }
  };

  return (
    <div className={styles.container}>
      <main className={styles.main}>
        <Canvas draw={draw} />
      </main>
    </div>
  );
}

Any Help will be appreciated. Thanks in advance.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

OK, another idea: You passing function not img. Try this:

const imgRef=useRef();

...
ctx.drawImage(
            imgRef.current, // image
            (tile - 1) * map.tsize, // source x

...
 <div className={styles.container}>
      <main className={styles.main}>
        <Canvas draw={draw} />
        <img src="/tiles.png" alt="tiles" ref={imgRef}/>
      </main>
 </div>
...
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda