this.g_pieces[i][j].classList.add(C.GetColorClass(color));
this.g_pieces[i][j].style.width = pieceWidth + "px";
this.g_pieces[i][j].style.height = pieceWidth + "px";
this.g_pieces[i][j].classList.add(C.GetColorClass(color));
this.g_pieces[i][j].style.width = pieceWidth + "px";
this.g_pieces[i][j].style.height = pieceWidth + "px";
- const [ip, jp] = this.getPixelPosition(i, j, r);
- this.g_pieces[i][j].style.transform = `translate(${ip}px,${jp}px)`;
+ let [ip, jp] = this.getPixelPosition(i, j, r);
+ // Translate coordinates to use chessboard as reference:
+ this.g_pieces[i][j].style.transform =
+ `translate(${ip - r.x}px,${jp - r.y}px)`;
if (this.enlightened && !this.enlightened[i][j])
this.g_pieces[i][j].classList.add("hidden");
chessboard.appendChild(this.g_pieces[i][j]);
if (this.enlightened && !this.enlightened[i][j])
this.g_pieces[i][j].classList.add("hidden");
chessboard.appendChild(this.g_pieces[i][j]);
- this.r_pieces = { 'w': {}, 'b': {} };
- let chessboard =
- document.getElementById(this.containerId).querySelector(".chessboard");
+ this.r_pieces = { w: {}, b: {} };
+ let container = document.getElementById(this.containerId);
// NOTE: +1 fix display bug on Firefox at least
rcontainer.style.width = (nbR * sqResSize + 1) + "px";
rcontainer.style.height = sqResSize + "px";
// NOTE: +1 fix display bug on Firefox at least
rcontainer.style.width = (nbR * sqResSize + 1) + "px";
rcontainer.style.height = sqResSize + "px";
chessboard.style.left = newX + "px";
const newY = (window.innerHeight - newHeight) / 2;
chessboard.style.top = newY + "px";
chessboard.style.left = newX + "px";
const newY = (window.innerHeight - newHeight) / 2;
chessboard.style.top = newY + "px";
- const newR = { x: newX, y: newY, width: newWidth, height: newHeight };
+ const newR = {x: newX, y: newY, width: newWidth, height: newHeight};
const pieceWidth = this.getPieceWidth(newWidth);
for (let i=0; i < this.size.x; i++) {
for (let j=0; j < this.size.y; j++) {
const pieceWidth = this.getPieceWidth(newWidth);
for (let i=0; i < this.size.x; i++) {
for (let j=0; j < this.size.y; j++) {
this.g_pieces[i][j].style.width = pieceWidth + "px";
this.g_pieces[i][j].style.height = pieceWidth + "px";
const [ip, jp] = this.getPixelPosition(i, j, newR);
this.g_pieces[i][j].style.width = pieceWidth + "px";
this.g_pieces[i][j].style.height = pieceWidth + "px";
const [ip, jp] = this.getPixelPosition(i, j, newR);
- this.g_pieces[i][j].style.transform = `translate(${ip}px,${jp}px)`;
+ // Translate coordinates to use chessboard as reference:
+ this.g_pieces[i][j].style.transform =
+ `translate(${ip - newX}px,${jp - newY}px)`;
// Our coordinate system differs from CSS one (x <--> y).
// We return here the CSS coordinates (more useful).
getPixelPosition(i, j, r) {
// Our coordinate system differs from CSS one (x <--> y).
// We return here the CSS coordinates (more useful).
getPixelPosition(i, j, r) {
const centerOnCursor = (piece, e) => {
const centerShift = this.getPieceWidth(r.width) / 2;
const offset = getOffset(e);
const centerOnCursor = (piece, e) => {
const centerShift = this.getPieceWidth(r.width) / 2;
const offset = getOffset(e);
- piece.style.left = (offset.x - r.x - centerShift) + "px";
- piece.style.top = (offset.y - r.y - centerShift) + "px";
+ piece.style.left = (offset.x - centerShift) + "px";
+ piece.style.top = (offset.y - centerShift) + "px";
curPiece.style.width = pieceWidth + "px";
curPiece.style.height = pieceWidth + "px";
centerOnCursor(curPiece, e);
curPiece.style.width = pieceWidth + "px";
curPiece.style.height = pieceWidth + "px";
centerOnCursor(curPiece, e);
this.g_pieces[a.x][a.y].style.width = pieceWidth + "px";
this.g_pieces[a.x][a.y].style.height = pieceWidth + "px";
const [ip, jp] = this.getPixelPosition(a.x, a.y, r);
this.g_pieces[a.x][a.y].style.width = pieceWidth + "px";
this.g_pieces[a.x][a.y].style.height = pieceWidth + "px";
const [ip, jp] = this.getPixelPosition(a.x, a.y, r);
- this.g_pieces[a.x][a.y].style.transform = `translate(${ip}px,${jp}px)`;
+ // Translate coordinates to use chessboard as reference:
+ this.g_pieces[a.x][a.y].style.transform =
+ `translate(${ip - r.x}px,${jp - r.y}px)`;
if (this.enlightened && !this.enlightened[a.x][a.y])
this.g_pieces[a.x][a.y].classList.add("hidden");
chessboard.appendChild(this.g_pieces[a.x][a.y]);
if (this.enlightened && !this.enlightened[a.x][a.y])
this.g_pieces[a.x][a.y].classList.add("hidden");
chessboard.appendChild(this.g_pieces[a.x][a.y]);
- let movingPiece = this.getDomPiece(move.start.x, move.start.y);
- if (!movingPiece) { //TODO this shouldn't be required
+ let initPiece = this.getDomPiece(move.start.x, move.start.y);
+ if (!initPiece) { //TODO this shouldn't be required
- const initTransform = movingPiece.style.transform;
- let chessboard =
- document.getElementById(this.containerId).querySelector(".chessboard");
- const r = chessboard.getBoundingClientRect();
- const [ix, iy] = this.getPixelPosition(move.start.x, move.start.y, r);
+ // NOTE: cloning generally not required, but light enough, and simpler
+ let movingPiece = initPiece.cloneNode();
+ initPiece.style.opacity = "0";
+ let container =
+ document.getElementById(this.containerId)
+ const r = container.querySelector(".chessboard").getBoundingClientRect();
const startCode = this.getPiece(move.start.x, move.start.y);
movingPiece.classList.remove(pieces[startCode]["class"]);
movingPiece.classList.add(pieces[move.drag.p]["class"]);
const startCode = this.getPiece(move.start.x, move.start.y);
movingPiece.classList.remove(pieces[startCode]["class"]);
movingPiece.classList.add(pieces[move.drag.p]["class"]);
movingPiece.classList.remove(C.GetColorClass(apparentColor));
movingPiece.classList.add(C.GetColorClass(move.drag.c));
}
movingPiece.classList.remove(C.GetColorClass(apparentColor));
movingPiece.classList.add(C.GetColorClass(move.drag.c));
}
const [i1, j1] = move.segments[index][0];
const [i2, j2] = move.segments[index][1];
const dep = this.getPixelPosition(i1, j1, r);
const arr = this.getPixelPosition(i2, j2, r);
const [i1, j1] = move.segments[index][0];
const [i2, j2] = move.segments[index][1];
const dep = this.getPixelPosition(i1, j1, r);
const arr = this.getPixelPosition(i2, j2, r);
const distance =
Math.sqrt((arr[0] - dep[0]) ** 2 + (arr[1] - dep[1]) ** 2);
const duration = 0.2 + (distance / maxDist) * 0.3;
const distance =
Math.sqrt((arr[0] - dep[0]) ** 2 + (arr[1] - dep[1]) ** 2);
const duration = 0.2 + (distance / maxDist) * 0.3;
- movingPiece.style.transform = `translate(${arr[0]}px, ${arr[1]}px)`;
- movingPiece.style.transitionDuration = duration + "s";
- setTimeout(cb, duration * 1000);
+ // TODO: unclear why we need this new delay below:
+ setTimeout(() => {
+ movingPiece.style.transitionDuration = duration + "s";
+ // movingPiece is child of container: no need to adjust cordinates
+ movingPiece.style.transform = `translate(${arr[0]}px, ${arr[1]}px)`;
+ setTimeout(cb, duration * 1000);
+ }, 50);
if (index < move.segments.length)
animateSegment(index++, animateSegmentCallback);
else {
if (index < move.segments.length)
animateSegment(index++, animateSegmentCallback);
else {