javascript - Centering svg shapes inside one another -
i faced situation want place svg shapes in center of 1 different scales. example placing rectangle or triangle or other shapes in center of circle.
i came across solutions purpose working shapes has x , y attributes rectangle. example place rectangle in center of circle this: demo
var circle =document.getelementbyid('c1'); var rect = document.getelementbyid('r1'); var group = document.getelementbyid('g1'); var bbox = rect.getbbox(); rect.setattribute('x',-(bbox.width/2)); rect.setattribute('y',-(bbox.height/2)); rect.setattribute('transform','scale(2)'); circle.setattribute('transform','scale(1)'); group.setattribute('transform','translate(200,150)'); but see sets rectangle x , y attributes. if have triangle example, dose not have x or y attribute because path. think if solution set bounding box x , y attributes of triangle or other shape work in demo
bbox.setattribute('x',-(bbox.width/2)); bbox.setattribute('y',-(bbox.height/2)); but dose not work expected because might made mistake in setting bbox attribute.
now question is there solution set bounding box attributes or there other better solution place shape in center of one?
thanks.
the bounding box (as returned getbbox() etc) read-only property. cannot adjust position of element changing bbox values.
if want reposition <path> element, have either: (a) change coordinates in d attribute, or (b) use transform attribute.
Comments
Post a Comment