M
M
Maksipes2020-05-07 13:24:26
React
Maksipes, 2020-05-07 13:24:26

How can I find out the dimensions of a DOM-mounted image?

There is a component that draws a certain picture

renderImage() {
    const {
      img,
    } = this.state;

    return (
              <img
                id="bImage"
                src={img}
              />
    );
  }


you need to get the size of this image from the browser
const image = document.getElementById('bImage');
???

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Didenko, 2020-05-07
@maksipes

create a function that renders the image.

const { img } = this.state;
const image = new Image();
img.src = image;

The width and height will be in the image.width and image.height properties. But keep in mind that loading an image is an asynchronous process, so you need to access the properties in image.onload = () => {}

S
shsv382, 2020-05-07
@shsv382

uuuuuuuuuuuuuuuuu.

let image_width = image.clientWidth;
let image_height = image.clientHeight;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question