-
-
Save parzibyte/ea2f41cb63140bbb046ddad955181b0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Ejemplo 4 de html2canvas para convertir el HTML de una web | |
* a un elemento canvas - Descargar la captura como imagen PNG | |
* | |
* @author parzibyte | |
*/ | |
//Definimos el botón para escuchar su click | |
const $boton = document.querySelector("#btnCapturar"), // El botón que desencadena | |
$objetivo = document.body; // A qué le tomamos la fotocanvas | |
// Nota: no necesitamos contenedor, pues vamos a descargarla | |
// Agregar el listener al botón | |
$boton.addEventListener("click", () => { | |
html2canvas($objetivo) // Llamar a html2canvas y pasarle el elemento | |
.then(canvas => { | |
// Cuando se resuelva la promesa traerá el canvas | |
// Crear un elemento <a> | |
let enlace = document.createElement('a'); | |
enlace.download = "Captura de página web - Parzibyte.me.png"; | |
// Convertir la imagen a Base64 | |
enlace.href = canvas.toDataURL(); | |
// Hacer click en él | |
enlace.click(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment