-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
204 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,8 @@ | |
"directories" : { | ||
"test" : " test" | ||
}, | ||
"dependencies" : { | ||
"canvas" : "" | ||
}, | ||
"devDependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters