1

Is it possible to get the natural size of a image when the image is created in javascript?

    var source = new Image();
    source.src = 'path/image.svg';
    console.log(source.src, source.clientWidth, source.clientHeight);

Are there any caveats I should be aware of when it's an svg?

altocumulus
  • 21,179
  • 13
  • 61
  • 84
Armeen Moon
  • 18,061
  • 35
  • 120
  • 233

1 Answers1

0

SVG by definition is a Scalable Vector Graphic and thus does not have a set dimension. You would need to switch to a standard image like GIF, JPG or PNG in order to retrieve the dimensions in this way.

Scott
  • 3,736
  • 2
  • 26
  • 44
  • when a svg gets rendered in the dom how does the height width get set – Armeen Moon Oct 16 '15 at 18:11
  • http://stackoverflow.com/questions/16342936/how-do-i-get-natural-dimensions-of-an-image-using-javascript-or-jquery this actually works – Armeen Moon Oct 16 '15 at 18:17
  • 2
    It might have set dimensions e.g ``. And then again it might not. If it has dimensions you can certainly retrieve them. – Robert Longson Oct 16 '15 at 18:19
  • For those bitmap formats, you need to specify whether you want its pixel dimensions or Real World measurements. PNG and JPEG may contain a resolution, but I don't think GIF has one. – Jongware Oct 16 '15 at 20:07