lichess-org / chessground

Mobile/Web chess UI for lichess.org
https://lichess.org
GNU General Public License v3.0
1.01k stars 260 forks source link

Possible to drag and drop pieces off the board? #291

Closed AhsanSarwar45 closed 9 months ago

AhsanSarwar45 commented 9 months ago

Hi, I was wondering if its possible to drag and drop pieces pieces off the board like in this chessboard.js example?

AhsanSarwar45 commented 9 months ago

An even better example would be the crazyhouse variant in lichess. Is that done using in built features, or custom-made?

niklasf commented 9 months ago

Dropping off the board is done via: https://github.com/lichess-org/chessground/blob/f90358e77403673027ef3215cfe6266dc98bbc44/src/config.ts#L66

To drag a new piece onto the board, a little bit of glue is needed, because the pocket is not part of chessground. Call https://github.com/lichess-org/chessground/blob/f90358e77403673027ef3215cfe6266dc98bbc44/src/api.ts#L72 from the mousedown event handler of the pocket.

AhsanSarwar45 commented 9 months ago

@niklasf Thank you, that worked. By any chance, is there a way to specify legal squares that the pieces can be dragged on to?

niklasf commented 9 months ago

For a new piece that's dragged in? Not directly, but you provide a callback https://github.com/lichess-org/chessground/blob/f90358e77403673027ef3215cfe6266dc98bbc44/src/config.ts#L77 and reset the board state if needed. That's how lichess.org implements Crazyhouse drops.

For pieces that are already on the board, yes:

https://github.com/lichess-org/chessground/blob/f90358e77403673027ef3215cfe6266dc98bbc44/src/config.ts#L35