-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (49 loc) Β· 2.02 KB
/
Copy pathindex.js
File metadata and controls
64 lines (49 loc) Β· 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"use strict";
import { findOptimalPlacement } from "./findOptimalPlacement.js";
import { getRandomColor } from "./getRandomColor.js";
const CONTAINER = { width: "100vw", height: "100vh" };
const response = await fetch("./rectangles.json");
const blocks = await response.json();
const bodyElement = document.body;
const containerElement = document.createElement("div");
containerElement.classList.add("container");
containerElement.style.width = CONTAINER.width;
containerElement.style.height = CONTAINER.height;
bodyElement.appendChild(containerElement);
const updateContainerSize = () => {
const { clientWidth, clientHeight } = containerElement;
const container = { width: clientWidth, height: clientHeight };
const { fullness, blockCoordinates } = findOptimalPlacement(
container,
blocks
);
containerElement.innerHTML = "";
blockCoordinates.forEach((block) => {
const blockElement = document.createElement("div");
blockElement.classList.add("block");
blockElement.style.top = `${block.top}px`;
blockElement.style.left = `${block.left}px`;
blockElement.style.width = `${block.right - block.left}px`;
blockElement.style.height = `${block.bottom - block.top}px`;
blockElement.style.backgroundColor = getRandomColor(
blockElement.style.width,
blockElement.style.height
);
const textElement = document.createElement("p");
textElement.classList.add("text");
const smallerDimension = Math.min(
block.right - block.left,
block.bottom - block.top
);
textElement.style.fontSize = `${smallerDimension * 0.5}px`;
textElement.textContent = block.initialOrder;
blockElement.appendChild(textElement);
containerElement.appendChild(blockElement);
});
const fullnessElement = document.createElement("p");
fullnessElement.classList.add("fullness");
fullnessElement.textContent = `Useful space coefficient: ${fullness}`;
containerElement.appendChild(fullnessElement);
};
updateContainerSize();
window.addEventListener("resize", updateContainerSize);