Skip to content

Commit

Permalink
image support
Browse files Browse the repository at this point in the history
  • Loading branch information
rook2pawn committed Aug 22, 2012
1 parent 91f7622 commit a9a2a96
Show file tree
Hide file tree
Showing 22 changed files with 204 additions and 4 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
node-fenpgn
===========

FenPGN holds the state of a chess board along with its FEN positional notation and PGN History.
Image Output
============

Documentation incoming.

Tests
=====

Tests for this module are in nodeunit,

nodeunit ./test

though this is likely to be changed to node-tap in the future.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var h = require('./help');
var h = require('./lib/help');
exports = module.exports = fenPGN;
function fenPGN(obj) {
if (obj === undefined) {
Expand Down
133 changes: 133 additions & 0 deletions lib/board.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
var Canvas = require('canvas');
var Board = function(params) {
this.board = params.board;
this.canvas = params.canvas;
this.squareWidth = params.squareWidth;
this.canvasHeight = params.squareWidth * 8;
this.canvasWidth = params.squareWidth * 8;
this.startx = params.startx || 0;
this.starty = params.starty || 0;
this.pieces = params.pieces;

// board but at view layer
this.boardView = undefined;
var copy = function(matrix) {
var newMatrix = [];
matrix.forEach(function(row) {
newMatrix.push(row.slice(0));
});
return newMatrix;
};
var createBoardView = function(board) {
var boardView = undefined;
if (board !== undefined) {
boardView = copy(board);
for (var row = 0; row <= 7; row++) {
for (var col = 0; col <= 7; col++) {
var piece = board[row][col];
var square = {};
square.piece = piece;
boardView[row][col] = square;
}
}
}
return boardView;
}
this.boardView = createBoardView(this.board);
this.view = 'white';
this.ctx = this.canvas.getContext('2d');
// an internal canvas buffer for chessboard
var boardBuffer = new Canvas();
boardBuffer.width = this.canvasWidth;
boardBuffer.height = this.canvasHeight;

this.canvas.width = this.canvasWidth;
this.canvas.height = this.canvasHeight;
this.canvasMinX = function() {
return 0;
}
this.canvasMaxX = this.canvasMinX() + this.canvasWidth;
this.canvasMinY = function() {
return 0;
}
this.canvasMaxY = this.canvasMinY() + this.canvasHeight;
this.setBoard = function(board) {
if (board !== undefined) {
this.board = board;
this.boardView = createBoardView(board);
}
};
this.drawStatus = function() {
this.ctx.drawImage(statusBuffer,0,0);
};
this.drawAll = function() {
this.drawBoardView();
this.drawPieces(this.pieces);
};
this.isIn = function(x,y) {
return ((x > this.canvasMinX() && x < this.canvasMaxX) && (y > this.canvasMinY() && y < this.canvasMaxY))
};
// x, y are mouse event pageX and pageY coordinates i.e. evt.pageX,evt.pageY
this.screen_coordinates = function(x,y) {
var canvasx = x - this.canvasMinX();
var canvasy = y - this.canvasMinY();
var col = Math.floor(canvasx / this.squareWidth);
var row = Math.floor(canvasy / this.squareWidth);
return {col:col,row:row};
};
this.blackView_coordinates = function(x,y) {
var canvasx = x - this.canvasMinX();
var canvasy = y - this.canvasMinY();
var col = Math.floor(canvasx / this.squareWidth);
var row = Math.floor(canvasy / this.squareWidth);
return {col:col,row:row};
};
this.whiteView_coordinates = function(x,y) {
var canvasx = x - this.canvasMinX();
var canvasy = y - this.canvasMinY();
var col = Math.floor(canvasx / this.squareWidth);
var row = Math.floor(canvasy / this.squareWidth);
row = 7 - row;
return {col:col,row:row};
};
this.drawBoardView = function() {
if (this.boardView !== undefined) {
var boardViewCopy = copy(this.boardView);
if (this.view == 'white') {
boardViewCopy.reverse();
}
var ctx = this.ctx;
var startx = this.startx;
var starty = this.starty;
var modvalue = 1;
this.ctx.drawImage(boardBuffer,0,0);
}
};
this.drawPieces = function(pieces) {
if (this.board === undefined) {
return undefined;
}
var board = copy(this.board);
if (this.view == 'white') {
board.reverse();
}
var chessPieces = pieces;
var newCtx = boardBuffer.getContext('2d');
var squarewidth = this.squareWidth;
var startx = this.startx;
var starty = this.starty;
if (typeof board === 'object') {
Object.keys(board).forEach(function(key,val) {
var rownum = parseInt(key);
var rowArray = board[key];
rowArray.forEach(function(val,index) {
if ((val !== '1') && (chessPieces[val] !== undefined)) {
newCtx.drawImage(chessPieces[val],index*squarewidth+startx,rownum*squarewidth+starty,squarewidth,squarewidth);
}
});
});
}
this.ctx.drawImage(boardBuffer,0,0);
};
};
module.exports = Board;
File renamed without changes.
27 changes: 27 additions & 0 deletions lib/image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var Canvas = require('canvas');
var images = require('./loadimages');
var Board = require('./board');
var obj = {};
obj.image = function(board) {
var myCanvas = new Canvas();
var fs = require('fs')
, out = fs.createWriteStream(__dirname + '/text.png')
, stream = myCanvas.createPNGStream();

stream.on('data', function(chunk){
out.write(chunk);
});

stream.on('end', function(){
console.log('saved png');
});
console.log(Board);
var myBoard = new Board({
board: board,
canvas : myCanvas,
squareWidth: 32,
pieces: images
});
myBoard.drawAll();
};
module.exports = obj;
Binary file added lib/img/bishop_b200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/img/bishop_w200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/img/king_b200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/img/king_w200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/img/knight_b200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/img/knight_w200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/img/pawn_b200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/img/pawn_w200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/img/queen_b200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/img/queen_w200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/img/rook_b200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/img/rook_w200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions lib/loadimages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var Canvas = require('canvas');
var imgSet = function(src) {
var img = new Canvas.Image;
img.src = src;
return img;
};
var pieces = {};
pieces['b'] = imgSet('img/bishop_b200.png');
pieces['B'] = imgSet('img/bishop_w200.png');
pieces['r'] = imgSet('img/rook_b200.png');
pieces['R'] = imgSet('img/rook_w200.png');
pieces['p'] = imgSet('img/pawn_b200.png');
pieces['P'] = imgSet('img/pawn_w200.png');
pieces['q'] = imgSet('img/queen_b200.png');
pieces['Q'] = imgSet('img/queen_w200.png');
pieces['k'] = imgSet('img/king_b200.png');
pieces['K'] = imgSet('img/king_w200.png');
pieces['n'] = imgSet('img/knight_b200.png');
pieces['N'] = imgSet('img/knight_w200.png');

module.exports = pieces;
7 changes: 7 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var img = require('./image');
var fenpgn = require('../index');
var fen = new fenpgn;

console.log(fen.board());
img.image(fen.board());

Binary file added lib/text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"directories" : {
"test" : " test"
},
"dependencies" : {
"canvas" : ""
},
"devDependencies": {}
}
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var fen = require('../index');
var h = require('../help');
var h = require('../lib/help');
exports.testStateCheck = function(test) {
var newboard = h.updateBoardMSAN(h.startboard,'g1f3');
newboard = h.updateBoardMSAN(newboard,'d7d5');
Expand Down

0 comments on commit a9a2a96

Please sign in to comment.