width of zero or unset are not getting rendered:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.3/d3.min.js"></script> </head> <body> <div id="viewImg"></div> <script> var w = 200; var h = 200; var svg = d3.select('#viewImg') .append('svg') .attr('width',w ) .attr('height',h ) .style('border', '1px solid #000'); svg.append('svg:image') .attr('xlink:href', 'https://en.js.cx/clipart/ball.svg') .attr('x', 20) .attr('y', 20) .attr('height', 20) .attr('width', 20) .attr('id','plz'); console.log(plz.getBBox()); </script> </body> </html>
width and it will work properly.
When you first run it, the browser hasn't loaded the image yet and reports image dimensions as 0. If you reload the page, the image is cached and Chrome (at least) reports on the correct image dimensions. If you know what your image dimensions are going to be, @Ruedi.Angehrn's answer will do the trick. If you need to use different images or don't know in advance what your image dimensions are going to be, you will need to use a different technique, for example by waiting for the page content to load fully before running getBBox().
- How to get the width and height of an image? ( getBBox() size of SVG return 0 )
- How can I get the natural width and height of an svg image element?
- how to use getElementById with getBBox to determine the svg width and height
- D3.js: How to get the computed width and height for an arbitrary element?
- How to maintain the SVG marker width and height ?
- Change height and width of the path tag in SVG Image
- How to set the width and height of svg to be equal to its g?
- How to get the height and width of text in a span not the whole span width and height
- How do I get the width of an svg element using d3js?
- Auto width and height for SVG image
- D3 how to change the width and length of SVG
- Responsive graphs: Get width and height of axis, so graph can take up remaining size of a container
- Dynamically set the width and height of SVG based on data
- How to specify height and width of a d3 tree dynamically to allow it to take up all the space it needs
- How to get the position and width of a tspan element
- How to get the zoom level of an svg image using d3
- How to get the id of svg that was draw over other svg and delete it
- how to adjust width and height when i click the rect in multilevel treeemap using d3.js?
- How can I get the D3.js axis ticks and positions as an array?
- In d3, how to get the interpolated line data from a SVG line?
- 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
- D3, SVG and textPath: How to move the text down?
- How can I set the each line color or width in SVG path
- How to use SVG gradients to display varying colors relative to the size of the colored region
- How can I display a placeholder image in my SVG until the real image is loaded?
- How can I copy the content of my SVG and append it to another SVG frame?
- How can i get the startAngle and endAngle of each arc in the sunburst example using d3.js?
- How do I create a minimap of an SVG using D3 v5 and show the current viewport dimensions?
- D3 treemap: reserve min width and height for the smallest node
More Query from same tag
- Create an interactive 3d scatter plot with D3.js and Three.js with json data
- Plotting json data to d3js graph's axis
- SVG: Draw marker on an image
- Special donut chart with different rings/arcs for positive and negative values
- Neo4j: Creating different kind of shapes for nodes
- dimple.js: unable to create stacked bar using two series
- Undefined is not a function error d3js
- Map not rendering, but data is correct
- D3 Transition: Rotation and Translation
- Asynchronous data retrieval and caching
- Include legend functionality (toggling) in D3 graph
- Efficient data storage, partitioning, and selection in d3
- Create dynamic vertical rule in d3
- D3js mulitline chart mouseOver
- d3 opposing charts axis alignment issue
- D3 dougnut chart customizing circle color
- D3.js d3.json returns Uncaught TypeError: Cannot read property '0' of undefined
- Convert flat arrays to hierarchical
- D3.js 'Pie Charts Labels' Overlapping
- nvd3.js ugly labels in pieChart
- How to update d3.js bar chart with new data
- How to escape all special characters in a string?
- d3.js - masking shapes to create transparent sectio
- Multiline chart x-axis ticks not aligned with data points
- D3 Sankey link scaling issue
- How to translate SVG and all its children
- D3 Graph not rendering when using Timescale
- How to pass a Json String from mvc controller to a d3.js chart?
- D3 Bar chart. How to set custom x and y axis based on JSON data?