Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto width and height for SVG image

Tags:

I am new to SVG, so sorry if this is a bit of a noob question. I am trying to figure out how to get an image to display with the full width and height of the referenced image. I am using D3.js to build a graph and I want a logo to appear in the corner. The problem is that the image href will be set using javascript, so the image being referenced is never a fixed width or height. What I am wanting is the image to display in it's full width and height, not scaled up or down. What I was hoping for is the ability to automatically set the width and height of the image based on it's content, such as setting the width and height attributes to auto. This however just results in the image not being shown.

d3.select("body")   .append("svg")   .attr('id','mySVG')   .attr({       "width": "100%",       "height": "100%"   })   .attr("viewBox", "0 0 " + width + " " + height )   .attr("preserveAspectRatio", "none");  d3.select('#mySVG')   .append('image')   .attr('image-rendering','optimizeQuality')   .attr('height','auto')     <--- This does not work   .attr('width','auto')      <--- This does not work   .attr('xlink:href', logoUrl)   .attr('x','0')   .attr('y','0'); 

How would I go about specifying that the SVG image width and height must be dynamically determined based on the referenced image size?

like image 989
BruceHill Avatar asked Apr 13 '13 15:04

BruceHill


People also ask

How do I set height and width in SVG?

Any height or width you set for the SVG with CSS will override the height and width attributes on the <svg> . So a rule like svg {width: 100%; height: auto;} will cancel out the dimensions and aspect ratio you set in the code, and give you the default height for inline SVG.

Does SVG have width and height?

The thing is: SVG images don't have a "size" in the sense you are probably thinking of. On the other hand, they DO have a height-to-width ratio. This ratio can usually be found in the viewBox attribute.

Are svgs resizable?

❓ How can I resize a SVG image? First, you need to add a SVG image file: drag & drop your SVG image file or click inside the white area to choose a file. Then adjust resize settings, and click the "Resize" button. After the process completes, you can download your result file.


1 Answers

I don't think 'auto' is a valid value for the 'length' attr of a svg image element. Take a look at the spec here. You might want to use '100%'

You could load the image then inspect the width and height onload.

JSFiddle: http://jsfiddle.net/WQBPC/4/

var logoUrl = 'http://placehold.it/100x50&text=Logo';  //SVG Setup stuff var svg =  d3.select("body").append("svg")   .attr('id','mySVG')   .attr({       "width": '100%',       "height": '100%'   })  var svg_img=  svg.append('image')   .attr('image-rendering','optimizeQuality')   .attr('x','0')   .attr('y','0');  //Load image and get size. var img = new Image(); img.src = logoUrl; img.onload = function() {  var width = this.width;  var height = this.height;   svg_img .attr('height', height)   .attr('width',width)   .attr('xlink:href', logoUrl)  } 
like image 148
WolfgangCodes Avatar answered Oct 09 '22 16:10

WolfgangCodes