chessgroundxx
Version:
lichess.org chess ui
116 lines • 3.85 kB
JavaScript
import * as cg from "./types.js";
export const invRanks = [...cg.ranks].reverse();
export const allKeys = Array.prototype.concat(...cg.files.map((c) => cg.ranks.map((r) => c + r)));
export function pos2key(pos) {
const bd = { width: 9, height: 10 };
return allKeys[bd.height * pos[0] + pos[1] - bd.height - 1];
}
export function key2pos(k) {
const shift = 1; //first rank is 0
return [k.charCodeAt(0) - 96, k.charCodeAt(1) - 48 + shift];
}
export const uciToMove = (uci) => {
if (!uci)
return undefined;
return [uci.slice(0, 2), uci.slice(2, 4)];
};
export const allPos = allKeys.map(key2pos);
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 function containsX(xs, x) {
return xs !== undefined && xs.indexOf(x) !== -1;
}
export const opposite = (c) => c === "white" ? "black" : "white";
export const distanceSq = (pos1, pos2) => {
const dx = pos1[0] - pos2[0], dy = pos1[1] - pos2[1];
return dx * dx + dy * dy;
};
export const samePiece = (p1, p2) => p1.role === p2.role && p1.color === p2.color;
export const posToTranslate = (bounds) => (pos, asWhite) => [
((asWhite ? pos[0] - 1 : 9 - pos[0]) * bounds.width) / 9,
((asWhite ? 10 - pos[1] : pos[1] - 1) * bounds.height) / 10,
];
export const translate = (el, pos) => {
el.style.transform = `translate(${pos[0]}px,${pos[1]}px)`;
};
export const posToTranslateBase = (pos, asWhite, xFactor, yFactor) => {
// ])
return [
(asWhite ? pos[0] - 1 : 9 - pos[0]) * xFactor,
(asWhite ? 10 - pos[1] : pos[1] - 1) * yFactor,
];
};
export const posToTranslateAbs = (bounds) => {
const xFactor = bounds.width / 9, yFactor = bounds.height / 10;
return (pos, asWhite) => posToTranslateBase(pos, asWhite, xFactor, yFactor);
};
export const translateAndScale = (el, pos, scale = 1) => {
el.style.transform = `translate(${pos[0]}px,${pos[1]}px) scale(${scale})`;
};
export const posToTranslateRel = (pos, asWhite) => {
return posToTranslateBase(pos, asWhite, 100, 100);
};
export const translateAbs = (el, pos) => {
el.style.transform = `translate(${pos[0]}px,${pos[1]}px)`;
};
export const translateRel = (el, percents) => {
el.style.transform = `translate(${percents[0]}%,${percents[1]}%)`;
};
export const setVisible = (el, v) => {
el.style.visibility = v ? "visible" : "hidden";
};
export const eventPosition = (e) => {
var _a;
if (e.clientX || e.clientX === 0)
return [e.clientX, e.clientY];
if ((_a = e.targetTouches) === null || _a === void 0 ? void 0 : _a[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 function computeSquareCenter(key, asWhite, bounds) {
const pos = key2pos(key);
if (!asWhite) {
pos[0] = 9 - pos[0] + 1;
pos[1] = 10 - pos[1] + 1;
}
return [
bounds.left + (bounds.width / 9) * (pos[0] - 1) + bounds.width / 18,
bounds.top + (bounds.height / 10) * (9 - (pos[1] - 1)) + bounds.height / 20,
];
}
//# sourceMappingURL=util.js.map