繁体   English   中英

如何克隆svg中的一组元素并在指定坐标显示克隆?

[英]How to clone a group of elements in svg and show the clone in specified coordinate?

SVG

<svg width="200" height="200">
    <g id="group">
        <rect x="10" y="10" width="50" height="20" fill="teal"></rect>
        <circle cx="35" cy="40" r="20" fill="red"></circle>
    </g>

    <circle id="ref_cycle" cx="135" cy="140" r="2" fill="green"></circle>

</svg>

我可以用:

var copy = d3.select("#group").clone(true).attr("transform", "translate(20,00)"); 

拥有<g id="group">的副本并将其显示在页面中。

但我希望这个克隆的组red "circle"中心与 SVG 中的"ref_cycle"中心对齐,但不会丢失组形状。 我怎么能在代码中做到这一点?

多谢。

这是我这边的工作代码,有一些评论,以防有人需要。

    var ref_cycle = d3.select("#ref_cycle"); 
        //^ get the reference cycle obj

    var clone_template = d3.select("#group"); 
        //^ get the template obj, for later select and clone use

    var clone_trans_x = ref_cycle.attr("cx") - clone_template.select("#inner-ref").attr("cx");
    var clone_trans_y = ref_cycle.attr("cy") - clone_template.select("#inner-ref").attr("cy");
        // to make select be simple, add an id tag in svg group's <circle id="inner-ref" ></circle> first.
        // do the math on offset of two objects    

    clone_template.clone(true).attr("transform", "translate(" + [clone_trans_x, clone_trans_y] + ")");
       // clone it 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM