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

309
Vistas
Seleccionar varias imágenes y almacenarlas en una matriz de datos (FIGMA)

Estoy creando un complemento para FIGMA, donde el usuario selecciona varias imágenes, que luego guardo en una matriz, que envío para que se interpreten.

Tengo 2 problemas con mi código

  • img.src = papelera; no activa img.onload, pero si configuro img.src = "cadena literal", el método onload funciona.
  • la matriz imageData enviada al final no está definida, supongo que debido a mi mala comprensión de las funciones asíncronas.

Agradecería su ayuda para resolver esto. Gracias PD: esto es javascript puro y no necesitas saber FIGMA para seguir el código.

 <input type="file" id="images" accept="image/png" multiple /> <button id="button">Create image</button> <script> const button = document.getElementById('button'); button.onclick = () => { const files = document.getElementById('images').files; function readFile(index) { console.log(1); if (index >= files.length) {return}; console.log(2); const file = files[index]; const imageCaption = file.name; var reader = new FileReader(); reader.readAsArrayBuffer(file); reader.onload = function (e) { console.log(4); // get file content const bin = e.target.result; const imageBytes = new Uint8Array(bin); //Get Image width and height var img = new Image(); img.src = bin; img.onload = function () { console.log(6); const width = img.width; const height = img.height; console.log("imageCaption: " + imageCaption); console.log("width: " + width); console.log("height: " + height); console.log("imageBytes: " + imageBytes); var data = {imageBytes, imageCaption, width, height}; //Read Next File nextData = readFile(index + 1); if( nextData ) { data.concat(nextData) } return data; } } } //Call function to Read and Send Images const imageData = readFile(0); //Send Data parent.postMessage({ pluginMessage: { type: 'send-image', imageData, } }, '*');
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Un amigo terminó ayudándome con eso. ¡Gracias Hiba!

 const button = document.getElementById('button'); const input = document.getElementById('input'); button.addEventListener('click', async () => { const files = input.files ? [...input.files] : []; const data = await Promise.all( files.map(async (file) => await getImageData(file)) ); console.log(data); }); async function getImageData(file) { // get binary data from file const bin = await file.arrayBuffer(); // translate bin data into bytes const imageBytes = new Uint8Array(bin) // create data blob from bytes const blob = new Blob([imageBytes], { type: "image/png" }); // create html image element and assign blob as src const img = new Image(); img.src = URL.createObjectURL(blob); // get width and height from rendered image await img.decode(); const { width, height } = img; const data = { image: blob, caption: file.name, width, height }; return data; }

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