Raphael JS基于中心坐标定位矩形

时间:2013-07-15 18:57:57

标签: javascript raphael

有没有办法根据中心坐标在Raphael.js中定位矩形(就像定位圆圈一样)?

2 个答案:

答案 0 :(得分:0)

你可以通过简单地创建自己的自定义元素来实现,这里有一个如何完成它的例子:

Raphael.fn.MyRect = function( cx, cy, width, height ) {
    var xLeftTop = cx - (width / 2);
    var yLeftTop = cy - (height / 2);
    this.rectObj = paper.rect( xLeftTop, yLeftTop, width, height );
    return this;
};

var paper = Raphael(10, 50, 320, 200);
paper.MyRect( 95, 35, 50, 50 ); // 95 is the center in x and 35 the center in y

一个实例:http://jsfiddle.net/7QMbH/

这样,您可以创建任意数量的矩形,并使您的代码易于理解。

答案 1 :(得分:0)

最简单的方法是创建坐标为 x - rect_width/2 y - rect_heigth/2 的矩形:

示例:

var rect_w = 180,
    rect_h = 80;

var paper = Raphael(10, 10, 500, 500); 
    paper.rect(100 - rect_w/2, 100 - rect_h/2, rect_w, rect_h, 5);

// The last 5 gives curved edges to you rectangle (if you need it of course).

基本上,不是左上角的100,100,而是10,60祝你好运

相关问题