我只是让我的脚湿snap.svg,所以我有下面的代码加载我的SVG:
<script>
window.onload = function () {
// what is the below line of code doing ??
var g = Snap();
g.attr({
viewBox: [0, 0, 800, 600]
});
Snap.load("samsonite.svg", function (f) {
// function getShift(dot) {
// return "t" + (400 - dot.x) + "," + (300 - dot.y);
// }
console.log(f);
var gr = f.select('#outer-most-group'),
top = g.g();
top.add(gr);
});
};
</script>
现在,如果我打破正确的代码:
var g = Snap(); // create an instance of snap.
和
Snap.load("samsonite.svg", function (f) { // Load the SVG using Snap.load()
但我不理解理直气壮的代码接下来的两行:
top = g.g();
top.add(gr); // I don't know what top is at this point but the add()
// method is being used to add gr, which is basically
// the outermost <g> element in the SVG I have.
会有人请解释一下上面两行是在做什么?特别是线top = g.g()
。
对齐方法g()
创建一个“基团”元件(<g>
)所示,在将g.circle()
创建圆形元件的方法相同。所以top
指的是您刚才创建的组元素。 gr
也是一组元素;它指的是匹配选择'#outer-most-group'
的元素。因此,top.add(gr)
从所装入的SVG文件到你刚刚在你的卡的情况下创建的组添加组。
多个组元件上:https://developer.mozilla.org/en/docs/Web/SVG/Element/g