在 Raphael JavaScript 库中查找纸张/画布的绝对位置

发布于 2024-10-14 20:17:48 字数 1095 浏览 10 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

混浊又暗下来 2024-10-21 20:17:49

要获取 Raphael 纸张的绝对位置,您可以使用 jQuery:

$(paper.canvas).offset()

To get the abs position of your Raphael paper, you can use jQuery:

$(paper.canvas).offset()

寻找一个思念的角度 2024-10-21 20:17:49

接受的解决方案对我不起作用,它给了我 x = -1 和 y = -1,所以如果有人有同样的问题,我可以这样解决:

var x = paper.canvas.parentNode.offsetLeft;
var y = paper.canvas.parentNode.offsetTop;

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:

var x = paper.canvas.parentNode.offsetLeft;
var y = paper.canvas.parentNode.offsetTop;
黯淡〆 2024-10-21 20:17:48

好的。我现在明白它应该是什么了:

var x = this.paper.canvas.offsetLeft;
var y = this.paper.canvas.offsetTop;

这似乎在 IE 8.0 和 Chrome 9.0 上都能正常工作。

OK. I see what it should be now:

var x = this.paper.canvas.offsetLeft;
var y = this.paper.canvas.offsetTop;

That seems to work correctly on both IE 8.0 and Chrome 9.0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文