使用Raphael的库来绘制左对齐的文本

use*_*167 4 javascript alignment raphael

我正在使用Raphael的JavaScript库(http://raphaeljs.com/)将一些图形绘制到我的Web应用程序中.我尝试了以下代码:

<html>
<head>
    <title></title>
    <script type="text/javascript" src="raphael/raphael.js"></script>
</head>
<body>    
    <script>
        var paper = Raphael(10, 50, 600, 200);            
        paper.text(300, 50, "first very very long line\nshort line").attr(
            {"font-family":"arial", 
            "font-size":"30",
            "text-align":"left"}
            );
    </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

结果是一个图形,其中两行文本居中.css font-family和font-size显示正确.但是css text-align被忽略了.为什么?

(使用Firefox 8.0和Chrome 15测试)

Spo*_*ike 14

看来,拉斐尔不使用text-align属性.而是使用text-anchor属性,其可能的值是start,middle,endinherit.

在您的示例中使用它如下:

paper.text(300, 50, "first very very long line\nshort line").attr({
    "font-family":"arial", 
    "font-size":"30",
    "text-anchor":"start"
});
Run Code Online (Sandbox Code Playgroud)