在 Raphael JavaScript 库中查找纸张/画布的绝对位置
使用 Raphael JavaScript 库时如何找到纸张/画布的绝对位置?
例如,假设最小的工作示例如下:
<html>
<head>
<script type="text/javascript" src="raphael.js"></script>
<script>
window.onload = function() {
var size = 600;
var paper = new Raphael(document.getElementById("canvas_container"),
size, size);
var c = paper.circle(100, 100, 50).attr({fill: "#00f"});
var x = 0; // get the paper's absolute x coordinate
var y = 0; // get the paper's absolute y coordinate
document.getElementById("coordinates").innerHTML = x + " " + y;
};
</script>
<style type="text/css">
#canvas_container {
width: 600px;
margin-left: auto;
margin-right: auto;
border: 1px solid #aaa;
}
</style>
</head>
<body>
<p id="coordinates"></p>
<div id="canvas_container"></div>
</body>
</html>
如何在不知道纸张所在 div 的 ID 的情况下从页面顶部找到纸张的绝对 x 和 y 坐标? (在示例中,我可以知道这一点,但我的实际情况使得了解 div ID 变得更加复杂。)
How can I find the absolute position of the paper/canvas when using the Raphael JavaScript library?
For instance, suppose the minimal working example is as follows:
<html>
<head>
<script type="text/javascript" src="raphael.js"></script>
<script>
window.onload = function() {
var size = 600;
var paper = new Raphael(document.getElementById("canvas_container"),
size, size);
var c = paper.circle(100, 100, 50).attr({fill: "#00f"});
var x = 0; // get the paper's absolute x coordinate
var y = 0; // get the paper's absolute y coordinate
document.getElementById("coordinates").innerHTML = x + " " + y;
};
</script>
<style type="text/css">
#canvas_container {
width: 600px;
margin-left: auto;
margin-right: auto;
border: 1px solid #aaa;
}
</style>
</head>
<body>
<p id="coordinates"></p>
<div id="canvas_container"></div>
</body>
</html>
How do I find the absolute x and y coordinates of the paper from the top of the page without knowing the ID of the div that it lives in? (In the example, I can know that, but my actual situation makes knowing the div ID much more complicated.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要获取 Raphael 纸张的绝对位置,您可以使用 jQuery:
$(paper.canvas).offset()
To get the abs position of your Raphael paper, you can use jQuery:
$(paper.canvas).offset()
接受的解决方案对我不起作用,它给了我 x = -1 和 y = -1,所以如果有人有同样的问题,我可以这样解决:
The accepted solution doesn't worked for me, it gives me x = -1 and y = -1, so if someone has the same problem I solved it this way:
好的。我现在明白它应该是什么了:
这似乎在 IE 8.0 和 Chrome 9.0 上都能正常工作。
OK. I see what it should be now:
That seems to work correctly on both IE 8.0 and Chrome 9.0.