Is there a rect's equivalent to text's text-anchor presentation attribute?
I want to be able to position a rect from its left/right side or depending on the situation. I know that it could be done with some simple calculations, but I'm just wondering if something already built-in exists.
Link on text-anchor presentation attribute... https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/text-anchor
I was searching for the same question, and found this post with no answer. The question is pretty old, but i put here the solution for someone may look for it as i did.
When one specifies (x, y) of a svg rect it is treated like: "Place the left top corner of the rect at the point (x, y)". But one may want (x, y) to be treated as: "Hey, place the center of my rect at (x, y)" or "Hey, place the top right corner of my rect at (x, y)". For this anchor mechanics is used, but there is no such in svg.
You can still achieve anchor mechanics with transform (using css or attribute). It is possible since percent values in transform.translate are calculated relative to applied shape, but not its parent.
THE SOLUTION WORKS ONLY IN CHROME FOR NOW
So to achieve "Hey, place the center of my rect at (x, y)" you need to set anchor to (0.5, 0.5). Here how it is done with transform:
Code Snippet on jsfiddle
There is nothing like this for other SVG elements (including
rect
), see the spec. You'll have to calculate the position yourself.