How do you center a rectangle in javaScript? Having the X and Y position of the rectangle being the center of the rectangle How do you center a rectangle in javaScript? Having the X and Y position of the rectangle being the center of the rectangle

How do you center a rectangle in javaScript? Having the X and Y position of the rectangle being the center of the rectangle. This is a rough idea for my code.

var cxt = canvas.getContext("2d");
var canvas = var canvas = document.getElementById("myCanvas");

function createSprite(x,y,width,height) {
 this.x=x;
 this.y=y;
 this.width=width;
 this.height=height;

 this.display(){
 
 cxt.beginPath();
 cxt.rect(this.x,this.y,this.width,this.height);
 cxt.fill();
 cxt.stroke();
 
 }

}


from Stackoverflow

Comments

Popular posts from this blog

With jQuery, how to automatically select the value of an input text that has been modified with the arrows? With jQuery, how to automatically select the value of an input text that has been modified with the arrows?

Cannot create an instance of a defined class in Javascript Cannot create an instance of a defined class in Javascript

Using Ts-Standard linting, JSDoc comment at beginning of file triggers "This rule requires the `strictNullChecks` compiler option" error Using Ts-Standard linting, JSDoc comment at beginning of file triggers "This rule requires the `strictNullChecks` compiler option" error