Get dimension of a path in SVG

后端 未结 2 877
萌比男神i
萌比男神i 2020-12-09 09:03

I need to get the dimension on the screen of a in a SVG from JavaScript.

I do not have any \"transformation\" or \"scaling\" (transform, scale) on my SVG.

2条回答
  •  孤街浪徒
    2020-12-09 09:24

    You can call getBoundingClientRect() method on your path to get dimensions. It will return a ClientRect object:

    var w = myPath.getBoundingClientRect().width;
    var h = myPath.getBoundingClientRect().height;
    

    There is also a getScreenCTM() method, which returns the transformation matrix in the current screen pixels of the element it was called on. the spec says:

    [getScreenCTM()] Returns the transformation matrix from current user units (i.e., after application of the ‘transform’ attribute, if any) to the parent user agent's notice of a "pixel". [...] Note that null is returned if this element is not hooked into the document tree.

提交回复
热议问题