You can edit the below JavaScript code to customize the image tool.
Apply Changes
function processImage(originalImg, gridSize = 8, marginSize = 40, cropToSquare = "yes", lightSquareColor = "rgba(255, 255, 255, 0.2)", darkSquareColor = "rgba(0, 0, 0, 0.5)", marginColor = "#2b2b2b", textColor = "#ffffff") {
gridSize = parseInt(gridSize) > 0 ? parseInt(gridSize) : 8;
marginSize = parseInt(marginSize) >= 0 ? parseInt(marginSize) : 40;
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
let srcX = 0, srcY = 0, srcW = originalImg.width, srcH = originalImg.height;
// Crop image to a perfect square if requested
// This is ideal since regular chessboards are perfect squares
if (String(cropToSquare).toLowerCase() === "yes" || String(cropToSquare).toLowerCase() === "true") {
const minDim = Math.min(originalImg.width, originalImg.height);
srcX = (originalImg.width - minDim) / 2;
srcY = (originalImg.height - minDim) / 2;
srcW = minDim;
srcH = minDim;
}
const boardW = srcW;
const boardH = srcH;
canvas.width = boardW + marginSize * 2;
canvas.height = boardH + marginSize * 2;
// Fill the outer boundary (margin area)
ctx.fillStyle = marginColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw the original image onto the playable board area
ctx.drawImage(originalImg, srcX, srcY, srcW, srcH, marginSize, marginSize, boardW, boardH);
const cellW = boardW / gridSize;
const cellH = boardH / gridSize;
// Draw the chess grid overlay (semi-transparent checkerboard)
for (let r = 0; r < gridSize; r++) {
for (let c = 0; c < gridSize; c++) {
// Standard chess convention has the top-left square (A8) as a light square
if ((r + c) % 2 === 0) {
ctx.fillStyle = lightSquareColor;
} else {
ctx.fillStyle = darkSquareColor;
}
ctx.fillRect(marginSize + c * cellW, marginSize + r * cellH, cellW, cellH);
}
}
// Draw a border and rank/file coordinates if there is outer margin space
if (marginSize > 0) {
ctx.strokeStyle = textColor;
ctx.lineWidth = Math.max(1, marginSize * 0.05);
ctx.strokeRect(marginSize - ctx.lineWidth / 2, marginSize - ctx.lineWidth / 2, boardW + ctx.lineWidth, boardH + ctx.lineWidth);
}
if (marginSize >= 15) {
ctx.fillStyle = textColor;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
const fontSize = Math.max(10, Math.min(marginSize * 0.4, 24));
ctx.font = `bold ${fontSize}px sans-serif`;
// Render File Coordinates (Letters at top and bottom margins)
for (let c = 0; c < gridSize; c++) {
let index = c % 26;
let letter = String.fromCharCode(65 + index);
if (c >= 26) {
letter = String.fromCharCode(65 + Math.floor(c / 26) - 1) + letter;
}
ctx.fillText(letter, marginSize + c * cellW + cellW / 2, canvas.height - marginSize / 2);
ctx.fillText(letter, marginSize + c * cellW + cellW / 2, marginSize / 2);
}
// Render Rank Coordinates (Numbers at left and right margins)
for (let r = 0; r < gridSize; r++) {
let number = (gridSize - r).toString();
ctx.fillText(number, marginSize / 2, marginSize + r * cellH + cellH / 2);
ctx.fillText(number, canvas.width - marginSize / 2, marginSize + r * cellH + cellH / 2);
}
}
return canvas;
}
Apply Changes