检查对象是否超出画布(Raphael.js)

发布于 2024-09-26 07:37:39 字数 1586 浏览 0 评论 0原文

目前我正在使用 Raphael.js 在画布周围拖放一个圆圈。但是,我想禁止用户将圆圈拖出画布。我的代码通常如下:

<script type="text/javascript" src="Javascript/raphael.js"></script>
    <script type="text/javascript">
        window.onload = function() {  
            //create canvas matching the image's dimensions
            var paper = new Raphael(document.getElementById('canvas_container'), <%=width%>, <%=height%>);

            //put the schematic image onto the canvas
            paper.image("<%=path%>",0,0,<%=width%>,<%=height%>);

            //create the marker on the canvas
            var c = paper.circle(<%=markerx%>, <%=markery%>, 5).attr({
                fill: "#800517",
                stroke: "none"

            });
            var start = function () {
                // storing original coordinates
                this.ox = this.attr("cx");
                this.oy = this.attr("cy");                    
            },
            move = function (dx, dy) {
                // move will be called with dx and dy
                if (this.ox + dx >= <%=width%> || this.oy + dy >= <%=height%>){
                    this.attr({cx: <%=width%>, cy: <%=height%>});
                }else{
                    this.attr({cx: this.ox + dx, cy: this.oy + dy});
                }
            },
            up = function () {
                // restoring state                    
            };

            c.drag(move, start, up);

        }
    </script> 

我想知道是否有人尝试过做这样的事情并设法做到正确。

currently I am using Raphael.js to drag and drop a circle around a canvas. However, I would like to disallow the user to drag the circle out of the canvas. My code is generally as follows:

<script type="text/javascript" src="Javascript/raphael.js"></script>
    <script type="text/javascript">
        window.onload = function() {  
            //create canvas matching the image's dimensions
            var paper = new Raphael(document.getElementById('canvas_container'), <%=width%>, <%=height%>);

            //put the schematic image onto the canvas
            paper.image("<%=path%>",0,0,<%=width%>,<%=height%>);

            //create the marker on the canvas
            var c = paper.circle(<%=markerx%>, <%=markery%>, 5).attr({
                fill: "#800517",
                stroke: "none"

            });
            var start = function () {
                // storing original coordinates
                this.ox = this.attr("cx");
                this.oy = this.attr("cy");                    
            },
            move = function (dx, dy) {
                // move will be called with dx and dy
                if (this.ox + dx >= <%=width%> || this.oy + dy >= <%=height%>){
                    this.attr({cx: <%=width%>, cy: <%=height%>});
                }else{
                    this.attr({cx: this.ox + dx, cy: this.oy + dy});
                }
            },
            up = function () {
                // restoring state                    
            };

            c.drag(move, start, up);

        }
    </script> 

I was wondering if there was anyone out there who has tried to do something like this and managed to get it right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

美人如玉 2024-10-03 07:37:39

使用 if 语句,如果您不想移动,则无需执行任何操作。另外,不要忘记圆的半径,也不要忘记检查值是否太小,而不仅仅是太大:

                                                        // If the new position is
           if (this.ox + dx <= width - radius &&        // not too far right
               this.oy + dy <= height - radius &&       // and not too far down
               this.ox + dx >= 0 + radius &&            // and not too far left
               this.oy + dy >= 0 + radius)              // and not too far up
           {                                            // ... then move
                this.attr({cx: this.ox + dx, cy: this.oy + dy});  
           }                                            // else nothing

jsFiddle 示例

但我更喜欢使用 minmax

var nowX, nowY, w = ..., h=..., r=...,
move = function (dx, dy) {
    // move will be called with dx and dy
    // restrict movement of circle to within boundaries
        // You can combine the following 4 lines into 2 or even 1,
        //     but I left it as 4 for readability.
    nowX = Math.max(0 + r, this.ox + dx);
    nowY = Math.max(0 + r, this.oy + dy);
    nowX = Math.min(w - r, nowX);
    nowY = Math.min(h - r, nowY);    
    this.attr({cx: nowX, cy: nowY});
}

所以,这是一个完整的工作示例 (jsFiddle)

window.onload = function() {
var nowX, nowY, w = 300, h=300, r=30, R = Raphael("canvas", w, h),   
    c = R.circle(100, 100, r).attr({
    fill: "hsb(.8, 1, 1)",
    stroke: "none",
    opacity: .5,
    cursor: "move"
});
var start = function () {
    // storing original coordinates
    this.ox = this.attr("cx");
    this.oy = this.attr("cy");
    this.attr({opacity: 1});
},
move = function (dx, dy) {
    // move will be called with dx and dy
    // restrict movement of circle to within boundaries
    nowX = Math.max(0 + r, this.ox + dx);
    nowY = Math.max(0 + r, this.oy + dy);
    nowX = Math.min(w - r, nowX);
    nowY = Math.min(h - r, nowY);    
    this.attr({cx: nowX, cy: nowY});
},
up = function () {
    // restoring state
    this.attr({opacity: .5});
};
c.drag(move, start, up);    
};​

With an if statement, you don't have to do anything if you don't want to move. Also, don't forget about the radius of the circle, and don't forget about checking for the value being to small, not just too big:

                                                        // If the new position is
           if (this.ox + dx <= width - radius &&        // not too far right
               this.oy + dy <= height - radius &&       // and not too far down
               this.ox + dx >= 0 + radius &&            // and not too far left
               this.oy + dy >= 0 + radius)              // and not too far up
           {                                            // ... then move
                this.attr({cx: this.ox + dx, cy: this.oy + dy});  
           }                                            // else nothing

jsFiddle example

But I like to use min and max more:

var nowX, nowY, w = ..., h=..., r=...,
move = function (dx, dy) {
    // move will be called with dx and dy
    // restrict movement of circle to within boundaries
        // You can combine the following 4 lines into 2 or even 1,
        //     but I left it as 4 for readability.
    nowX = Math.max(0 + r, this.ox + dx);
    nowY = Math.max(0 + r, this.oy + dy);
    nowX = Math.min(w - r, nowX);
    nowY = Math.min(h - r, nowY);    
    this.attr({cx: nowX, cy: nowY});
}

So, here's a full working example (jsFiddle)

window.onload = function() {
var nowX, nowY, w = 300, h=300, r=30, R = Raphael("canvas", w, h),   
    c = R.circle(100, 100, r).attr({
    fill: "hsb(.8, 1, 1)",
    stroke: "none",
    opacity: .5,
    cursor: "move"
});
var start = function () {
    // storing original coordinates
    this.ox = this.attr("cx");
    this.oy = this.attr("cy");
    this.attr({opacity: 1});
},
move = function (dx, dy) {
    // move will be called with dx and dy
    // restrict movement of circle to within boundaries
    nowX = Math.max(0 + r, this.ox + dx);
    nowY = Math.max(0 + r, this.oy + dy);
    nowX = Math.min(w - r, nowX);
    nowY = Math.min(h - r, nowY);    
    this.attr({cx: nowX, cy: nowY});
},
up = function () {
    // restoring state
    this.attr({opacity: .5});
};
c.drag(move, start, up);    
};​
Hello爱情风 2024-10-03 07:37:39

嘿伙计们,我设法解决了它。我意识到我不应该使用 this.attr 来设置 cx 和 cy 相反,我应该使用 c.attr 来识别我的圈子的 attr。

Hey guys, i managed to solve it. I realised it was I should not have used this.attr to set the cx and cy instead I should've used c.attr to identify the attr to my circle instead.

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