Aligning the objects in SVG center

前端 未结 3 1854
情话喂你
情话喂你 2021-01-21 04:09

How can i align objects within svg tag center. I can make it aligned by adjusting the x,y cordiantes in viewbox. But every time i need to change the value according to the size

3条回答
  •  春和景丽
    2021-01-21 05:04

    you can use getBBox() to get the size of your path, and then adjust your viewBox accordingly...

    var box=path.getBBox()
    svg.setAttribute("viewBox",`${box.x} ${box.y} ${box.width} ${box.height}`)
    
    console.log(box)
    svg{border:1px solid red}
    
    	
    

    P.S.: just do this once and change your viewBox manually in your code...

提交回复
热议问题