javascript - Getting the Screen Pixel coordinates of a Rect element -
i trying screen pixel coordinates of rectangle in svg via java script. when rectangle has been clicked, can figure out width, height, x , y position getbbox().
but these positions original positions. want screen position.
for example if manipulate viewbox of whole svg, these getbbox coordinates not more same screen pixels. there function or way coordinates considering current viewbox , pixel size of svg element?
demo: http://phrogz.net/svg/screen_location_for_element.xhtml
var svg = document.queryselector('svg'); var pt = svg.createsvgpoint(); function screencoordsforrect(rect){ var corners = {}; var matrix = rect.getscreenctm(); pt.x = rect.x.animval.value; pt.y = rect.y.animval.value; corners.nw = pt.matrixtransform(matrix); pt.x += rect.width.animval.value; corners.ne = pt.matrixtransform(matrix); pt.y += rect.height.animval.value; corners.se = pt.matrixtransform(matrix); pt.x -= rect.width.animval.value; corners.sw = pt.matrixtransform(matrix); return corners; }
the magenta squares absolutely-positioned divs in html element, using screen space coordinates. when drag or resize rectangles function called , moves corner divs on 4 corners (lines 116-126). note works when rectangles in arbitrary nested transformation (e.g. blue rectangle) , svg scaled (resize browser window).
for fun, drag 1 of rectangles off edge of svg canvas , notice screen-space corners staying on unseen dots.
Comments
Post a Comment