Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raphael JS and Text positioning?

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.

like image 519
RadiantHex Avatar asked Jan 23 '10 21:01

RadiantHex


2 Answers

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'}); 
like image 55
Dasha Salo Avatar answered Sep 20 '22 08:09

Dasha Salo


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

like image 33
Jimmy Chandra Avatar answered Sep 18 '22 08:09

Jimmy Chandra