如何在javascript中检查画布的上、右、左值

如何在javascript中检查画布的上、右、左值,javascript,Javascript,我画了一个正方形和面积。现在我只检查底部的画布正方形。例如:当正方形移动到顶部时(如果顶部==正方形,则停止) 我要检查画布的顶部、右侧、左侧。如何做到这一点?您可以使用jQuery()检查任何属性的值 或者您可以使用jQuery()获得CSS样式。假设您的x和y位于左下角 this.hitBottom = function() { var rbottom = myGameArea.canvas.height - this.height; if (this.y > rbottom)

我画了一个正方形和面积。现在我只检查底部的画布正方形。例如:当正方形移动到顶部时(如果顶部==正方形,则停止)


我要检查画布的顶部、右侧、左侧。如何做到这一点?

您可以使用jQuery()检查任何属性的值


或者您可以使用jQuery()获得CSS样式。

假设您的x和y位于左下角

this.hitBottom = function() {
  var rbottom = myGameArea.canvas.height - this.height;
  if (this.y > rbottom) {
    this.y = rbottom;
    //this.gravitySpeed=0;
  }
}
this.hitTop = function() {
  if (this.y - this.height < 0) {
    this.y = this.height;
    //this.gravitySpeed=0;
  }
}
this.hitLeft = function() {
  if (this.x < 0) {
    this.x = 0;
    //this.gravitySpeed=0;
  }
}
this.hitRight = function() {
  if (this.x > myGameArea.canvas.width - this.width) {
    this.x = myGameArea.canvas.width - this.width;
    //this.gravitySpeed=0;
  }
}
this.hitbooth=函数(){
var rbottom=myGameArea.canvas.height—this.height;
如果(this.y>rbotom){
这个。y=rbottom;
//这个。重力速度=0;
}
}
this.hitTop=函数(){
if(this.y-this.height<0){
this.y=this.height;
//这个。重力速度=0;
}
}
this.hitLeft=函数(){
如果(此.x<0){
这个.x=0;
//这个。重力速度=0;
}
}
this.hitRight=函数(){
if(this.x>myGameArea.canvas.width-this.width){
this.x=myGameArea.canvas.width-this.width;
//这个。重力速度=0;
}
}
this.hitBottom = function() {
  var rbottom = myGameArea.canvas.height - this.height;
  if (this.y > rbottom) {
    this.y = rbottom;
    //this.gravitySpeed=0;
  }
}
this.hitTop = function() {
  if (this.y - this.height < 0) {
    this.y = this.height;
    //this.gravitySpeed=0;
  }
}
this.hitLeft = function() {
  if (this.x < 0) {
    this.x = 0;
    //this.gravitySpeed=0;
  }
}
this.hitRight = function() {
  if (this.x > myGameArea.canvas.width - this.width) {
    this.x = myGameArea.canvas.width - this.width;
    //this.gravitySpeed=0;
  }
}