2012-02-26 43 views
2

有没有什么简单的方法来创建一个形状像菱形?我想有没有这样的命令:拉斐尔更具体的形状(如菱形)

var paper = new Raphael(document.getElementById('canvas_container'), 600, 600); 
var rhombus = paper.rhombus(...); 

也许有比绘制路径更简单的方法。任何想法?

回答

2

拉斐尔没有什么像paper.rhombus。但是你可以自由地扩展拉斐尔,这样你就可以得到菱形方法,就像你问的那样。

我创建了用于绘制菱形的小型fiddle,方法与您想要的相同。

来自小提琴的示例代码。

r.rhombus({ 
     cx:300, 
     cy:300, 
     side:15 
    }); 

    // a rhombus rotated around its center. 
    r.rhombus({ 
     cx:600, 
     cy:300, 
     side:15, 
     angle:45 
    }).attr({ 
     'stroke-width' :2 
    }) 

变量解释:

cx - center x position 
cy - center y position, 
side - length of side of the rhombus. 
angle - the angle through which the rhombus will be around its center (cx,cy) 

学到了很多菱形:-)

+0

感谢您的回答!我觉得必须更多地了解Javascript。 :) – bmolnar 2012-02-28 14:52:05

+0

如果您发现答案可以接受,请勾选答案。 – rajkamal 2012-02-28 17:22:24

1

我发现它麻烦的使用角度,因为它也旋转坐标系此对象。例如,稍后将其翻译为右侧时,它将沿着对角线向东南方向移动。 我使用的路径为菱形,该代码是相当短的:

this.rhombus = this.paper.path("M 100 100 l30 -30 l30 30 l-30 30 l-30 -30"); 

男:动的“笔”,以这些坐标 L:画一条线相对于此点,所以30向右和30个以上的 等。