【发布时间】:2014-10-24 16:49:58
【问题描述】:
我有几个想要展示的外部 SVG 文件(路径和多边形的各种形状)。对于每种形状,我想在随机位置以不同的笔触颜色呈现它们。
我可以使用d3.xml()一次成功读取一个,更改其属性等。
var sampleSVG = d3.select("body")
.append("svg")
.attr("width", 300)
.attr("height", 300);
var shapes = ["https://dl.dropboxusercontent.com/u/2467665/shapes-24.svg",
"https://dl.dropboxusercontent.com/u/2467665/shapes-23.svg"];
d3.xml("https://dl.dropboxusercontent.com/u/2467665/shapes-24.svg", "image/svg+xml",
function(xml) {
sampleSVG.node().appendChild(xml.documentElement);
var innerSVG = sampleSVG.select("svg");
innerSVG
.select("*")//selects whatever the shape is, be it path or polygon
.attr("transform", "translate("+Math.floor(Math.random() * 100)+","+Math.floor(Math.random() * 100)+") scale(.3)")
.attr("stroke", "purple");
});
从我的阵列中读取多个外部文件的最佳方法是什么?以及如何独立操作每个文件的位置和属性?
【问题讨论】: