Javascript 拖动矩形时如何重置Raphael画布大小

Javascript 拖动矩形时如何重置Raphael画布大小,javascript,canvas,raphael,Javascript,Canvas,Raphael,Raphael中的拖动功能是object.drag(移动、启动、启动) 我的想法是在开始功能中检测对象的位置,如果对象的宽度或高度大于当前画布的大小,则重置画布大小,如高度或宽度增加100 var R = Raphael("canvas",500,500); function rectangle(){ var ox = 0; var oy = 0; var rect = R.rect(100,100,10,20).attr({ fill: "white", stroke: "bl

Raphael中的拖动功能是object.drag(移动、启动、启动)

我的想法是在开始功能中检测对象的位置,如果对象的宽度或高度大于当前画布的大小,则重置画布大小,如高度或宽度增加100

var R = Raphael("canvas",500,500);

function rectangle(){
  var ox = 0; 
  var oy = 0;
  var rect = R.rect(100,100,10,20).attr({
fill: "white",
stroke: "black"
});

var start = function () {

    this.ox = this.attr("x");
    this.oy = this.attr("y");        

    this.box.ow = this.box.attr("width");
    this.box.oh = this.box.attr("height"); 

    },

    move = function (dx, dy) {

    this.attr({x: this.ox + dx, y: this.oy + dy});
    this.box.attr({width: this.box.ow + dx, height: this.box.oh + dy});

    if (this.attr("x") > R.width){
      R.width = R.width + 100;
    }

    if (this.attr("y") > R.height){
      R.height = R.height + 100;
    }

   R.setSize(R.width,R.height);

    },
    up = function () {
      ox = 0;
      oy = 0;

    };
 rect.drag(move, start, up);

  }
  window.onload = rectangle();

矩形是可拖动的,但是画布的大小不会随着矩形的位置而更新。那么我的代码怎么了?有人能帮忙吗

如果您有一个矩形,通常认为x和y坐标位于左上角。如果在画布内拖动矩形,则画布外不可能有x和/或y

而不是检查

if (this.attr("x") > R.width){
      R.width = R.width + 100;
}

if (this.attr("y") > R.height){
  R.height = R.height + 100;
}
尝试检查类似于
(R.width-rect.width)
,无论矩形的值是多少。对你的R.height和矩形的height也做同样的事情

意思是:

if (this.attr("x") > R.width-rectangleWidth){
      R.width = R.width + 100;
}

if (this.attr("y") > R.height-rectangleHeight){
  R.height = R.height + 100;
}

谢谢你回答这个问题。我尝试了新的代码,但是,画布的大小仍然没有随着矩形的移动而更新。