Created
February 28, 2023 22:17
-
-
Save thinktt/5b1d5933755c45dce152178f73a94696 to your computer and use it in GitHub Desktop.
Chess Grounds Animation Manipulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cg = Chessground(document.getElementById(this.id), { | |
coordinates: false, | |
movable: { | |
free: false, | |
color: this.colorSide, | |
dests: getLegalMoves(this.game), | |
showDests: false, | |
events: { | |
after: onMove | |
} | |
}, | |
premovable: { | |
enabled: false, | |
showDests: true, | |
}, | |
draggable: { | |
enabled: true, | |
}, | |
selectable: { | |
enabled: true, | |
}, | |
drawable: { | |
enabled: false, | |
}, | |
animation: { | |
enabled: true | |
}, | |
}) | |
const gameState = Chess(yourFen) | |
function onMove(from, to) { | |
if (isPromotion(from, to, gameState)) { | |
cg.set({ animation: { enabled: false } }) | |
doPromoteRequest(from, to) | |
return | |
} | |
this.$emit('move', from + to) | |
} | |
function isPromotion(fromSquare, toSquare, gameState) { | |
const squareState = gameState.get(fromSquare) | |
if (squareState.type !== 'p') return false | |
if (toSquare.includes('8') || toSquare.includes('1')) return true | |
return false | |
} | |
function doPromoteRequest(from, to) { | |
// here you need to render however you're having your user select their piece | |
} | |
function doPromotion() { | |
updateBoard(from, to, piece) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment