Skip to content

Instantly share code, notes, and snippets.

@slior

slior/drawing.js Secret

Created November 1, 2020 11:39
Show Gist options
  • Save slior/9ebb007f02243f89c85fb1018cdf3e78 to your computer and use it in GitHub Desktop.
Save slior/9ebb007f02243f89c85fb1018cdf3e78 to your computer and use it in GitHub Desktop.
Mancala (0f71c1e427a9d272094845910c92d7dba462034d) - 1
function initCanvas(canvasEl)
{
let ret = canvasEl && (new fabric.Canvas(canvasEl));
if (ret)
{
ret.line = (x1,y1,x2,y2,c) => drawLineOn(ret,x1,y1,x2,y2,c);
}
return ret;
}
function drawBoard(cnvs)
{
let CELL_SIZE = 50;
let CELL_COUNT = 14;
let TOP_LEFT = { x : 50, y : 50};
let playerCellCount = CELL_COUNT/2-1;
let boardWidthInCells = playerCellCount+2 //for the side cells (player homes)
let boardHeightInCells = 3;
//frame
horizLine(TOP_LEFT.x,TOP_LEFT.y,boardWidthInCells * CELL_SIZE); //top left to top right
verticalLine(TOP_LEFT.x,TOP_LEFT.y,CELL_SIZE*boardHeightInCells); //top left to bottom left
horizLine(TOP_LEFT.x,TOP_LEFT.y + CELL_SIZE*boardHeightInCells,boardWidthInCells * CELL_SIZE); // bottom left to bottom right
verticalLine(TOP_LEFT.x + boardWidthInCells * CELL_SIZE,TOP_LEFT.y,CELL_SIZE * boardHeightInCells); //top right to bottom right
//home cells
verticalLine(TOP_LEFT.x + CELL_SIZE,TOP_LEFT.y,CELL_SIZE*boardHeightInCells)
verticalLine(TOP_LEFT.x + CELL_SIZE*(boardWidthInCells-1),TOP_LEFT.y,CELL_SIZE*boardHeightInCells)
//cell horizontal lines
let upperCellY = TOP_LEFT.y + CELL_SIZE;
let lowerCellY = TOP_LEFT.y + CELL_SIZE*2;
let lineLen = (boardWidthInCells-2)*CELL_SIZE;
horizLine(TOP_LEFT.x + CELL_SIZE,upperCellY,lineLen)
horizLine(TOP_LEFT.x + CELL_SIZE,lowerCellY,lineLen)
//cell borders
range(2,CELL_COUNT/2).map(cellNum => {
verticalLine(TOP_LEFT.x + cellNum*CELL_SIZE,TOP_LEFT.y,CELL_SIZE)
verticalLine(TOP_LEFT.x + cellNum*CELL_SIZE,TOP_LEFT.y+CELL_SIZE*(boardHeightInCells-1),CELL_SIZE)
} )
function verticalLine(x,y,len) { cnvs.line(x,y,x,y+len); }
function horizLine(x,y,len) { cnvs.line(x,y,x+len,y); }
function initGame(cnvsELID)
{
drawBoard(initCanvas(cnvsELID));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment