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
  • 20,402
  • 12
  • 60
  • 80
Armeen Harwood
  • 16,621
  • 30
  • 113
  • 222

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,673
  • 2
  • 22
  • 41
  • when a svg gets rendered in the dom how does the height width get set – Armeen Harwood 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 Harwood 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