Obtendría el tamaño de una imagen, siguiendo este tutorial, pero obtengo un error de TypeScript:
const img = new Image(); img.onload = function() { alert(this.width + 'x' + this.height); } img.src = 'http://www.google.com/intl/en_ALL/images/logo.gif';¿Cómo obtener el tamaño de la imagen (alto y ancho) usando JavaScript?
La propiedad 'ancho' no existe en el tipo 'GlobalEventHandlers'.
¿Qué hacer?
El controlador de carga this está escrito en la imagen. Haga referencia a la imagen directamente en su lugar.
const img = new Image(); img.onload = function() { console.log(img.width + 'x' + img.height); } O use addEventListener .
img.addEventListener('load', function() { console.log(this.width + 'x' + this.height); })