简体   繁体   English

我如何在raphael.js中定位与中心相关的对象

[英]how can I position objects in raphael.js related to center

When I set x, y values to objects to position them on the paper, it measures x, y from the top left corner. 当我将x,y值设置为对象以将其放置在纸张上时,它从左上角开始测量x,y。 Is there any way to change it so that it would position related to the center middle of the paper? 有什么方法可以更改它,使其位置与纸张的中间位置相关吗?

Raphael has rectangles, circles, and ellipses. 拉斐尔有矩形,圆形和椭圆形。

  • Latter 2 are positioned with center coordinates, so you have nothing to worry about. 后2个具有中心坐标的位置,因此您不必担心。
  • Rectangle is positioned based on its top left corner coordinates 根据矩形的左上角坐标定位矩形

So to answer to your question: 因此,回答您的问题:

Raphael does not exactly provide a way to position the rectangle with center coordinates. 拉斐尔(Raphael)并未完全提供一种使用中心坐标定位矩形的方法。
However you can do that by yourself easily. 但是,您可以自己轻松地做到这一点。
Look at this DEMO . 看看这个DEMO

For example: 例如:

// lets assume your center coordinates are cx and cy
var rec = paper.rect(cx, cy, 120, 80);

// to position in the middle, just do this
var rec = paper.rect(cx - 120/2, cy - 80/2, 120, 80);

It is as simple as that. 它是如此简单。 Good luck! 祝好运!


Edit 编辑

If that is what you want to do in your project, then just Raphael.js and override the rectangle class. 如果这是您要在项目中执行的操作,则只需Raphael.js并覆盖矩形类。

Raphael does not support g element. Raphael不支持g元素。 So, you can run an loop and add attribute transform="translate(*half_of_canvas_width*, *half_of_canvas_height*)" like this: 因此,您可以运行循环并添加属性transform =“ translate(* half_of_canvas_width *,* half_of_canvas_height *)”,如下所示:

var paper = new Raphael("canvas");

var cx = 250;
var cy = 250;

var rec = paper.rect(0, 0, 250, 250).attr({fill:'red'})

var list = document.getElementById("canvas").childNodes[0].childNodes;
for(var i = 2 /*because the first two elements are desc tag*/, l = list.length; i < l; i++){
    list[i].setAttribute("transform", "translate(250,250)");
}

demo: 演示:

http://jsfiddle.net/ry8kT/150/ http://jsfiddle.net/ry8kT/150/

Good luck :) 祝好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM