-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
126 lines (103 loc) · 4.68 KB
/
Copy pathscript.js
File metadata and controls
126 lines (103 loc) · 4.68 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
let wakeLock = null;
async function requestWakeLock() {
try {
wakeLock = await navigator.wakeLock.request('screen');
console.log('Wake Lock: Active');
} catch (err) {
console.error(`${err.name}, ${err.message}`);
}
}
function releaseWakeLock() {
if (wakeLock) {
wakeLock.release().then(() => {
wakeLock = null;
console.log('Wake Lock: Released');
});
}
}
fetch('framesData.lz')
.then(response => response.text())
.then(data => {
const decompressedData = LZString.decompressFromBase64(data); // Decompress the fetched data
// Ensure it is parsed as an array
const framesData = JSON.parse(decompressedData); // Parse the JSON to get the array
initializeAnimation(framesData); // Call your initialization function
})
.catch(error => console.error('Error fetching framesData.lz:', error));
function initializeAnimation(framesData) {
const frameCount = framesData.length; // Total number of frames
const fps = 30; // Frames per second
const frameDuration = 1000 / fps; // Duration of each frame in milliseconds
const asciiDisplay = document.getElementById('ascii-display');
const audioPlayer = document.getElementById('audio-player');
const playButton = document.getElementById('play-button');
let currentFrame = 0;
// 4:3 aspect ratio dimensions
const aspectRatio = 4 / 3;
// Function to adjust the size of the ASCII display while maintaining aspect ratio
function adjustDisplaySize() {
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
// Set the width and height based on the aspect ratio
let displayWidth = windowWidth;
let displayHeight = windowWidth / aspectRatio;
// If the height exceeds the window height, adjust accordingly
if (displayHeight > windowHeight) {
displayHeight = windowHeight;
displayWidth = displayHeight * aspectRatio;
}
// Adjust font size based on the calculated display size
const fontSize = displayWidth / 62; // Adjust this value to fine-tune the size
asciiDisplay.style.fontSize = `${fontSize}px`;
}
// Call the function initially and on window resize
adjustDisplaySize();
window.addEventListener('resize', adjustDisplaySize);
// Play the animation when the button is clicked
playButton.addEventListener('click', () => {
// Disable the play button to prevent further clicks
playButton.disabled = true;
// Hide the play button visually
playButton.style.display = 'none';
requestWakeLock(); // Request the wake lock when animation starts
// Pre-load the audio without playing it (this satisfies iOS's interaction requirement)
audioPlayer.load();
// Set a timeout to start both audio and animation simultaneously
setTimeout(() => {
audioPlayer.play(); // Play the audio after the delay
playAnimation(); // Start the animation after the same delay
}, 250); // Delay both for sync
});
// Function to play the animation
function playAnimation() {
const startTime = performance.now();
function renderFrame() {
const elapsedTime = performance.now() - startTime;
const expectedFrame = Math.floor(elapsedTime / frameDuration);
// Check if expectedFrame is within bounds of framesData
if (expectedFrame < framesData.length) {
const asciiFrame = framesData[expectedFrame]; // Get the frame from the array
asciiDisplay.textContent = asciiFrame.replace(/\\n/g, '\n'); // Replace '\n' with actual newlines
currentFrame = expectedFrame;
} else {
// Stop the animation after the last frame
asciiDisplay.textContent = framesData[framesData.length - 1].replace(/\\n/g, '\n'); // Display the last frame
releaseWakeLock(); // Release the wake lock after the last frame
return; // Exit the function to stop rendering
}
requestAnimationFrame(renderFrame); // Continue animation loop
}
renderFrame(); // Start rendering the frames
}
// Release the wake lock when the page is hidden or when exiting fullscreen
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
releaseWakeLock();
}
});
document.addEventListener('fullscreenchange', () => {
if (!document.fullscreenElement) {
releaseWakeLock();
}
});
}