I'm making a chess game with the HTML5 canvas and I'm having trouble getting the rank and file labels to display correctly. By labels I mean the 1,2,..,8 and A,B,...H text that is positioned around the outskirts of the board to uniquely identify board positions. Currently I'm using two divs outside of the "board" element to display the the labels. The way I currently do this isn't responsive and often doesn't display properly when shown on devices other than the one I designed it on. The text for the file (letters) and rank (numbers) might be different sizes, or the characters might not align with the center of each column or row.
This is what the board looks like when everything is working as intended
And here's the problem I run into on the phone. The letters don't line up with the tiles of the board, as well as the control widget moving outside the page:
chessboard = document.getElementById('chessboard');
ctxPiece = document.getElementById('chesspieceCanvas').getContext('2d');
ctxHighlight = document.getElementById('highlight').getContext('2d');
drawBoard(chessboard, chessboard.getContext('2d'));
function drawBoard(canvas, ctx) {
var rows = 8;
TILE_SIZE = canvas.height / rows;
var white = true;
var TILE_COLOR1 = "rgb(160,82,45)";
var TILE_COLOR2 = "rgb(245,222,179)";
for (var row = 0; row < rows; row++) {
for (var col = 0; col < rows; col++) {
if (!white) {
ctx.fillStyle = TILE_COLOR1;
ctx.fillRect(col * TILE_SIZE, row * TILE_SIZE, TILE_SIZE, TILE_SIZE);
white = true;
} else {
ctx.fillStyle = TILE_COLOR2;
ctx.fillRect(col * TILE_SIZE, row * TILE_SIZE, TILE_SIZE, TILE_SIZE);
white = false;
}
}
white = !white;
}
}
#gameArea {
width: 650px;
font-size: 16px;
}
#output {
text-align: center;
}
#outputMessage {
display: inline-block;
font-size: 2.5em;
font-weight: bold;
}
#boardAndInfo {
overflow: visible;
position: relative;
width: 1000px;
}
#board {
display: inline-block;
border: 0.12em solid black;
height: inherit;
position: relative;
width: 600px;
line-height: 0;
overflow: hidden;
}
#chessboard {
position: absolute;
z-index: 0;
}
#highlight {
position: absolute;
z-index: 1;
}
#gameInfo {
display: inline-block;
height: 585px;
width: 190px;
vertical-align: top;
position: relative;
}
#fileNotation {
color: gray;
font-size: 2.0em;
margin-left: 50px;
letter-spacing: 54px;
}
#chesspieceCanvas {
font-size: 70px;
font-family: 'Open Sans', sans-serif;
position: relative;
z-index: 2;
}
#rankNotation {
color: gray;
float: left;
font-size: 2.0em;
line-height: 235%;
position: relative;
width: 1.5ch;
word-wrap: break-word;
}
<body>
<div id='gameSynopsis'>
</div>
<div id="gameArea" lang='en'>
<div id="output">
<span id="outputMessage">Some text here</span>
<span id="aiThinkingIndicator" class="loading hide"></span>
</div>
<div id="fileNotation" class="boardLabel">
ABCDEFGH
</div>
<div id="rankNotation" class="boardLabel">
87654321
</div>
<div id="boardAndInfo">
<div id="board">
<canvas id="chessboard" width="600" height="600"></canvas>
<canvas id="highlight" width="600" height="600"></canvas>
<canvas id="chesspieceCanvas" width="600" height="600">
<p id='canvasSupportMsg'></p>
</canvas>
</div>
<fieldset id="gameInfo">
<legend id='gameInfoTitle'>Stuff</legend>
</fieldset>
</div>
</div>
</body>
The fiddle for my game can be found here. If I'm missing anything else please let me know.
I'm looking for a way to change the way this works in order to have it scale properly but I haven't been able to find a solution yet.