I'm trying to position text within the SVG canvas, by supplying x, y coordinates
var t = paper.text(50, 50, "Raphaël\nkicks\nbutt!");
but does not position the text like all other objects...
x, y coordinates specify the center of the object! Not the "left and top most" pixel!
I would like to "left align" the text along a line, same as standard HTML.
Help would be very much appreciated.
Text-anchor property for text method is set to 'middle' by default.
If you want to left align it then change text-anchor in attributes for the object:
var t = paper.text(50, 50, "Raphaël\nkicks\nbutt!").attr({'text-anchor': 'start'});
I know you didn't say you need to vertical align it to top, but if you want to use paper.text instead of paper.print... and would like to vertical align to be top.
Try this:
function alignTop(t) { var b = t.getBBox(); var h = Math.abs(b.y2) - Math.abs(b.y) + 1; t.attr({ 'y': b.y + h }); }
And just pass the Raphael text object to it. It will top align it for you. and just call that function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With