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

319
Views
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 answers
Answer question

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 Report
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!