Improve diagraming abilities, fix Chakart full rules
[xogo.git] / utils / drawDiagrams.js
CommitLineData
5abaabb3
BA
1function fenToDiag(vname) {
2 import(`/variants/${vname}/class.js`).then(module => {
3 window.V = module.default;
4 for (const [k, v] of Object.entries(V.Aliases))
5 window[k] = v;
549ca151 6 re_drawDiagrams();
5abaabb3
BA
7 });
8}
9
10// TODO: heuristic to improve for ratio != 1 (how?)
549ca151
BA
11function getDiagSize(elt) {
12 const baseWidth = Math.min(window.innerWidth, 800);
13 let multFact = 1;
14 if (elt.classList.contains("left") || elt.classList.contains("right"))
15 multFact = 0.45;
16 else if (baseWidth > 630)
17 multFact = 0.5;
18 else
19 multFact = 0.7;
20 return multFact * baseWidth;
5abaabb3
BA
21}
22
549ca151
BA
23let vr = null;
24function re_drawDiagrams() {
5abaabb3 25 const diagrams = document.getElementsByClassName("diag");
549ca151
BA
26 if (diagrams.length == 0)
27 return;
28 const redrawing = !!vr;
29 if (!redrawing)
30 vr = new Array(diagrams.length);
5abaabb3 31 for (let i=0; i<diagrams.length; i++) {
549ca151
BA
32 if (!redrawing) {
33 let chessboard = document.createElement("div");
34 chessboard.classList.add("chessboard");
35 diagrams[i].appendChild(chessboard);
36 diagrams[i].id = "diag_" + i;
37 }
38 const diagSize = getDiagSize(diagrams[i]);
5abaabb3
BA
39 diagrams[i].style.width = diagSize + "px";
40 diagrams[i].style.height = diagSize + "px";
549ca151
BA
41 if (!redrawing) {
42 vr[i] = new V({
43 element: "diag_" + i,
44 fen: diagrams[i].dataset.fen,
45 color: diagrams[i].dataset.col || 'w',
46 options: {},
47 diagram: true
48 });
49 }
5abaabb3
BA
50 }
51}
549ca151
BA
52
53window.onresize = re_drawDiagrams;