From 4615f6b5ba23f497b8f43b9a38a410925d35ff91 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Fri, 23 Dec 2022 23:08:17 +0100 Subject: [PATCH] Add a Firefox workaround for animations. --- www/js/chessboard-0.3.0.js | 48 ++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/www/js/chessboard-0.3.0.js b/www/js/chessboard-0.3.0.js index d1d53f1..451f1a9 100644 --- a/www/js/chessboard-0.3.0.js +++ b/www/js/chessboard-0.3.0.js @@ -818,33 +818,35 @@ function doAnimations(a, oldPos, newPos) { } } - for (var i = 0; i < a.length; i++) { - // clear a piece - if (a[i].type === 'clear') { - document.getElementById(SQUARE_ELS_IDS[a[i].square]).querySelectorAll('.' + CSS.piece).forEach( - (piece) => fadeOut(piece, onFinish) - ); - } + requestAnimationFrame(() => { // Firefox workaround. + for (var i = 0; i < a.length; i++) { + // clear a piece + if (a[i].type === 'clear') { + document.getElementById(SQUARE_ELS_IDS[a[i].square]).querySelectorAll('.' + CSS.piece).forEach( + (piece) => fadeOut(piece, onFinish) + ); + } - // add a piece (no spare pieces) - if (a[i].type === 'add' && cfg.sparePieces !== true) { - let square = document.getElementById(SQUARE_ELS_IDS[a[i].square]); - square.append(buildPiece(a[i].piece, true)); - let piece = square.querySelector('.' + CSS.piece); - fadeIn(piece, onFinish); - } + // add a piece (no spare pieces) + if (a[i].type === 'add' && cfg.sparePieces !== true) { + let square = document.getElementById(SQUARE_ELS_IDS[a[i].square]); + square.append(buildPiece(a[i].piece, true)); + let piece = square.querySelector('.' + CSS.piece); + fadeIn(piece, onFinish); + } - // add a piece from a spare piece - if (a[i].type === 'add' && cfg.sparePieces === true) { - animateSparePieceToSquare(a[i].piece, a[i].square, onFinish); - } + // add a piece from a spare piece + if (a[i].type === 'add' && cfg.sparePieces === true) { + animateSparePieceToSquare(a[i].piece, a[i].square, onFinish); + } - // move a piece - if (a[i].type === 'move') { - animateSquareToSquare(a[i].source, a[i].destination, a[i].piece, - onFinish); + // move a piece + if (a[i].type === 'move') { + animateSquareToSquare(a[i].source, a[i].destination, a[i].piece, + onFinish); + } } - } + }); } // returns the distance between two squares -- 2.39.2