chessground12
Version:
Extended lishuuro.org Chess UI
118 lines • 3.89 kB
JavaScript
import { ranks as ranks2, files as files2, dimensions, } from './types';
export const invRanks = [...ranks2].reverse();
export const NRanks = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
export const invNRanks = [...NRanks].reverse();
function files(n) {
return files2.slice(0, n);
}
function ranks(n) {
return ranks2.slice(0, n);
}
export function allKeys(geom) {
const bd = dimensions[geom];
return Array.prototype.concat(...files(bd.width).map(c => ranks(bd.height).map(r => c + r)));
}
export function allPos(geom) {
return allKeys(geom).map(key2pos);
}
export const pos2key = (pos) => (files2[pos[0]] + ranks2[pos[1]]);
export const key2pos = (k) => {
if (k.length == 2) {
return [k.charCodeAt(0) - 97, k.charCodeAt(1) - 49];
}
else {
const rank = parseInt(k.slice(1));
return [k.charCodeAt(0) - 97, rank - 1];
}
};
export function roleOf(letter) {
return (letter.replace('+', 'p').toLowerCase() + '-piece');
}
export function letterOf(role, uppercase = false) {
const letterPart = role.slice(0, role.indexOf('-'));
const letter = letterPart.length > 1 ? letterPart.replace('p', '+') : letterPart;
return (uppercase ? letter.toUpperCase() : letter);
}
export function dropOrigOf(role) {
return (letterOf(role, true) + '@');
}
export function kingRoles(_variant) {
return ['k-piece'];
}
export function memo(f) {
let v;
const ret = () => {
if (v === undefined)
v = f();
return v;
};
ret.clear = () => {
v = undefined;
};
return ret;
}
export const timer = () => {
let startAt;
return {
start() {
startAt = performance.now();
},
cancel() {
startAt = undefined;
},
stop() {
if (!startAt)
return 0;
const time = performance.now() - startAt;
startAt = undefined;
return time;
},
};
};
export const opposite = (c) => (c === 'white' ? 'black' : 'white');
export const samePiece = (p1, p2) => p1.role === p2.role && p1.color === p2.color && p1.promoted === p2.promoted;
export const pieceSide = (p, o) => (p.color === o ? 'ally' : 'enemy');
export const pieceClasses = (p, o) => `${p.color} ${pieceSide(p, o)} ${p.promoted ? 'promoted ' : ''}${p.role}`;
export const distanceSq = (pos1, pos2) => {
const dx = pos1[0] - pos2[0], dy = pos1[1] - pos2[1];
return dx * dx + dy * dy;
};
export const posToTranslate = (bounds, bd) => (pos, asWhite) => [
((asWhite ? pos[0] : bd.width - 1 - pos[0]) * bounds.width) / bd.width,
((asWhite ? bd.height - 1 - pos[1] : pos[1]) * bounds.height) / bd.height,
];
export const translate = (el, pos) => {
el.style.transform = `translate(${pos[0]}px,${pos[1]}px)`;
};
export const setVisible = (el, v) => {
el.style.visibility = v ? 'visible' : 'hidden';
};
export const eventPosition = (e) => {
if (e.clientX || e.clientX === 0)
return [e.clientX, e.clientY];
if (e.targetTouches?.[0])
return [e.targetTouches[0].clientX, e.targetTouches[0].clientY];
return; // touchend has no position!
};
export const isRightButton = (e) => e.buttons === 2 || e.button === 2;
export const createEl = (tagName, className) => {
const el = document.createElement(tagName);
if (className)
el.className = className;
return el;
};
export const isMiniBoard = (el) => {
return Array.from(el.classList).includes('mini');
};
export function computeSquareCenter(key, asWhite, bounds, bd) {
const pos = key2pos(key);
if (!asWhite) {
pos[0] = bd.width - 1 - pos[0];
pos[1] = bd.height - 1 - pos[1];
}
return [
bounds.left + (bounds.width * (pos[0] + 0.5)) / bd.width,
bounds.top + (bounds.height * (bd.height - pos[1] - 0.5)) / bd.height,
];
}
//# sourceMappingURL=util.js.map