-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmystify.html
More file actions
534 lines (468 loc) · 23.6 KB
/
mystify.html
File metadata and controls
534 lines (468 loc) · 23.6 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Atari Mystify</title>
<!-- Tailwind CSS CDN for styling -->
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* Apply Inter font and basic body styling */
body {
font-family: "Inter", sans-serif;
margin: 0;
overflow: hidden; /* Prevent scrollbars */
background-color: #000; /* Black background for the canvas */
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100vw;
}
/* Canvas will fill the entire screen */
canvas {
display: block;
background-color: #000; /* Ensure canvas background is black */
}
/* Styling for the control panel */
.control-panel {
position: absolute;
top: 20px;
right: 20px;
background-color: rgba(0, 0, 0, 0.7); /* Semi-transparent black */
padding: 1.5rem;
border-radius: 0.75rem; /* Rounded corners */
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
color: #fff;
max-width: 300px;
z-index: 10; /* Ensure it's above the canvas */
transition: opacity 0.3s ease-in-out; /* Add transition for fade effect */
}
/* Class to hide the control panel */
.control-panel.hidden-controls {
opacity: 0;
pointer-events: none; /* Disable interactions when hidden */
}
.control-group {
margin-bottom: 1rem;
}
.control-group label {
display: block;
margin-bottom: 0.5rem;
font-weight: bold;
color: #a0aec0; /* Light gray label color */
}
.control-group input[type="range"],
.control-group select {
width: 100%;
padding: 0.5rem;
border-radius: 0.5rem;
border: 1px solid #4a5568; /* Darker border */
background-color: #2d3748; /* Dark background for inputs */
color: #fff;
appearance: none; /* Remove default browser styling for range input */
-webkit-appearance: none;
}
/* Custom styling for range input thumb */
.control-group input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 16px;
height: 16px;
border-radius: 50%;
background: #63b3ed; /* Blue thumb */
cursor: pointer;
margin-top: -6px; /* Center thumb vertically */
box-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
}
.control-group input[type="range"]::-moz-range-thumb {
width: 16px;
height: 16px;
border-radius: 50%;
background: #63b3ed;
cursor: pointer;
box-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
}
.control-group input[type="range"]::-webkit-slider-runnable-track {
height: 4px;
background: #4a5568;
border-radius: 2px;
}
.control-group input[type="range"]::-moz-range-track {
height: 4px;
background: #4a5568;
border-radius: 2px;
}
.control-group select option {
background-color: #2d3748; /* Dark background for select options */
color: #fff;
}
/* Checkbox specific styling */
.control-group input[type="checkbox"] {
width: auto; /* Override 100% width for checkbox */
margin-right: 0.5rem;
transform: scale(1.2); /* Make checkbox slightly larger */
}
.reset-button {
width: 100%;
padding: 0.75rem;
background-color: #4299e1; /* Blue button */
color: #fff;
border: none;
border-radius: 0.5rem;
cursor: pointer;
font-weight: bold;
transition: background-color 0.2s ease-in-out, transform 0.1s ease-in-out;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
.reset-button:hover {
background-color: #3182ce; /* Darker blue on hover */
transform: translateY(-1px);
}
.reset-button:active {
transform: translateY(0);
box-shadow: none;
}
</style>
</head>
<body>
<canvas id="mystifyCanvas"></canvas>
<div class="control-panel">
<h2 class="text-xl font-bold mb-4 text-center text-blue-300">Atari Mystify Controls</h2>
<div class="control-group">
<label for="numObjects">Number of Objects: <span id="numObjectsValue">5</span></label>
<input type="range" id="numObjects" min="1" max="20" value="5">
</div>
<div class="control-group">
<label for="numNodes">Nodes per Object: <span id="numNodesValue">4</span></label>
<input type="range" id="numNodes" min="3" max="10" value="4">
</div>
<div class="control-group">
<label for="speedControl">Speed: <span id="speedValue">1.0</span>x</label>
<input type="range" id="speedControl" min="0.1" max="10.0" step="0.1" value="1.0">
</div>
<div class="control-group">
<label for="trailFrequency">Trail Frequency: <span id="trailFrequencyValue">10</span></label>
<input type="range" id="trailFrequency" min="1" max="50" value="10">
</div>
<div class="control-group flex items-center">
<input type="checkbox" id="individualPrints" class="mr-2">
<label for="individualPrints" class="mb-0">Individual Prints (Hollow)</label>
</div>
<div class="control-group flex items-center">
<input type="checkbox" id="autoHideControls" class="mr-2">
<label for="autoHideControls" class="mb-0">Auto-hide Controls</label>
</div>
<div class="control-group">
<label for="colorPalette">Color Palette:</label>
<select id="colorPalette">
<option value="classicAtari">Classic Atari</option>
<option value="vibrantAtari">Vibrant Atari</option>
<option value="monochrome">Monochrome (Green)</option>
</select>
</div>
<button id="resetButton" class="reset-button">Reset Animation</button>
</div>
<script>
// Get the canvas element and its 2D rendering context
const canvas = document.getElementById('mystifyCanvas');
const ctx = canvas.getContext('2d');
const controlPanel = document.querySelector('.control-panel'); // Get the control panel element
// Global variables to manage animation and settings
let objects = []; // Array to hold all animated objects
let animationFrameId; // Stores the ID of the requestAnimationFrame, used for cancellation
let frameCount = 0; // Global frame counter for individual prints
// Default settings for the animation
const settings = {
numObjects: 5,
numNodes: 4,
speedMultiplier: 1.0, // New setting for overall animation speed
trailFrequency: 10, // Higher value means more prints visible at once.
trailMode: 'fade', // New setting: 'fade' or 'individual'
colorPalette: 'classicAtari', // Default color palette
initialSpeed: 2, // Base speed for objects (used for initial velocity generation)
maxSpeedVariance: 1, // Max random speed added/subtracted
autoHideControls: true // New setting: controls auto-hide by default
};
// Define Atari-inspired color palettes
const palettes = {
classicAtari: [
'#A00000', // Dark Red
'#00A000', // Dark Green
'#0000A0', // Dark Blue
'#A0A000', // Dark Yellow
'#A000A0', // Dark Magenta
'#00A0A0', // Dark Cyan
'#C0C0C0', // Light Gray
'#404040' // Dark Gray
],
vibrantAtari: [
'#FF0000', // Bright Red
'#00FF00', // Bright Green
'#0000FF', // Bright Blue
'#FFFF00', // Bright Yellow
'#FF00FF', // Bright Magenta
'#00FFFF', // Bright Cyan
'#FFFFFF', // White
'#808080' // Medium Gray
],
monochrome: [
'#00FF00', // Bright Green (for old CRT feel)
'#00CC00', // Medium Green
'#009900', // Dark Green
'#006600' // Very Dark Green
]
};
/**
* Initializes the canvas size and sets up event listeners for window resize
* and UI controls.
*/
function init() {
// Set initial canvas size to fill the window
resizeCanvas();
// Add event listener to resize canvas when window size changes
window.addEventListener('resize', resizeCanvas);
// Get UI elements
const numObjectsInput = document.getElementById('numObjects');
const numObjectsValueSpan = document.getElementById('numObjectsValue');
const numNodesInput = document.getElementById('numNodes');
const numNodesValueSpan = document.getElementById('numNodesValue');
const speedControlInput = document.getElementById('speedControl');
const speedValueSpan = document.getElementById('speedValue');
const trailFrequencyInput = document.getElementById('trailFrequency');
const trailFrequencyValueSpan = document.getElementById('trailFrequencyValue');
const individualPrintsCheckbox = document.getElementById('individualPrints');
const autoHideControlsCheckbox = document.getElementById('autoHideControls'); // New auto-hide checkbox
const colorPaletteSelect = document.getElementById('colorPalette');
const resetButton = document.getElementById('resetButton');
// Set initial values for UI elements based on settings
numObjectsInput.value = settings.numObjects;
numObjectsValueSpan.textContent = settings.numObjects;
numNodesInput.value = settings.numNodes;
numNodesValueSpan.textContent = settings.numNodes;
speedControlInput.value = settings.speedMultiplier;
speedValueSpan.textContent = settings.speedMultiplier.toFixed(1);
trailFrequencyInput.value = settings.trailFrequency;
trailFrequencyValueSpan.textContent = settings.trailFrequency;
individualPrintsCheckbox.checked = (settings.trailMode === 'individual');
autoHideControlsCheckbox.checked = settings.autoHideControls; // Set initial state for auto-hide checkbox
colorPaletteSelect.value = settings.colorPalette;
// Add event listeners for UI control changes
numObjectsInput.addEventListener('input', (e) => {
settings.numObjects = parseInt(e.target.value);
numObjectsValueSpan.textContent = settings.numObjects;
resetAnimation();
});
numNodesInput.addEventListener('input', (e) => {
settings.numNodes = parseInt(e.target.value);
numNodesValueSpan.textContent = settings.numNodes;
resetAnimation();
});
speedControlInput.addEventListener('input', (e) => {
settings.speedMultiplier = parseFloat(e.target.value);
speedValueSpan.textContent = settings.speedMultiplier.toFixed(1);
});
trailFrequencyInput.addEventListener('input', (e) => {
settings.trailFrequency = parseInt(e.target.value);
trailFrequencyValueSpan.textContent = settings.trailFrequency;
if (settings.trailMode === 'individual') {
resetAnimation();
}
});
individualPrintsCheckbox.addEventListener('change', (e) => {
settings.trailMode = e.target.checked ? 'individual' : 'fade';
resetAnimation();
});
// Event listener for the new auto-hide controls checkbox
autoHideControlsCheckbox.addEventListener('change', (e) => {
settings.autoHideControls = e.target.checked;
// If auto-hide is turned off, ensure controls are visible
if (!settings.autoHideControls) {
controlPanel.classList.remove('hidden-controls');
} else {
// If auto-hide is turned on, and mouse is not over the body, hide them
// This handles cases where user checks it while mouse is already outside
if (!document.body.matches(':hover')) { // Check if mouse is currently over the body
controlPanel.classList.add('hidden-controls');
}
}
});
colorPaletteSelect.addEventListener('change', (e) => {
settings.colorPalette = e.target.value;
resetAnimation();
});
resetButton.addEventListener('click', resetAnimation);
// --- Auto-hide controls logic ---
// Initial state: If auto-hide is enabled by default, hide the controls on load.
// They will reappear when the mouse enters the document body.
if (settings.autoHideControls) {
controlPanel.classList.add('hidden-controls');
}
// Event listener for mouse leaving the document body (browser window content area)
document.body.addEventListener('mouseleave', () => {
if (settings.autoHideControls) {
controlPanel.classList.add('hidden-controls');
}
});
// Event listener for mouse entering the document body (browser window content area)
document.body.addEventListener('mouseenter', () => {
if (settings.autoHideControls) {
controlPanel.classList.remove('hidden-controls');
}
});
// Start the initial animation
resetAnimation();
}
/**
* Resizes the canvas to fill the entire browser window.
* This function is called on initial load and whenever the window is resized.
*/
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// When canvas resizes, the content is cleared, so we need to reset the animation
resetAnimation();
}
/**
* Generates a single animated object with a specified number of nodes.
* Each object now contains its current nodes, a history of snapshots for trails, and its color.
* @returns {Object} An object containing nodes, trailSnapshots, and color for a single animated shape.
*/
function generateObject() {
const currentPalette = palettes[settings.colorPalette];
// Pick a random color from the selected palette for this object
const objectColor = currentPalette[Math.floor(Math.random() * currentPalette.length)];
const nodes = [];
for (let i = 0; i < settings.numNodes; i++) {
// Random initial position within canvas bounds
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
// Random initial velocity, ensuring some movement
// Initial speed is set here, then scaled by speedMultiplier in animate()
const dx = (Math.random() * settings.maxSpeedVariance * 2 - settings.maxSpeedVariance) + (Math.random() < 0.5 ? settings.initialSpeed : -settings.initialSpeed);
const dy = (Math.random() * settings.maxSpeedVariance * 2 - settings.maxSpeedVariance) + (Math.random() < 0.5 ? settings.initialSpeed : -settings.initialSpeed);
// Nodes now only store position and velocity; color is stored at the object level
nodes.push({ x, y, dx, dy });
}
// Return an object with current nodes, an empty trail history, and its color
return { nodes: nodes, trailSnapshots: [], color: objectColor };
}
/**
* Resets the animation by clearing existing objects and generating new ones.
* Cancels any ongoing animation frame and starts a new one.
*/
function resetAnimation() {
// Cancel any existing animation frame to prevent multiple loops running
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
}
objects = []; // Clear the existing objects
frameCount = 0; // Reset frame counter on animation reset
// Generate new objects based on current settings
for (let i = 0; i < settings.numObjects; i++) {
objects.push(generateObject());
}
// Clear the canvas completely before starting new animation to remove old trails
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Start the animation loop
animate();
}
/**
* The main animation loop.
* Updates object positions, handles collisions, and redraws the scene.
*/
function animate() {
// Request the next animation frame for smooth animation
animationFrameId = requestAnimationFrame(animate);
frameCount++; // Increment frame counter
// --- Canvas Clearing Logic ---
if (settings.trailMode === 'fade') {
// For 'fade' mode, draw a semi-transparent black rectangle over the entire canvas
// This creates the continuous fading trail effect.
ctx.fillStyle = `rgba(0, 0, 0, ${1 / settings.trailFrequency})`;
ctx.fillRect(0, 0, canvas.width, canvas.height);
} else { // settings.trailMode === 'individual'
// For 'individual' mode, clear the canvas completely each frame.
// This is necessary to redraw all historical prints with updated opacities
// and avoid accumulation without explicit fade.
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
// --- Object Update and Drawing Logic ---
objects.forEach(obj => {
// 1. Update current node positions and handle collisions for all nodes
// This block ALWAYS runs to ensure smooth movement, regardless of trail mode or frequency.
obj.nodes.forEach((node) => {
// Update node position based on its velocity, scaled by the speedMultiplier
node.x += node.dx * settings.speedMultiplier;
node.y += node.dy * settings.speedMultiplier;
// Boundary collision detection:
// If node hits left or right edge, reverse horizontal velocity
if (node.x < 0 || node.x > canvas.width) {
node.dx *= -1;
// Adjust position slightly to prevent sticking to the edge
node.x = node.x < 0 ? 0 : canvas.width;
}
// If node hits top or bottom edge, reverse vertical velocity
if (node.y < 0 || node.y > canvas.height) {
node.dy *= -1;
// Adjust position slightly to prevent sticking to the edge
node.y = node.y < 0 ? 0 : canvas.height;
}
});
// 2. Drawing logic based on trailMode
if (settings.trailMode === 'fade') {
// Draw the current filled shape (for 'fade' mode)
ctx.strokeStyle = obj.color;
ctx.fillStyle = obj.color;
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(obj.nodes[0].x, obj.nodes[0].y);
obj.nodes.forEach(node => ctx.lineTo(node.x, node.y));
ctx.closePath();
ctx.stroke();
ctx.fill();
} else { // settings.trailMode === 'individual'
// Add current snapshot to trail if frequency condition met
// This is for capturing the "print" at specific intervals.
if (frameCount % settings.trailFrequency === 0) {
const snapshot = obj.nodes.map(node => ({ x: node.x, y: node.y }));
obj.trailSnapshots.push(snapshot);
}
// Limit trail length (number of visible prints).
// If the number of snapshots exceeds the desired trail frequency, remove the oldest.
while (obj.trailSnapshots.length > settings.trailFrequency) {
obj.trailSnapshots.shift(); // Remove the oldest snapshot from the beginning of the array
}
// Draw all trail snapshots with fading opacity
// This block ALWAYS runs to ensure smooth fading of existing prints.
obj.trailSnapshots.forEach((snapshot, index) => {
// Calculate opacity: oldest prints (index 0) are most transparent,
// newest (last index) are fully opaque. This creates the fade-out effect.
// Adding 1 to index and using length ensures alpha ranges from >0 to 1.
const alpha = (index + 1) / obj.trailSnapshots.length;
ctx.strokeStyle = obj.color;
ctx.lineWidth = 2;
ctx.globalAlpha = alpha; // Apply opacity to the current drawing operation
ctx.beginPath();
ctx.moveTo(snapshot[0].x, snapshot[0].y);
snapshot.forEach(pos => ctx.lineTo(pos.x, pos.y)); // Draw lines based on snapshot positions
ctx.closePath();
ctx.stroke(); // Draw only the outline (hollow)
});
// Draw the current animating shape (the "person walking")
ctx.globalAlpha = 1; // Ensure full opacity for the main shape
ctx.strokeStyle = obj.color;
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(obj.nodes[0].x, obj.nodes[0].y);
obj.nodes.forEach(node => ctx.lineTo(node.x, node.y));
ctx.closePath();
ctx.stroke(); // Draw only the outline (hollow) for the main shape too, matching the prints
}
});
}
// Initialize the application when the window has fully loaded
window.onload = init;
</script>
</body>
</html>