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>
I can use:
var copy = d3.select("#group").clone(true).attr("transform", "translate(20,00)");
to have a copy of <g id="group">
and display it in page.
But I want this cloned group red "circle"
center align with that "ref_cycle"
's center in SVG, but with out lost the group shape. how I can do this in code?
thanks a lot.
here is working code my side with some comments, in case someone need it.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.