I have read 5+ other questions on this and they are all different from mine, because I am drawing more than one image, and because I set the width and height of my canvas in my html and not my css.
<canvas id="myCanvas" width="480" height="320"></canvas>
This is how I am drawing my images (I am making a chess game)
function piece(color, piece, square, pieceId){
this.dragging=false;
this.color=color;
this.piece=piece;
this.square=square;
this.moveCount=0;
this.pieceId = pieceId;
this.captured = false;
this.firstMove;
this.xCoord=square.x+(spaceWidth/2-8);
this.yCoord=square.y+(spaceHeight/2-8);
this.drawPiece = function(){
if (this.color == 'w') {
// need to get rid of these magic numbers don't really know what i am doing
if (this.piece == 'p') {
loadImage("img/whitepawn.png", this.xCoord-4, this.yCoord-3);
} else if (this.piece == 'b') {
loadImage("img/whitebishop.png", this.xCoord-4, this.yCoord-3); } else if (this.piece == 'kn') {
loadImage("img/whiteknight.png", this.xCoord-4, this.yCoord-3); } else if (this.piece == 'r') {
loadImage("img/whiterook.png", this.xCoord-4, this.yCoord-3);
} else if (this.piece == 'k') {
loadImage("img/whiteking.png", this.xCoord-4, this.yCoord-3);
} else if (this.piece == 'q') {
loadImage("img/whitequeen.png", this.xCoord-4, this.yCoord-3); }
} else if (this.color == 'b') { // black piece
// need to get rid of these magic numbers don't really know what i am doing
if (this.piece == 'p') {
loadImage("img/blackpawn.png", this.xCoord-4, this.yCoord-3);
} else if (this.piece == 'b') {
loadImage("img/blackbishop.png", this.xCoord-4, this.yCoord-3); } else if (this.piece == 'kn') {
loadImage("img/blackknight.png", this.xCoord-4, this.yCoord-3); } else if (this.piece == 'r') {
loadImage("img/blackrook.png", this.xCoord-4, this.yCoord-3);
} else if (this.piece == 'k') {
loadImage("img/blackking.png", this.xCoord-4, this.yCoord-3);
} else if (this.piece == 'q') {
loadImage("img/blackqueen.png", this.xCoord-4, this.yCoord-3); }
} else {
;
}
}
}
function loadImage( src, x, y) {
var imageObj = new Image();
imageObj.src = src;
imageObj.onload = function() {
ctx.imageSmoothingEnabled = false;
ctx.drawImage(imageObj, x, y, 25, 25);
};
}
Does anyone know what I am doing wrong? I have been trying to figure this out all day, and could use some help.