使用D3.js插入本地SVG图像

问题描述 投票:0回答:2

我可以使用URL轻松地使用D3.js插入SVG图像,如下所示:

var img = g.append("svg:image")
    .attr("xlink:href", "http://www.clker.com/cliparts/1/4/5/a/1331068897296558865Sitting%20Racoon.svg")
    .attr("width", 200)
    .attr("height", 200)
    .attr("x", 228)
    .attr("y",53);

但是,我已经在网上到处寻找并尝试了我能想到的一切,我无法使用本地图像参考(绝对或相对路径)做同样的事情,这让我发疯。

javascript d3.js svg
2个回答
0
投票

我的版本(工作):

CSS:

   iframe {
     top: 0;
     left: 0;
     height: 200px;
     width: 200px;
     border-width: 0px;
     /* background-color: #B0B0B0; */
     border: none;
    }

JS:

(function () {
  // Prevent White Flash While iFrame Loads. Prevent variables from being global.
  // 1. Inject CSS which makes iframe invisible 
  var div = document.createElement('div'),
      ref = document.getElementsByTagName('base')[0]
         || document.getElementsByTagName('script')[0];
    div.innerHTML = '&shy;<style> iframe { visibility: hidden; } </style>';
    ref.parentNode.insertBefore(div, ref);
  // 2. When window loads, remove that CSS, making iframe visible again
    window.onload = function() {
      div.parentNode.removeChild(div);
    }
})();

HTML:

<iframe src="image\hour-glass.svg" scrolling="no" align="bottom"></iframe>

-2
投票
© www.soinside.com 2019 - 2024. All rights reserved.