-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.js
More file actions
322 lines (281 loc) · 10.2 KB
/
ui.js
File metadata and controls
322 lines (281 loc) · 10.2 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
function initUI() {
// -------------------------------
// Presets
// -------------------------------
function populatePresetSelector() {
const presetSelector = document.getElementById('presetSelector');
presetOptions.forEach(option => {
const opt = document.createElement('option');
opt.value = option.value;
opt.textContent = option.label;
presetSelector.appendChild(opt);
});
}
document.addEventListener('DOMContentLoaded', () => {
populatePresetSelector();
updateLayerListUI();
});
document.getElementById('presetSelector').addEventListener('change', function() {
const preset = presets[this.value];
if (preset) {
const activeLayer = getActiveLayer();
if(!activeLayer) return;
Object.keys(preset).forEach(key => {
activeLayer.params[key] = preset[key];
});
saveState();
updateUIFromState();
drawComposition();
}
});
// -------------------------------
// Input Handlers
// -------------------------------
const handleInput = function() {
if (this.id === 'layerList') return; // Handled separately
const activeLayer = getActiveLayer();
if (!activeLayer) return;
saveState();
if (this.type === 'checkbox') {
if (masterAudioParams.hasOwnProperty(this.id) || this.id === 'autoRotate') {
masterAudioParams[this.id] = this.checked;
} else {
activeLayer.params[this.id] = this.checked;
}
} else {
const val = this.type === 'number' || this.type === 'range' ? parseFloat(this.value) : this.value;
if (masterAudioParams.hasOwnProperty(this.id)) {
masterAudioParams[this.id] = val;
} else {
activeLayer.params[this.id] = val;
}
}
if (this.type === 'range' || this.type === 'color' || this.tagName.toLowerCase() === 'select') {
const valueSpan = document.getElementById(this.id + 'Value');
if (valueSpan) {
valueSpan.textContent = (this.type === 'range' && this.step.includes('.')) ? parseFloat(this.value).toFixed(1) : this.value;
}
}
if (this.id === 'scale' && !masterAudioParams.audioReactive) {
window.baseScale = parseFloat(this.value);
}
if (this.id === 'lineWidth' && !masterAudioParams.audioReactive) {
window.baseLineWidth = parseFloat(this.value);
}
// Auto-Rotate handling
if (this.id === 'autoRotate') {
if (this.checked) animateRotation();
}
drawComposition();
};
document.querySelectorAll('input, select').forEach(input => {
input.addEventListener('input', handleInput);
input.addEventListener('change', handleInput);
});
// Layer Management UI
document.getElementById('addSpiralLayerBtn').addEventListener('click', () => addLayer('Original Spiral'));
document.getElementById('addFractalLayerBtn').addEventListener('click', () => addLayer('Fractal'));
document.getElementById('removeLayerBtn').addEventListener('click', () => removeActiveLayer());
document.getElementById('layerList').addEventListener('change', (e) => {
appState.activeLayerIndex = parseInt(e.target.value);
updateUIFromState();
});
// -------------------------------
// Audio Reactive Setup
// -------------------------------
const audioToggleButton = document.getElementById('audioToggleButton');
document.getElementById('audioReactive').addEventListener('change', function() {
masterAudioParams.audioReactive = this.checked;
document.getElementById('audioOptions').style.display = this.checked ? 'block' : 'none';
audioToggleButton.style.display = this.checked ? 'inline-block' : 'none';
if (this.checked && audioContext) {
animateAudioReactive();
} else {
drawComposition();
}
});
audioToggleButton.addEventListener('click', function() {
if (!audioContext) {
// Start Audio
const activeLayer = getActiveLayer();
if(activeLayer) {
window.baseScale = activeLayer.params.scale;
}
initAudio().then(() => {
if (audioContext) {
this.textContent = 'Stop Audio';
if (masterAudioParams.audioReactive) {
animateAudioReactive();
}
}
});
} else {
// Stop Audio
stopAudio();
this.textContent = 'Start Audio';
}
});
// -------------------------------
// Controls Toggle & Info Modal
// -------------------------------
const controlsOverlay = document.getElementById('controlsOverlay');
const toggleButton = document.getElementById('toggleControls');
const infoButton = document.getElementById('infoButton');
const infoModal = document.getElementById('infoModal');
const closeModal = document.querySelector('.close-modal');
infoButton.addEventListener('click', () => {
infoModal.style.display = 'block';
});
closeModal.addEventListener('click', () => {
infoModal.style.display = 'none';
});
window.addEventListener('click', (event) => {
if (event.target == infoModal) {
infoModal.style.display = 'none';
}
});
const controlsNotice = document.createElement('div');
controlsNotice.id = 'controlsNotice';
controlsNotice.className = 'controls-notice';
document.body.appendChild(controlsNotice);
toggleButton.addEventListener('click', function() {
if (controlsOverlay.style.display === 'none') {
controlsOverlay.style.display = 'block';
this.textContent = 'Hide Controls';
} else {
controlsOverlay.style.display = 'none';
this.textContent = 'Show Controls';
showControlsNotice();
}
});
canvas.addEventListener('click', function(e) {
if (controlsOverlay.style.display === 'none' && !document.fullscreenElement) {
controlsOverlay.style.display = 'block';
toggleButton.textContent = 'Hide Controls';
}
});
function showControlsNotice() {
controlsNotice.textContent = 'Click anywhere to show controls';
controlsNotice.style.display = 'block';
setTimeout(() => {
controlsNotice.style.display = 'none';
}, 3000);
}
// -------------------------------
// Mobile Touch Controls
// -------------------------------
let initialPinchDistance = null;
let initialScale = null;
let touchStartTime = null;
const TAP_THRESHOLD = 200;
canvas.addEventListener('touchstart', handleTouchStart, { passive: true });
canvas.addEventListener('touchmove', handleTouchMove, { passive: false });
function handleTouchStart(e) {
if (document.fullscreenElement && isMobile) {
touchStartTime = Date.now();
}
if (e.touches.length === 2) {
initialPinchDistance = Math.hypot(
e.touches[0].pageX - e.touches[1].pageX,
e.touches[0].pageY - e.touches[1].pageY
);
const activeLayer = getActiveLayer();
initialScale = activeLayer ? activeLayer.params.scale : 30;
}
}
function handleTouchMove(e) {
e.preventDefault();
if (e.touches.length === 2 && initialPinchDistance) {
const currentDistance = Math.hypot(
e.touches[0].pageX - e.touches[1].pageX,
e.touches[0].pageY - e.touches[1].pageY
);
const newScale = initialScale * (currentDistance / initialPinchDistance);
const activeLayer = getActiveLayer();
if (activeLayer) {
activeLayer.params.scale = Math.min(Math.max(newScale, 1), 100);
if (!masterAudioParams.audioReactive || !masterAudioParams.audioScale) {
window.baseScale = activeLayer.params.scale;
}
updateUIFromState();
saveState();
drawComposition();
}
} else if (e.touches.length === 1) {
const deltaX = e.touches[0].movementX || 0;
const activeLayer = getActiveLayer();
if(activeLayer) {
const currentRotation = activeLayer.params.rotation || 0;
activeLayer.params.rotation = (currentRotation + deltaX * 0.5 + 360) % 360;
updateUIFromState();
saveState();
drawComposition();
}
}
}
function handleTouchEnd(e) {
if (document.fullscreenElement && isMobile && e.touches.length === 0) {
const touchDuration = Date.now() - touchStartTime;
if (touchDuration < TAP_THRESHOLD && initialPinchDistance === null) {
document.exitFullscreen();
}
}
initialPinchDistance = null;
}
// -------------------------------
// Fullscreen Handling
// -------------------------------
const fullscreenButton = document.getElementById('fullscreenButton');
const fullscreenOverlay = document.getElementById('fullscreenOverlay');
const isMobile = 'ontouchstart' in window || window.innerWidth < 768;
fullscreenButton.addEventListener('click', () => {
if (canvas.requestFullscreen) {
canvas.requestFullscreen();
showFullscreenOverlay();
controlsOverlay.style.display = 'none';
}
});
document.addEventListener('fullscreenchange', () => {
if (!document.fullscreenElement) {
fullscreenOverlay.style.display = 'none';
controlsOverlay.style.display = 'block';
toggleButton.textContent = 'Hide Controls';
}
});
function showFullscreenOverlay() {
fullscreenOverlay.textContent = isMobile ? 'Tap to exit full screen' : 'Press ESC to exit full screen';
fullscreenOverlay.style.display = 'block';
setTimeout(() => {
fullscreenOverlay.style.display = 'none';
}, 3000);
}
// -------------------------------
// Screensaver (Idle Timer)
// -------------------------------
let idleTimer;
const IDLE_TIMEOUT = 4000; // 4 seconds
const uiElements = [controlsOverlay, toggleButton, infoButton];
function resetIdleTimer() {
uiElements.forEach(el => el.classList.remove('idle'));
clearTimeout(idleTimer);
if (controlsOverlay.style.display !== 'none') {
idleTimer = setTimeout(() => {
uiElements.forEach(el => el.classList.add('idle'));
}, IDLE_TIMEOUT);
}
}
['mousemove', 'mousedown', 'touchstart', 'keydown'].forEach(evt => {
window.addEventListener(evt, resetIdleTimer, true);
});
resetIdleTimer();
}
function setRatio(ratio) {
const activeLayer = getActiveLayer();
if(activeLayer && activeLayer.params.layerRatio !== undefined) {
activeLayer.params.layerRatio = ratio;
updateUIFromState();
saveState();
drawComposition();
}
}
initUI();