chessboardjs
chessboardjs copied to clipboard
Figure doubles on change turn to you
There is a bug with snapping figure:
When you get a piece and it is "flying" near the pointer in time of your opponent turn, a new piece will born on place where you got your piece when opponent made the move.
Example: http://chessboardjs.com/examples#5001
Reproduction:
- Opponent turn(you can add pause for random opponent turn)
- Pick up piece(but do not drop it)
- Opponent made the move
- You see second piece.

I believe this happens because drawPositionInstant() is called when the move animation finishes. This causes the dragged piece to be readded to the board. Adding the following to drawPositionInstant() after the loop fixes a similar problem I had, though I haven't tested with example 5001.
if (DRAGGING_A_PIECE) {
// make sure dragged pieces don't suddenly reappear
$('#' + SQUARE_ELS_IDS[DRAGGED_PIECE_SOURCE]).addClass(CSS.highlight1)
.find('.' + CSS.piece).css('display', 'none');
}
(There is a similar problem if you drop a piece while an animation is running. The call to drawPositionInstant() will cause a duplicate of the animating piece to appear on the target square. I don't know how to fix this; it might involve keeping track of all destination squares for animations and then not updating those squares in the drawPositionInstantFunction())
It's been a while since I looked at this code, but I believe @sorensp is correct here.
I think I used drawPositionInstant() to just "make sure" that the board was in a valid state after animations finished, but tracking the state of the animations is probably what needs to happen instead.
Thanks but this solution runs into all sorts of disappearing piece problems in cases where the attempted move is illegal. Take a look at this solution: https://github.com/oakmac/chessboardjs/pull/86
It still has problems, but gets a bit closer.
What's the status on this? Maybe another solution is to allow the user to supply a callback to be invoked when the animation finishes. This way, you can restrict picking up a piece (drag start) until the "opponent's" turn finishes.