javascript 拉斐尔的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4019110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:10:16  来源:igfitidea点击:

Raphael Position

javascriptraphael

提问by Marc

How can I get the position of an object in Raphael? I can get the size using getBBox(), but there appears to be no way to get the position?

如何在 Raphael 中获取对象的位置?我可以使用 getBBox() 获取大小,但似乎无法获取位置?

回答by Dmitry Baranovskiy

getBBox() should give you position as well as x and y properties.

getBBox() 应该给你位置以及 x 和 y 属性。

var bbox = el.getBBox();
alert([bbox.x, bbox.y]);

回答by b_dubb

getBBox() returns an object with 5 properties. they are:

getBBox() 返回一个具有 5 个属性的对象。他们是:

  1. x
  2. y
  3. width
  4. height
  5. toString()
  1. X
  2. 宽度
  3. 高度
  4. toString()

if you set getBBox( false ) it will return coordinate data for the object's position AFTER a transformation. set it to getBBox( true ) to return coordinates for the object prior to transformation

如果您设置 getBBox( false ),它将在转换后返回对象位置的坐标数据。将其设置为 getBBox( true ) 以在转换之前返回对象的坐标

use like this ...

像这样使用...

paper.Raphael(10,10,300,300);
circle.paper( 30, 55, 15 );
var circleBBox = circle.getBBox( false );

edit: just downloaded R 2.1 and i believe it has added x2 and y2 to the properties returned by getBBox()

编辑:刚刚下载了 R 2.1,我相信它已将 x2 和 y2 添加到 getBBox() 返回的属性中

回答by clarkf

Depending on what kind of shape it is, the documentation seems to say it can be accessed using the .attr()function. So, if it's a circle...

根据它的形状,文档似乎说可以使用该.attr()函数访问它。所以,如果它是一个圆...

var x = myCircle.attr('cx'); //cx is the center-x-coordinate of the circle
var y = myCircle.attr('cy'); //same, for y
var r = myCircle.attr('r'); //Radius of circle.

A square would have attrs of x, y, width, height. Check the documentation for more info.

一个正方形将有attrx、y、宽度、高度的 s。查看文档以获取更多信息。

回答by mrowe.sch

you may also access the x and y values this way:

您也可以通过这种方式访问​​ x 和 y 值:

var x = myCircle.attrs.x;
var y = myCircle.attrs.y

回答by Lego

attributes x, y are those within the set. The issue here is that if the set gets translated somewhere else, the x and y given in by .getBBOx() do not account for the translation.

属性 x, y 是集合中的那些。这里的问题是,如果集合在其他地方被翻译,.getBBOx() 给出的 x 和 y 不考虑翻译。

Raphael.transformPath(path, transform) can help by applying the same transforms that the set has...

Raphael.transformPath(path, transform) 可以通过应用与集合相同的变换来帮助......

to translate that point you can:

要翻译这一点,您可以:

tp = Raphael.transformPath("M"+x+","+y, set.attr('transform'))
x = tp[0][1]
y = tp[0][2]