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

102
Vistas
Save Canvas to PNG for iOS

I use react and html-to-image to export HTML to images. Because Safari not working properly with the jpg/png export, I need to export HTML node to SVG (data url is generated like this : data:image/svg...).

With this string, I am creating a new Image inside a Canvas then saving canvas to .Png with method canvas.toDataURL("image/png").

Everything is working on Firefox/Chrome, but on Safari, it's not working the first time.

  • First Click - Just avec HTML node (no text)
  • Second Click - Some Images + Previous HTML Node
  • Third Click - Everything is working

enter image description here

Here is part of my function :

var canvas = document.createElement("canvas")
  var ctx = canvas.getContext('2d')
  var img = new Image; 

  setTimeout(()=>{
    htmlToImage.toSvg(document.querySelector('.Banner'), {pixelRatio: RatioVal}).then((dataUrl) => {            
      canvas.width = 1300*RatioVal;
      canvas.height = 690*RatioVal;            
      img.src = dataUrl;     

      img.onload = () => {        
        ctx.drawImage(img,0,0,canvas.width, canvas.height)  
        var Export = canvas.toDataURL("image/png");   
        document.body.append(Export)
        var a = document.createElement('a');       
        a.href = Export;
        a.download = window.Chara.Nom+".png";
        a.click();              
      };                  
    })
  },600)

Do you know why I need to click on Export 3 times to have a full image ? Is this the best way to convert SVG dataUrl to PNG Image ?

I tried many things (Blob to Png etc..) but the problem is the same on Safari.

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