-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
200 lines (180 loc) · 4.96 KB
/
script.js
File metadata and controls
200 lines (180 loc) · 4.96 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
let audioCtx;
let bufferSource;
var dark;
dark=0;
document.getElementById("playBtn").addEventListener("click", playWave);
document.getElementById("stopBtn").addEventListener("click", stopWave);
function playWave() {
stopWave(); // Stop previous wave if any
const formula = document.getElementById("formula").value;
const vf = parseFloat(document.getElementById("vf").value);
const va = parseFloat(document.getElementById("va").value);
const vp = parseFloat(document.getElementById("vp").value);
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
const sampleRate = audioCtx.sampleRate;
const duration = 2; // seconds
const buffer = audioCtx.createBuffer(1, sampleRate * duration, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < data.length; i++) {
const t = i / sampleRate;
try {
// eslint-disable-next-line no-eval
let y = eval(formula);
// Clamp y to [-1, 1] to prevent clipping
if (y > 1) y = 1;
if (y < -1) y = -1;
data[i] = y;
} catch (e) {
data[i] = 0;
}
}
bufferSource = audioCtx.createBufferSource();
bufferSource.buffer = buffer;
bufferSource.connect(audioCtx.destination);
bufferSource.start();
}
function stopWave() {
if (bufferSource) {
bufferSource.stop();
bufferSource.disconnect();
}
}
// Code for preset menu - created by ChatGPT
const menuButton = document.getElementById("loadPresetBtn");
const popupMenu = document.getElementById("presetMenu");
const box = document.getElementById("formula");
// Show or hide the menu
menuButton.addEventListener("click", () => {
popupMenu.style.display =
popupMenu.style.display === "none" ? "block" : "none";
});
// When you click an option, write its value to the textbox
popupMenu.addEventListener("click", event => {
const item = event.target.closest(".menu-item");
if (!item) return; // clicked outside an item
box.value = item.dataset.value;
popupMenu.style.display = "none"; // hide the menu again
});
// root.style.setProperty("--text", "#333");
// Theme Definitions
// Add new themes here
const themes = [
{
"id": "light",
"name": "Default",
"text": "#333",
"boxtext": "#333",
"bg-main": "#f0f0f0",
"bg-btn": "#fcfcfc",
"bg-menu": "#eee",
"textbox": "white",
"menu-border": "1px solid #aaa",
"box-border": "1px solid light-dark(rgb(118, 118, 118), rgb(133, 133, 133))",
"btn-border-clr": "buttonborder",
"btn-radius": "10px"
},
{
"id": "dark",
"name": "Dark Mode",
"text": "#f0f0f0",
"boxtext": "#f0f0f0",
"bg-main": "#232323",
"bg-btn": "#323232",
"bg-menu": "#454545",
"textbox": "#323232",
"menu-border": "1px solid #aaa",
"box-border": "1px solid light-dark(rgb(118, 118, 118), rgb(133, 133, 133))",
"btn-border-clr": "buttonborder",
"btn-radius": "10px"
},
{
"id": "blue",
"name": "True Blue",
"text": "#f0f0f0",
"boxtext": "#f0f0f0",
"bg-main": "#055ae3",
"bg-btn": "#4287f5",
"bg-menu": "#356bc4",
"textbox": "#4287f5",
"menu-border": "1px solid #aaa",
"box-border": "1px solid #127",
"btn-border-clr": "#125",
"btn-radius": "10px"
},
{
"id": "highcon",
"name": "High Contrast",
"text": "#ff5",
"boxtext": "#ff5",
"bg-main": "#000",
"bg-btn": "#000",
"bg-menu": "#000",
"textbox": "#000",
"menu-border": "3px solid #ff5",
"box-border": "2px solid #ff5",
"btn-border-clr": "#fff",
"btn-radius": "10px"
},
{
"id": "rainbow",
"name": "Crazy Colours",
"text": "#ff0",
"boxtext": "#ff0",
"bg-main": "#11d",
"bg-btn": "#15bb15",
"bg-menu": "#a37",
"textbox": "#f21",
"menu-border": "3px solid #f0f",
"box-border": "2px solid #0ff",
"btn-border-clr": "#cc7522",
"btn-radius": "10px"
},
{
"id": "smbnes",
"name": "Itsumi 1-1",
"text": "#b92716",
"boxtext": "#138400",
"bg-main": "#6481f9",
"bg-btn": "#f0ac3f",
"bg-menu": "#f0ac3f",
"textbox": "#f0ac3f",
"menu-border": "3px solid #b92716",
"box-border": "5px solid #b92716",
"btn-border-clr": "#b92716",
"btn-radius": "0px"
}
];
// Code for changing themes (and theme menu) - created by ChatGPT
const root = document.documentElement;
const menu = document.getElementById("themeMenu");
const themeButton = document.getElementById("theme-button");
// build menu directly
menu.innerHTML = "";
themes.forEach(theme => {
const item = document.createElement("div");
item.className = "menu-item";
item.dataset.value = theme.id;
item.textContent = theme.name;
menu.appendChild(item);
});
// theme applier
function applyTheme(id) {
const theme = themes.find(t => t.id === id);
if (!theme) return;
for (const [key, value] of Object.entries(theme)) {
if (key !== "id" && key !== "name") {
root.style.setProperty(`--${key}`, value);
}
}
}
// toggle menu
themeButton.addEventListener("click", () => {
menu.style.display = menu.style.display === "block" ? "none" : "block";
});
// click to switch theme
menu.addEventListener("click", event => {
const item = event.target.closest(".menu-item");
if (!item) return;
applyTheme(item.dataset.value);
menu.style.display = "none";
});