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

时间:2010-10-07 10:07:05

标签: javascript raphael

目前我正在使用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> 

我想知道是否有人试图做这样的事情,并设法使其正确。

2 个答案:

答案 0 :(得分:4)

使用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 example


但我更喜欢使用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});
}


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);    
};​

答案 1 :(得分:0)

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