Fix Dice display
[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;
2c8375bb
BA
14 if (elt.classList.contains("left") || elt.classList.contains("right")) {
15 if (baseWidth >= 551)
16 multFact = 0.45;
17 else
18 multFact = 0.7;
19 }
549ca151
BA
20 else if (baseWidth > 630)
21 multFact = 0.5;
22 else
23 multFact = 0.7;
24 return multFact * baseWidth;
5abaabb3
BA
25}
26
549ca151
BA
27let vr = null;
28function re_drawDiagrams() {
5abaabb3 29 const diagrams = document.getElementsByClassName("diag");
549ca151
BA
30 if (diagrams.length == 0)
31 return;
32 const redrawing = !!vr;
33 if (!redrawing)
34 vr = new Array(diagrams.length);
5abaabb3 35 for (let i=0; i<diagrams.length; i++) {
549ca151
BA
36 if (!redrawing) {
37 let chessboard = document.createElement("div");
38 chessboard.classList.add("chessboard");
39 diagrams[i].appendChild(chessboard);
40 diagrams[i].id = "diag_" + i;
41 }
42 const diagSize = getDiagSize(diagrams[i]);
5abaabb3
BA
43 diagrams[i].style.width = diagSize + "px";
44 diagrams[i].style.height = diagSize + "px";
549ca151
BA
45 if (!redrawing) {
46 vr[i] = new V({
47 element: "diag_" + i,
48 fen: diagrams[i].dataset.fen,
b2fc1259 49 marks: diagrams[i].dataset.mks
2c8375bb 50 ? diagrams[i].dataset.mks.split(',')
b2fc1259 51 : undefined,
549ca151
BA
52 color: diagrams[i].dataset.col || 'w',
53 options: {},
54 diagram: true
55 });
56 }
5abaabb3
BA
57 }
58}
549ca151
BA
59
60window.onresize = re_drawDiagrams;