|
15 | 15 | <span class="space-fill"></span>
|
16 | 16 | <span id="home-button" class="nav-item" onclick="changeView('home')" style="display: none;">Back to Overview</span>
|
17 | 17 | <a href="./README.md" target="viewer-frame" class="nav-item" onclick="changeView('README.md')">About this Project</a>
|
18 |
| - <span class="nav-item"><input type="checkbox" id="theme-switch"><label for="theme-switch" class="theme-switch-label"><i id="theme-switch-icon" class="fas fa-moon"></i></label></span> |
| 18 | + <span class="nav-item" id="theme-switch"><input type="checkbox" id="theme-switch-checkbox"><label for="theme-switch-checkbox" class="theme-switch-label"><i id="theme-switch-icon" class="fas fa-moon"></i></label></span> |
19 | 19 | <a href="https://github.com/MatteoVoges/extending-three.js" class="nav-item"><i class="fa-brands fa-github"></i></a>
|
20 | 20 | </div>
|
21 | 21 |
|
|
31 | 31 | </body>
|
32 | 32 |
|
33 | 33 | <script>
|
34 |
| - document.getElementById("viewer-frame").addEventListener("load", function() { |
| 34 | + const viewerFrame = document.getElementById("viewer-frame"); |
| 35 | + const homeButton = document.getElementById("home-button"); |
| 36 | + const examplesContainer = document.getElementById("examples"); |
| 37 | + const themeSwitch = document.getElementById("theme-switch"); |
| 38 | + const themeSwitchIcon = document.getElementById("theme-switch-icon"); |
| 39 | + const themeSwitchCheckbox = document.getElementById("theme-switch-checkbox"); |
| 40 | + |
| 41 | + function applyPrimaryColor() { |
35 | 42 | const primaryColor = "rgb(" + getComputedStyle(document.documentElement).getPropertyValue("--primary-color") + ")";
|
36 |
| - for (const e of document.getElementById("viewer-frame").contentDocument.documentElement.getElementsByClassName("lil-gui")) { |
| 43 | + for (const e of document.getElementsByClassName("lil-gui")) { |
37 | 44 | e.style.setProperty("--number-color", primaryColor);
|
38 | 45 | }
|
39 |
| - }); |
40 |
| - |
41 |
| - document.getElementById("theme-switch").addEventListener("change", function() { |
| 46 | + } |
42 | 47 |
|
| 48 | + function applyColorTheme() { |
43 | 49 | let theme;
|
44 |
| - if (this.checked) { |
| 50 | + if (themeSwitchCheckbox.checked) { |
45 | 51 | // dark theme
|
46 |
| - document.getElementById("theme-switch-icon").classList.remove("fa-moon"); |
47 |
| - document.getElementById("theme-switch-icon").classList.add("fa-sun"); |
| 52 | + themeSwitchIcon.classList.remove("fa-moon"); |
| 53 | + themeSwitchIcon.classList.add("fa-sun"); |
48 | 54 | theme = {"background-color": "30, 30, 30", "dark-background-color": "11, 23, 32", "text-color": "255, 255, 255", "dark-text-color": "136, 136, 136"};
|
| 55 | + themeSwitchCheckbox.checked = false; |
49 | 56 | } else {
|
50 | 57 | // light theme
|
51 |
| - document.getElementById("theme-switch-icon").classList.remove("fa-sun"); |
52 |
| - document.getElementById("theme-switch-icon").classList.add("fa-moon"); |
| 58 | + themeSwitchIcon.classList.remove("fa-sun"); |
| 59 | + themeSwitchIcon.classList.add("fa-moon"); |
53 | 60 | theme = {"background-color": "255, 250, 240", "dark-background-color": "210, 210, 220", "text-color": "0, 0, 0", "dark-text-color": "100, 100, 100"};
|
| 61 | + themeSwitchCheckbox.checked = true; |
54 | 62 | }
|
55 | 63 |
|
56 | 64 | // apply theme
|
|
59 | 67 | }
|
60 | 68 |
|
61 | 69 | // change background color of renderer
|
62 |
| - const viewerFrame = document.getElementById("viewer-frame"); |
63 | 70 | if (viewerFrame.contentWindow.setBackgroundColorCallback) viewerFrame.contentWindow.setBackgroundColorCallback();
|
64 |
| - }); |
| 71 | + } |
| 72 | + |
| 73 | + viewerFrame.addEventListener("load", applyPrimaryColor); |
| 74 | + themeSwitch.addEventListener("click", applyColorTheme); |
65 | 75 |
|
66 | 76 | // apply theme on load
|
67 | 77 | if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
68 |
| - document.getElementById("theme-switch").checked = true; |
69 |
| - document.getElementById("theme-switch").dispatchEvent(new Event("change")); |
| 78 | + themeSwitchCheckbox.checked = true; |
| 79 | + applyColorTheme(); |
70 | 80 | }
|
71 | 81 |
|
72 | 82 | function changeView(path) {
|
73 |
| - const viewerFrame = document.getElementById("viewer-frame"); |
74 |
| - const examples = document.getElementById("examples"); |
75 |
| - const homeButton = document.getElementById("home-button"); |
76 |
| - |
77 | 83 | if (path === "home") {
|
78 | 84 | viewerFrame.src = "";
|
79 | 85 | viewerFrame.style.display = "none";
|
80 | 86 | homeButton.style.display = "none";
|
81 |
| - examples.style.display = "grid"; |
| 87 | + examplesContainer.style.display = "grid"; |
82 | 88 | } else {
|
83 |
| - examples.style.display = "none"; |
| 89 | + examplesContainer.style.display = "none"; |
84 | 90 | viewerFrame.src = path;
|
85 | 91 | homeButton.style.display = "flex";
|
86 | 92 | viewerFrame.style.display = "unset";
|
|
89 | 95 | window.location.hash = path;
|
90 | 96 | }
|
91 | 97 |
|
92 |
| - const examples = { |
| 98 | + const exampleSources = { |
93 | 99 | "Playground": "playground",
|
94 | 100 | "PBR - Interactive": "pbr-interactive",
|
95 | 101 | "PBR - Materials": "pbr-materials",
|
|
98 | 104 | }
|
99 | 105 |
|
100 | 106 | // autogenerate examples
|
101 |
| - for (const [name, src] of Object.entries(examples)) { |
| 107 | + for (const [name, src] of Object.entries(exampleSources)) { |
102 | 108 | const example = document.createElement("div");
|
103 | 109 | example.className = "example";
|
104 | 110 | example.onclick = function() {changeView("examples/" + src + "/index.html")};
|
|
107 | 113 | <div>${name}</div>
|
108 | 114 | `;
|
109 | 115 |
|
110 |
| - document.getElementById("examples").appendChild(example); |
| 116 | + examplesContainer.appendChild(example); |
111 | 117 | }
|
112 | 118 | </script>
|
113 | 119 |
|
|
0 commit comments