Raphael JS and Text positioning?
Asked Answered
C

4

46

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.

Cabanatuan answered 23/1, 2010 at 21:15 Comment(0)
C
10

Resolved!

By using the following

paper.print(100, 100, "Test string", paper.getFont("Times", 800), 30);

it now aligns text on the left.

Cabanatuan answered 23/1, 2010 at 23:40 Comment(1)
paper.print() is not the same as paper.text() because print() returns a set of objects where each object is a path resembling a character. See @dasha salo's answer for the correct "text" solution. Unfortunately there is no vertical alignment property currently.Overlying
A
151

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'});
Abridgment answered 29/9, 2010 at 19:57 Comment(2)
Probably the 4th time for me to look this question up and get your answer. First to upvote. ;)Ferocious
but is there a top anchor?Meilhac
D
20

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

Duleba answered 3/9, 2012 at 7:29 Comment(2)
As You said @Cabanatuan didn't ask for vertical align, but at least it helped me, thanks!Eugenol
Thank you good sir for the t.attr({y:}) reference, you saved my butt! :)Marlow
C
10

Resolved!

By using the following

paper.print(100, 100, "Test string", paper.getFont("Times", 800), 30);

it now aligns text on the left.

Cabanatuan answered 23/1, 2010 at 23:40 Comment(1)
paper.print() is not the same as paper.text() because print() returns a set of objects where each object is a path resembling a character. See @dasha salo's answer for the correct "text" solution. Unfortunately there is no vertical alignment property currently.Overlying
D
0

Following code works well in IE , Chrome (Firefox not tested):

var t = paper.text(50, 50, "Raphaël\nkicks\nbutt!"),
    b = t._getBBox();
t.translate(-b.width/2,-b.height/2);

Explanation:

in Raphael , text is centered around your given x & y by default, you can set left align with:

t.attr({'text-anchor':'start'})

but you have no attribute to set it top align. I firstly tried :

var b=t.getBBox(); 

but it returned NaN in IE, so I turned to:

var b=t._getBBox();

_getBBox() is undocumented but used internally by Raphael itself , and it works!

Hope it helps.

Dice answered 19/10, 2013 at 2:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.