-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.js
More file actions
230 lines (192 loc) · 6.94 KB
/
run.js
File metadata and controls
230 lines (192 loc) · 6.94 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
// ==========================================================
// ZIP‑BASED ASCII ANIMATION LOADER (fflate)
// ==========================================================
/**
* @fileoverview ASCII animation player for micr.dev.
* Loads a ZIP archive of pre-rendered text frames and plays them
* back in the browser console at a configurable FPS, optionally
* synchronised with an audio track.
*
* The ZIP is fetched once and kept in memory (`FRAME_MAP`). On
* tab blur the animation pauses; on focus it clears the intro.
* Type `makeLove()` in the console to start the Bad Apple!!
* ASCII playback with audio.
*
* @module run
* @requires fflate (ESM, loaded from CDN)
*/
import {
unzipSync,
strFromU8,
} from "https://cdn.jsdelivr.net/npm/fflate/esm/browser.js";
const ZIP_PATH = "./assets/frames.zip";
const totalFrames = 6572;
const fps = 30;
const indent = " ";
let FRAME_MAP = null;
let zipReady = false;
/**
* Fetch and decompress the frames ZIP from the server.
* Idempotent — subsequent calls return immediately.
* @returns {Promise<void>}
*/
async function loadZip() {
if (zipReady) return;
const res = await fetch(ZIP_PATH);
const buf = new Uint8Array(await res.arrayBuffer());
FRAME_MAP = unzipSync(buf);
zipReady = true;
console.log(
"(ASCII ZIP extracted:",
Object.keys(FRAME_MAP).length,
"frames)"
);
}
/**
* Retrieve the raw text content of a single frame by index.
* Loads the ZIP lazily on the first call.
* @param {number} idx - Zero-padded frame index (1–6572).
* @returns {Promise<string|null>} Frame text or `null` if not found.
*/
async function getFrame(idx) {
if (!zipReady) await loadZip();
const name = "out" + idx.toString().padStart(4, "0") + ".jpg.txt";
const entry = FRAME_MAP[name];
if (!entry) return null;
return strFromU8(entry);
}
/**
* No-op preload stub — the ZIP approach requires no separate
* network requests per frame. Kept for API compatibility.
* @returns {Promise<void>}
*/
async function preloadFrames() { return; }
// ==========================================================
// ANIMATION LOGIC
// ==========================================================
let frameIndex = 1;
let interval = null;
let stopFlag = false;
/** Pause animation and restart intro when the tab loses focus. */
window.addEventListener("blur", () => {
if (!interval) return;
stopFlag = true;
clearInterval(interval);
interval = null;
if (window.eggAudio) {
window.eggAudio.pause();
window.eggAudio.currentTime = 0;
}
setTimeout(() => {
console.clear();
intro();
}, 200);
});
/** Log a message when the tab regains focus — animation stays paused. */
window.addEventListener("focus", () => {
console.log("(animation paused — tab re-focused)");
});
/** Advance the animation one line on each call. */
function softClear() {
console.log("\n");
}
// ==========================================================
// PLAYBACK
// ==========================================================
/**
* Begin ASCII frame playback in the browser console.
* Schedules `getFrame` lookups at `1000/fps` ms intervals and
* writes each frame to `console.log`. When all frames are
* exhausted a credits line is printed after a 1 s delay.
* @returns {Promise<void>}
*/
async function playAscii() {
if (interval) return;
stopFlag = false;
frameIndex = 1;
await preloadFrames();
let firstFrame = true;
interval = setInterval(async () => {
if (stopFlag || frameIndex > totalFrames) {
clearInterval(interval);
interval = null;
setTimeout(() => {
console.log(
"\n\n" +
"【Touhou】Bad Apple!! Gameboy 8-bit ver. by 檜風呂\n" +
"https://www.nicovideo.jp/watch/sm8954478\n"
);
}, 1000);
return;
}
const frame = await getFrame(frameIndex);
if (frame) {
// Delay audio by 0.5 s after frame 1
if (firstFrame) {
firstFrame = false;
requestAnimationFrame(() => {
if (window.playEggAudio)
setTimeout(() => window.playEggAudio(), 700);
});
}
console.log(
"\n".repeat(25) + indent + frame.replace(/\n/g, "\n" + indent)
);
}
frameIndex++;
}, 1000 / fps);
}
// ==========================================================
// INTRO + PUBLIC API
// ==========================================================
/**
* Print the ASCII art intro banner to the console.
*/
function intro() {
console.log(
"\n" +
`
_____________________________________________________________________/\\\\\\_______________________________
____________________________________________________________________\\/\\___/\\\\\\_______________________________
______________________/\\___/\\\\\\__________________________________________\\/\\___\\/\\______________/\\\\\\_______________________
____/\\\\\\_________/\\\\\\____/\\\\\\___\\////______/\\\\\\_________\\/\\\\\\____/\\\\\\____/\\\\\\_______________\\/\\______
__/\\\\\\///\\\\\\____/\\\\\\///\\\\\\___/\\\\\\_________\\/\\\\\\___/\\\\\\_________\\/\\\\\\______/\\\\\\///___________\\/\\_______
_\\/\\\\\\__\\///\\\\\\__\\/\\\\\\__\\//\\\\\\___\\/\\\\\\_________\\/\\\\\\__/\\\\\\_________\\/\\\\\\______\\////____________\\/\\______
_\\/\\\\\\____\\/\\\\\\__\\/\\\\\\____\\/\\\\\\__\\/\\\\\\_________\\/\\\\\\_\\/\\\\\\_________\\/\\\\\\___________________________\\/\\______
_\\/\\\\\\____\\/\\\\\\__\\/\\\\\\____\\/\\\\\\__\\/\\\\\\_________\\/\\\\\\__\\/\\\\\\_________\\/\\\\\\__________________________\\/\\______
_\\/\\\\\\____\\/\\\\\\__\\/\\\\\\____\\/\\\\\\__\\/\\\\\\_________\\/\\\\\\___\\/\\\\\\_________\\/\\\\\\__________________________\\/\\______
_\\/\\\\\\____\\/\\\\\\__\\/\\\\\\____\\/\\\\\\__\\/\\\\\\_________\\/\\\\\\____\\/\\\\\\_________\\/\\\\\\__________________________\\/\\______
_\\/\\//\\____\\///___\\///_____\\///___\\/\\//_________\\///_____\\///__________\\///__________________________\\///_______
whether you are a curious explorer or a developer tinkering with my website,
you've found this easter egg! your efforts won't go to waste.
type "makeLove()" and press enter :)
and don't forget to ★ the repo: github.com/Microck/micr.dev
`
);
}
/**
* Trigger the Bad Apple!! ASCII animation with audio.
* Exposed as `window.makeLove` for console access.
*/
function makeLove() {
console.log("\nnot war?");
setTimeout(() => {
playAscii();
}, 500);
}
window.makeLove = makeLove;
intro();
// ==========================================================
// AUDIO HANDLER
// ==========================================================
const eggAudio = new Audio("./assets/run.ogg");
eggAudio.preload = "auto";
eggAudio.volume = 0.25;
/**
* Play the ``run.ogg`` audio track from the start.
* Exposed as `window.playEggAudio`.
*/
window.playEggAudio = function () {
eggAudio.currentTime = 0;
eggAudio.play();
};