Skip to content

Commit

Permalink
Merge pull request #301 from unicolored/coordinatesOnSquares
Browse files Browse the repository at this point in the history
Add option to display coordinate on every square
  • Loading branch information
ornicar committed May 1, 2024
2 parents fad0543 + 7ad20b4 commit d115014
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 11 deletions.
50 changes: 50 additions & 0 deletions assets/chessground.base.css
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,53 @@ piece.fading {
.cg-wrap coords.ranks coord {
transform: translateY(39%);
}

.cg-wrap coords.squares {
bottom: 0;
left: 0;
text-transform: uppercase;
text-align: right;
flex-flow: column-reverse;
height: 100%;
width: 12.5%;
}

.cg-wrap coords.squares.black {
flex-flow: column;
}

.cg-wrap coords.squares.left {
text-align: left;
}

.cg-wrap coords.squares coord {
padding: 6% 4%;
}

.cg-wrap coords.squares.rank2 {
transform: translateX(100%);
}

.cg-wrap coords.squares.rank3 {
transform: translateX(200%);
}

.cg-wrap coords.squares.rank4 {
transform: translateX(300%);
}

.cg-wrap coords.squares.rank5 {
transform: translateX(400%);
}

.cg-wrap coords.squares.rank6 {
transform: translateX(500%);
}

.cg-wrap coords.squares.rank7 {
transform: translateX(600%);
}

.cg-wrap coords.squares.rank8 {
transform: translateX(700%);
}
18 changes: 9 additions & 9 deletions assets/chessground.brown.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ cg-board square.current-premove {
background-color: rgba(20, 30, 85, 0.5);
}

/** Alternating colors in rank/file labels */
.cg-wrap.orientation-white coords.ranks coord:nth-child(2n),
.cg-wrap.orientation-white coords.files coord:nth-child(2n),
.cg-wrap.orientation-black coords.ranks coord:nth-child(2n + 1),
.cg-wrap.orientation-black coords.files coord:nth-child(2n + 1) {
/** Alternating colors in rank/file/square labels */
.cg-wrap coords:nth-child(odd) coord:nth-child(even),
.cg-wrap coords.squares:nth-child(even) coord:nth-child(odd),
.cg-wrap.orientation-black coords.files:nth-child(even) coord:nth-child(odd),
.cg-wrap coords.files:nth-child(even) coord:nth-child(even) {
color: rgba(72, 72, 72, 0.8);
}

.cg-wrap.orientation-black coords.ranks coord:nth-child(2n),
.cg-wrap.orientation-black coords.files coord:nth-child(2n),
.cg-wrap.orientation-white coords.ranks coord:nth-child(2n + 1),
.cg-wrap.orientation-white coords.files coord:nth-child(2n + 1) {
.cg-wrap coords:nth-child(odd) coord:nth-child(odd),
.cg-wrap coords.squares:nth-child(even) coord:nth-child(even),
.cg-wrap.orientation-black coords.files:nth-child(even) coord:nth-child(even),
.cg-wrap coords.files:nth-child(even) coord:nth-child(odd) {
color: rgba(255, 255, 255, 0.8);
}
17 changes: 17 additions & 0 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@
],
},
});
Chessground(document.getElementById('board-4'), {
orientation: 'black',
coordinatesOnSquares: true,
ranksPosition: 'left',
});
Chessground(document.getElementById('board-5'), {
orientation: 'white',
coordinatesOnSquares: true,
});
</script>
</head>
<body>
Expand All @@ -98,5 +107,13 @@
board with fixed arrows + labels
<div class="chessground" id="board-3"></div>
</div>
<div>
board with coordinates on-square, ranks position left
<div class="chessground" id="board-4"></div>
</div>
<div>
board with coordinates on-square
<div class="chessground" id="board-5"></div>
</div>
</body>
</html>
2 changes: 2 additions & 0 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface HeadlessState {
lastMove?: cg.Key[]; // squares part of the last move ["c3"; "c4"]
selected?: cg.Key; // square currently selected "a1"
coordinates: boolean; // include coords attributes
coordinatesOnSquares: boolean; // include coords attributes on every square
ranksPosition: cg.RanksPosition; // position ranks on either side. left | right
autoCastle: boolean; // immediately complete the castle by moving the rook after king move
viewOnly: boolean; // don't bind events: the user will never be able to move pieces around
Expand Down Expand Up @@ -113,6 +114,7 @@ export function defaults(): HeadlessState {
orientation: 'white',
turnColor: 'white',
coordinates: true,
coordinatesOnSquares: false,
ranksPosition: 'right',
autoCastle: true,
viewOnly: false,
Expand Down
17 changes: 15 additions & 2 deletions src/wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,21 @@ export function renderWrap(element: HTMLElement, s: HeadlessState): Elements {
if (s.coordinates) {
const orientClass = s.orientation === 'black' ? ' black' : '';
const ranksPositionClass = s.ranksPosition === 'left' ? ' left' : '';
container.appendChild(renderCoords(ranks, 'ranks' + orientClass + ranksPositionClass));
container.appendChild(renderCoords(files, 'files' + orientClass));

if (s.coordinatesOnSquares) {
const rankN: (i: number) => number = s.orientation === 'white' ? i => i + 1 : i => 8 - i;
files.forEach((f, i) =>
container.appendChild(
renderCoords(
ranks.map(r => f + r),
'squares rank' + rankN(i) + orientClass + ranksPositionClass,
),
),
);
} else {
container.appendChild(renderCoords(ranks, 'ranks' + orientClass + ranksPositionClass));
container.appendChild(renderCoords(files, 'files' + orientClass));
}
}

let ghost: HTMLElement | undefined;
Expand Down

1 comment on commit d115014

@unicolored
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ornicar !
Next : I will try asap to add an "On Square" option on the display preferences of Lichess.
Capture d’écran 2024-05-01 à 16 15 48

Please sign in to comment.