Skip to content

Commit 6b2144a

Browse files
committed
Refactor chess piece styling and rendering logic
- Simplified CSS by removing SVG background rules; core styles now handled in board.js. - Updated board.js to dynamically set piece backgrounds with cache-busting for improved loading. - Added console logs for better debugging of piece rendering and loading status.
1 parent 0e4c80c commit 6b2144a

2 files changed

Lines changed: 28 additions & 24 deletions

File tree

css/pieces.css

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
/**
2-
* Chess piece styling using SVG files for reliable cross-browser rendering
2+
* Chess piece styling - core styles only
3+
* All piece-specific styling handled by inline styles in board.js
34
*/
45

5-
/* Base piece styling */
66
.piece {
77
width: 80%;
88
height: 80%;
99
cursor: grab;
1010
user-select: none;
1111
position: relative;
12-
background-size: contain;
13-
background-repeat: no-repeat;
14-
background-position: center;
1512
transition: transform 0.15s ease;
1613
will-change: transform;
17-
18-
/* Disable any content-based display */
1914
color: transparent !important;
2015
}
2116

@@ -42,22 +37,6 @@
4237
z-index: 100;
4338
}
4439

45-
/* WHITE PIECES */
46-
.piece.w.p { background-image: url("../assets/wp.svg") !important; }
47-
.piece.w.r { background-image: url("../assets/wr.svg") !important; }
48-
.piece.w.n { background-image: url("../assets/wn.svg") !important; }
49-
.piece.w.b { background-image: url("../assets/wb.svg") !important; }
50-
.piece.w.q { background-image: url("../assets/wq.svg") !important; }
51-
.piece.w.k { background-image: url("../assets/wk.svg") !important; }
52-
53-
/* BLACK PIECES */
54-
.piece.b.p { background-image: url("../assets/bp.svg") !important; }
55-
.piece.b.r { background-image: url("../assets/br.svg") !important; }
56-
.piece.b.n { background-image: url("../assets/bn.svg") !important; }
57-
.piece.b.b { background-image: url("../assets/bb.svg") !important; }
58-
.piece.b.q { background-image: url("../assets/bq.svg") !important; }
59-
.piece.b.k { background-image: url("../assets/bk.svg") !important; }
60-
6140
/* Move animations */
6241
@keyframes move-piece {
6342
0% { transform: scale(1); }

js/board.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,36 @@ function renderPieces(boardState, movedPiece = null) {
3232
// Clear existing pieces first
3333
document.querySelectorAll('.piece').forEach(p => p.remove());
3434

35+
// Generate a cache-busting token
36+
const cacheBuster = Date.now();
37+
console.log("Rendering pieces with cache buster:", cacheBuster);
38+
3539
for (let row = 0; row < 8; row++) {
3640
for (let col = 0; col < 8; col++) {
3741
const piece = boardState[row][col];
3842
if (piece) {
3943
const squareElement = getSquareElement(row, col);
4044
if (squareElement) {
4145
const pieceElement = document.createElement('div');
42-
pieceElement.classList.add('piece', piece.color, piece.type);
46+
pieceElement.classList.add('piece');
47+
48+
// Add piece color and type as classes for potential styling
49+
pieceElement.classList.add(piece.color, piece.type);
50+
51+
// Use absolute path with cache busting
52+
const svgPath = `assets/${piece.color}${piece.type}.svg?v=${cacheBuster}`;
53+
console.log(`Setting background for ${piece.color}${piece.type} at [${row},${col}] to: ${svgPath}`);
54+
55+
pieceElement.style.backgroundImage = `url('${svgPath}')`;
56+
pieceElement.style.backgroundSize = 'contain';
57+
pieceElement.style.backgroundRepeat = 'no-repeat';
58+
pieceElement.style.backgroundPosition = 'center';
59+
60+
// Force image preload to debug loading issues
61+
const img = new Image();
62+
img.onload = () => console.log(`✓ Successfully loaded: ${svgPath}`);
63+
img.onerror = () => console.error(`✗ Failed to load: ${svgPath}`);
64+
img.src = svgPath;
4365

4466
// Add animation class if this is the piece that just moved
4567
if (movedPiece && movedPiece.row === row && movedPiece.col === col) {
@@ -53,6 +75,9 @@ function renderPieces(boardState, movedPiece = null) {
5375

5476
// Add data attributes for easier identification if needed
5577
pieceElement.dataset.piece = `${piece.color}${piece.type}`;
78+
pieceElement.dataset.color = piece.color;
79+
pieceElement.dataset.type = piece.type;
80+
5681
squareElement.appendChild(pieceElement);
5782
}
5883
}

0 commit comments

Comments
 (0)