score:26
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)
}
score:3
d3.select("svg")
.append("image")
.attr("id", "myImage")
.attr("xlink:href", "myImage.png")
.on("load", function(d) {
var myImage = new Image();
myImage.onload = function() {
d3.select("#myImage")
.attr("width", this.width)
.attr("height", this.height);
}
myImage.src = "myImage.png";
});
score:3
It's a couple years since this has been asked but I'm currently trying to determine some things about the use of auto
as a value for width or height myself and thought I would share what I've found. According to Amelia Bellamy-Royds' 2015 article on scaling SVGs and the codepen example she provides, if you put auto
in as either the width
or the height
value of your <svg>
, along with a value for the viewBox
, you should be able to see the content scaling proportionately. Unfortunately, she also points out that at the time this only worked in "Blink/Firefox" browsers. Blink is a fork of part of WebKit, so it's possible that by now Chrome will support this as well. I am seeing behavior in Chrome which would seem to support this possibility, but I'm also getting an error so YMMV.
Source: stackoverflow.com
Related Query
- Auto width and height for SVG image
- How can I get the natural width and height of an svg image element?
- Change height and width of the path tag in SVG Image
- D3.js: How to get the computed width and height for an arbitrary element?
- Making SVG container 100% width and height of parent container in D3 v4 (instead of by pixels)
- IE11 does not accept SVG height and width when using D3
- How to maintain the SVG marker width and height ?
- D3 treemap: reserve min width and height for the smallest node
- viewBox attribute with svg height and width attributes set
- How to get the width and height of an image? ( getBBox() size of SVG return 0 )
- D3 Bar Chart with Variable X and Y(variable width and height for bars)
- Distribute nodes evenly around central node using a relative SVG width and height
- Dynamically set the width and height of SVG based on data
- how can I set a height and width for this map in d3.js
- D3 SVG with finite height and width
- how to use getElementById with getBBox to determine the svg width and height
- Why is this SVG Path showing incorrect width and height on Firefox?
- How to set the width and height of svg to be equal to its g?
- set width and height of an SVG element - d3.js
- d3.js brush() inherits incorrect height and width From svg parent using viewBox
- Clojurescript integrating with datamaps sets svg width and height 0
- D3 how to change the width and length of SVG
- how to use svg file for image source in D3
- D3 Zoom with Scrollbars used as panning, Scrollbar width and height adjusts to zoom scale
- How to increase width and height of plotly area graph
- Loading a huge image (5mb) into svg background leads to pixelation and performance issues
- Image blurry when using url fill pattern for svg circle
- How do I replace a node with an image using D3 and CoffeeScript for a network visualization?
- How to reset svg scaling and fit to screen for random but large maps/datasets with different orientations
- Using d3js to calculate midpoints and curve path for quadratic SVG connectors
More Query from same tag
- making an arc in d3.js
- D3.js prepend (similar to jQuery prepend)
- Remove a svg element after transition in d3
- Tooltip is not getting displayed
- c3js json timeseries issue
- how can i group JSON data into the weeks according to calender?
- Object Oriented D3 JS - How to select object?
- how can i get selected path index in group using D3 control?
- Striped background in d3pie.js
- d3.js: Path intersects axis in between ticks
- Hexagonal binning with exponential random numbers
- To get the xy coordinates of dropped element on svg:image in d3
- Plotting a Histogram in D3 without knowing number of bins
- In D3.js how do I append a text element after having changed the attributes of my path element?
- Create news website with data visualizations in Django
- Why does my D3 code break when I set the script type to module?
- maintaing object constancy in transistioning donut chart
- Drag multiple elements that aren't grouped in a `g` tag?
- d3js basic javascript&css: how to I apply an array of strings (for image urls) to the existing bar chart?
- Stroke on rect in D3 blurry
- d3.event.scale contains NaN on zoom for touch devices
- Adding an animated arrow on an offset Leaflet polyline
- Binning an array in javascript for a histogram
- How to create semi circle with d3.shape that start at 9pm and ends at 6am
- Disabling layout reset on focus out in d3 chord diagram
- Is it possible to get a list of Forces currently active in a D3 Force Simulation?
- Simple Bar Plot with dimple.js and d3.js
- How to append elements to a nested layer
- D3 graphing selective portions of data set
- how can i display the value from tsv file when i have # in d3