如何获得svg图像元素的自然宽度和高度? [英] How can I get the natural width and height of an svg image element?

查看:307
本文介绍了如何获得svg图像元素的自然宽度和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做相对简单的事情.我正在尝试在D3中进行以下工作:

I'm trying to do something relatively simple. I'm trying to get the following working in D3:

  • 以自然宽度和高度将图像绘制为SVG元素.
  • 能够单击此图像以使用D3绘制圆圈.

我坚持如何将图像绘制为SVG元素.到目前为止,我有:

I'm stuck on how to draw the image as an SVG element. So far I have:

content.append("image")
    .attr("xlink:href",this.store.image_url);

但是如何获得该元素的自然宽度/高度?

But how can I get the natural width / height of this element?

推荐答案

您可以使用一些JavaScript

You can do this with a bit of javascript

var img = new Image();
img.onload = function(){

        img.naturalWidth and img.naturalHeight can be read off within here

};
img.src = this.store.image_url;

这篇关于如何获得svg图像元素的自然宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆