Compare commits
2 Commits
751cc2cc04
...
0540b262dd
| Author | SHA1 | Date | |
|---|---|---|---|
|
0540b262dd
|
|||
|
a3ae7c9f71
|
146
frontend/main.js
146
frontend/main.js
@@ -38,45 +38,118 @@ function drawChessBoard(canvas) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function drawPiece(canvas, piece, field) {
|
function computeScreenCoords(position, canvas) {
|
||||||
let canvasRect = canvas.getBoundingClientRect();
|
let canvasRect = canvas.getBoundingClientRect();
|
||||||
let rc = boardIndexToRowCol(field);
|
let rc = boardIndexToRowCol(position);
|
||||||
|
|
||||||
let im = new Image(BOX_SIDE, BOX_SIDE);
|
|
||||||
im.src = `./icons/${piece}.svg`;
|
|
||||||
im.onload = () => {
|
|
||||||
let topPx = canvasRect.top + INDEX_MARGIN + rc[0]*BOX_SIDE + window.pageYOffset;
|
let topPx = canvasRect.top + INDEX_MARGIN + rc[0]*BOX_SIDE + window.pageYOffset;
|
||||||
let leftPx = canvasRect.left + INDEX_MARGIN + rc[1]*BOX_SIDE + window.pageXOffset;
|
let leftPx = canvasRect.left + INDEX_MARGIN + rc[1]*BOX_SIDE + window.pageXOffset;
|
||||||
|
return [topPx, leftPx]
|
||||||
|
}
|
||||||
|
|
||||||
|
class Move {
|
||||||
|
constructor(kind, from, to) {
|
||||||
|
this.kind = kind;
|
||||||
|
this.from = from;
|
||||||
|
this.to = to;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Piece {
|
||||||
|
constructor(who, color, position) {
|
||||||
|
this.who = who;
|
||||||
|
this.color = color;
|
||||||
|
this.position = position;
|
||||||
|
this._im = null;
|
||||||
|
}
|
||||||
|
icon() {
|
||||||
|
return `./icons/${this.who}_${this.color}.svg`
|
||||||
|
}
|
||||||
|
draw(canvas) {
|
||||||
|
let [topPx, leftPx] = computeScreenCoords(this.position, canvas);
|
||||||
|
if (this._im === null) {
|
||||||
|
let im = new Image(BOX_SIDE, BOX_SIDE);
|
||||||
|
im.src = this.icon();
|
||||||
|
im.onload = () => {
|
||||||
im.style.position = 'absolute';
|
im.style.position = 'absolute';
|
||||||
im.style.top = `${topPx}px`;
|
im.style.top = `${topPx}px`;
|
||||||
im.style.left = `${leftPx}px`;
|
im.style.left = `${leftPx}px`;
|
||||||
};
|
};
|
||||||
document.body.appendChild(im);
|
document.body.appendChild(im);
|
||||||
|
this._im = im;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this._im.style.top = `${topPx}px`;
|
||||||
|
this._im.style.left = `${leftPx}px`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
undraw() {
|
||||||
|
if (this._im !== null) {
|
||||||
|
this._im.remove();
|
||||||
|
this._im = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function _setupBoard(canvas) {
|
class Configuration {
|
||||||
drawPiece(_canvas, 'rook_black', 'a8');
|
constructor(pieces) {
|
||||||
drawPiece(_canvas, 'rook_black', 'h8');
|
this.pieces = pieces
|
||||||
drawPiece(_canvas, 'knight_black', 'b8');
|
}
|
||||||
drawPiece(_canvas, 'knight_black', 'g8');
|
draw(canvas) {
|
||||||
drawPiece(_canvas, 'bishop_black', 'c8');
|
for (let piece of this.pieces) {
|
||||||
drawPiece(_canvas, 'bishop_black', 'f8');
|
piece.draw(canvas);
|
||||||
drawPiece(_canvas, 'queen_black', 'd8');
|
}
|
||||||
drawPiece(_canvas, 'king_black', 'e8');
|
}
|
||||||
|
getAt(position) {
|
||||||
for (let colLetter of INDEX_LETTERS) {
|
for (let piece of this.pieces) {
|
||||||
drawPiece(canvas, 'pawn_black', `${colLetter}7`);
|
if (piece.position === position) return piece;
|
||||||
drawPiece(canvas, 'pawn_white', `${colLetter}2`);
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
dropAt(position) {
|
||||||
|
for (let idx = 0; idx < this.pieces.length; idx++) {
|
||||||
|
if (this.pieces[idx].position === position) {
|
||||||
|
this.pieces[idx].undraw();
|
||||||
|
this.pieces.splice(idx, 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
makeMove(move, canvas) {
|
||||||
|
let piece = this.getAt(move.from);
|
||||||
|
if (piece === null) return;
|
||||||
|
this.dropAt(move.to);
|
||||||
|
piece.position = move.to;
|
||||||
|
piece.draw(canvas);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
drawPiece(_canvas, 'rook_white', 'a1');
|
function setupBoard() {
|
||||||
drawPiece(_canvas, 'rook_white', 'h1');
|
let pawns = [];
|
||||||
drawPiece(_canvas, 'knight_white', 'b1');
|
for (let x of INDEX_LETTERS) {
|
||||||
drawPiece(_canvas, 'knight_white', 'g1');
|
pawns.push(new Piece('pawn', 'black', `${x}7`),
|
||||||
drawPiece(_canvas, 'bishop_white', 'c1');
|
new Piece('pawn', 'white', `${x}2`));
|
||||||
drawPiece(_canvas, 'bishop_white', 'f1');
|
}
|
||||||
drawPiece(_canvas, 'queen_white', 'd1');
|
return new Configuration([
|
||||||
drawPiece(_canvas, 'king_white', 'e1');
|
new Piece('rook', 'black', 'a8'),
|
||||||
|
new Piece('rook', 'black', 'h8'),
|
||||||
|
new Piece('knight', 'black', 'b8'),
|
||||||
|
new Piece('knight', 'black', 'g8'),
|
||||||
|
new Piece('bishop', 'black', 'c8'),
|
||||||
|
new Piece('bishop', 'black', 'f8'),
|
||||||
|
new Piece('queen', 'black', 'd8'),
|
||||||
|
new Piece('king', 'black', 'e8'),
|
||||||
|
|
||||||
|
...pawns,
|
||||||
|
|
||||||
|
new Piece('rook', 'white', 'a1'),
|
||||||
|
new Piece('rook', 'white', 'h1'),
|
||||||
|
new Piece('knight', 'white', 'b1'),
|
||||||
|
new Piece('knight', 'white', 'g1'),
|
||||||
|
new Piece('bishop', 'white', 'c1'),
|
||||||
|
new Piece('bishop', 'white', 'f1'),
|
||||||
|
new Piece('queen', 'white', 'd1'),
|
||||||
|
new Piece('king', 'white', 'e1'),
|
||||||
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
function boardIndexToRowCol(boardIndex) {
|
function boardIndexToRowCol(boardIndex) {
|
||||||
@@ -87,6 +160,19 @@ function boardIndexToRowCol(boardIndex) {
|
|||||||
return [row, col];
|
return [row, col];
|
||||||
}
|
}
|
||||||
|
|
||||||
var _canvas = createCanvas();
|
async function demo() {
|
||||||
drawChessBoard(_canvas);
|
let canvas = createCanvas();
|
||||||
_setupBoard(_canvas);
|
drawChessBoard(canvas);
|
||||||
|
let config = setupBoard();
|
||||||
|
config.draw(canvas);
|
||||||
|
await new Promise(r => setTimeout(r, 6000));
|
||||||
|
config.makeMove({'kind': 'xxx', 'from': 'e2', 'to': 'e4'}, canvas);
|
||||||
|
await new Promise(r => setTimeout(r, 2000));
|
||||||
|
config.makeMove({'kind': 'xxx', 'from': 'e7', 'to': 'e5'}, canvas);
|
||||||
|
await new Promise(r => setTimeout(r, 2000));
|
||||||
|
config.makeMove({'kind': 'xxx', 'from': 'd2', 'to': 'd4'}, canvas);
|
||||||
|
await new Promise(r => setTimeout(r, 2000));
|
||||||
|
config.makeMove({'kind': 'xxx', 'from': 'e5', 'to': 'd4'}, canvas);
|
||||||
|
}
|
||||||
|
|
||||||
|
demo();
|
||||||
|
|||||||
Reference in New Issue
Block a user