Raphael JS and Text positioning?

前端 未结 4 1463
花落未央
花落未央 2020-12-23 14:02

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!\");
<         


        
4条回答
  •  情深已故
    2020-12-23 14:20

    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.

提交回复
热议问题