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

204
Vistas
Preloading and drawing an Image onto a canvas inside a Vue SPA doe snot work

I have a Vue 3 SPA where I manipulate some canvas elements. I preload my image using the function below

async preloadLogo () {
    return new Promise( (resolve) => {
        var logo_img_temp = new Image();
        const logo_src = require("../assets/eccc_logo.png");
        logo_img_temp.src = logo_src;
        logo_img_temp.onload = ( ) => {
            resolve(logo_img_temp)
        };
    })
},

In another function I try to drawImage the preloaded image using the code below and a context

var ctx = composedCnv.getContext('2d');
...
this.preloadLogo().then(
    ( img ) => {
        console.log(img instanceof HTMLImageElement,img.tagName, img); returns true IMG <img .../>
        ctx.drawImage(img, 50,50);
    }
)

I do not get the Image drawn even though the rest of my code works and I get the text onto the canvas. The best I could come up with is to get an error saying img is not of type HTMLImageElement which I checked to be true. Any idea what i am doing wrong?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

A work around was adding an img to the template so it is rendered on creation and then getElementById.

<template>
...
   <img id="YOUR_ID" src="@/PATH/IMG_NAME.png" style="display: none;"/>
...
</template>

and then fetching it using a const image = document.getElementById('YOUR_ID').

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