-function animateSquareToSquare(src, dest, piece, completeFn) {
- // get information about the source and destination squares
- var srcSquareEl = document.getElementById(SQUARE_ELS_IDS[src]);
- var srcSquarePosition = offset(srcSquareEl);
- var destSquareEl = document.getElementById(SQUARE_ELS_IDS[dest]);
- var destSquarePosition = offset(destSquareEl);
-
- // create the animated piece and absolutely position it
- // over the source square
- var animatedPieceId = createId();
- document.body.append(buildPiece(piece, true, animatedPieceId));
- var animatedPieceEl = document.getElementById(animatedPieceId);
- animatedPieceEl.style.display = null;
- animatedPieceEl.style.position = 'absolute';
- animatedPieceEl.style.top = srcSquarePosition.top + 'px';
- animatedPieceEl.style.left = srcSquarePosition.left + 'px';
-
- // remove original piece(s) from source square
- // TODO: multiple pieces should never really happen, but it will if we are moving
- // while another animation still isn't done
- srcSquareEl.querySelectorAll('.' + CSS.piece).forEach((piece) => piece.remove());
-
- // on complete
- var complete = function() {
- // add the "real" piece to the destination square
- destSquareEl.append(buildPiece(piece));
-
- // remove the animated piece
- animatedPieceEl.remove();
-
- // run complete function
- if (typeof completeFn === 'function') {
- completeFn();
- }
- };
-
- // animate the piece to the destination square
- animatedPieceEl.addEventListener('transitionend', complete, {once: true});
- requestAnimationFrame(() => {
- animatedPieceEl.style.transitionProperty = 'top, left';
- animatedPieceEl.style.transitionDuration = cfg.moveSpeed + 'ms';
- animatedPieceEl.style.top = destSquarePosition.top + 'px';
- animatedPieceEl.style.left = destSquarePosition.left + 'px';
- });
-}
-
-function animateSparePieceToSquare(piece, dest, completeFn) {
- var srcOffset = offset(document.getelementById(SPARE_PIECE_ELS_IDS[piece]));
- var destSquareEl = document.getElementById(SQUARE_ELS_IDS[dest]);
- var destOffset = offset(destSquareEl);
-
- // create the animate piece
- var pieceId = createId();
- document.body.append(buildPiece(piece, true, pieceId));
- var animatedPieceEl = document.getElementById(pieceId);
- animatedPieceEl.style.display = '';
- animatedPieceEl.style.position = 'absolute';
- animatedPieceEl.style.left = srcOffset.left;
- animatedPieceEl.style.top = srcOffset.top;
-
- // on complete
- var complete = function() {
- // add the "real" piece to the destination square
- destSquareEl.querySelector('.' + CSS.piece).remove();
- destSquareEl.append(buildPiece(piece));
-
- // remove the animated piece
- animatedPieceEl.remove();
-
- // run complete function
- if (typeof completeFn === 'function') {
- completeFn();
- }
- };
-
- // animate the piece to the destination square
- // FIXME: support this for non-jquery
- var opts = {
- duration: cfg.moveSpeed,
- complete: complete