最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Finding the absolute position of the papercanvas in the Raphael JavaScript library - Stack Overflow

programmeradmin5浏览0评论

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 plicated.)

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 plicated.)

Share Improve this question edited Jun 18, 2015 at 10:19 Peter O. 32.9k14 gold badges85 silver badges97 bronze badges asked Jan 30, 2011 at 4:28 agarrettagarrett 4331 gold badge5 silver badges15 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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.

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

$(paper.canvas).offset()

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;
发布评论

评论列表(0)

  1. 暂无评论