Estoy creando un proyecto React que incluye imágenes. En mi proyecto, quiero obtener el ancho y el alto de la imagen que se cargó. Para eso estoy usando
const image = document.getElementById("inputimage"); const width = Number (image.width); const height = Number (image.height); console.log( width, height );Etiqueta de imagen en otra página
<img id='inputimage' alt='image' src={imageUrl} width="500px" height="auto" /> Aquí Number() se usa para convertir image.width e image.height en números para futuros cálculos. Cada vez que ejecuto este código no obtuve el resultado.
const image = document.getElementById("inputimage"); const width = image.width; const height = image.height; console.log( width, height ); Pero cuando elimino Number() , obtuve resultados.
¿Alguien sabe por qué no obtuve resultados cuando se usa Number() ? ¿Hay algo que pueda hacer para convertir image.width e image.height en números?
Creo que puedes usar parseInt(image.width) (o image.height ).