-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
626 lines (532 loc) · 18.4 KB
/
index.html
File metadata and controls
626 lines (532 loc) · 18.4 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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BIT — TRON</title>
<link rel="icon" type="image/svg+xml" href="favicon.svg">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
background: #000;
color: #00ddff;
font-family: 'Courier New', monospace;
min-height: 100vh;
overflow: hidden;
}
#canvas {
display: block;
position: fixed;
top: 0;
left: 0;
width: 100vw !important;
height: 100vh !important;
z-index: 0;
}
/* ── Top bar: title + status ── */
.top-bar {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 2;
display: flex;
align-items: center;
justify-content: space-between;
padding: 20px 28px;
pointer-events: none;
}
h1 {
font-size: 36px;
letter-spacing: 12px;
color: #00ddff;
pointer-events: auto;
}
#status {
font-size: 32px;
font-weight: bold;
letter-spacing: 10px;
transition: color 0.15s, text-shadow 0.15s;
pointer-events: none;
}
/* ── Bottom panel: input + controls ── */
.bottom-panel {
position: fixed;
bottom: 0;
left: 0;
right: 0;
z-index: 2;
display: flex;
justify-content: center;
padding: 0 24px 28px;
pointer-events: none;
}
.bottom-panel > * {
pointer-events: auto;
}
/* Button row */
.controls-row {
display: flex;
gap: 8px;
align-items: center;
}
.ctrl-btn {
background: transparent;
border: 1px solid #1a3344;
color: #447788;
padding: 8px 16px;
font-family: 'Courier New', monospace;
font-size: 11px;
cursor: pointer;
text-transform: uppercase;
letter-spacing: 2px;
transition: all 0.2s;
text-decoration: none;
}
.ctrl-btn:hover {
border-color: #00ddff;
color: #00ddff;
box-shadow: 0 0 8px rgba(0,221,255,0.2);
}
.ctrl-btn:active {
background: rgba(0,221,255,0.15);
}
.ctrl-btn.accent {
border-color: #332d00;
color: #998800;
}
.ctrl-btn.accent:hover {
border-color: #ffdd00;
color: #ffdd00;
box-shadow: 0 0 8px rgba(255,221,0,0.2);
}
.ctrl-sep {
width: 1px;
height: 16px;
background: #1a2a33;
margin: 0 4px;
}
/* DOM particles */
.particle {
position: fixed;
width: 4px;
height: 4px;
border-radius: 50%;
pointer-events: none;
z-index: 100;
animation: particleFly 0.8s ease-out forwards;
}
@keyframes particleFly {
0% { opacity: 1; transform: translate(0, 0) scale(1); }
100% { opacity: 0; transform: translate(var(--dx), var(--dy)) scale(0); }
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div class="top-bar">
<h1>BIT</h1>
<div id="status"> </div>
</div>
<div class="bottom-panel">
<div class="controls-row">
<button class="ctrl-btn" id="btnAsk">Yes</button>
<button class="ctrl-btn" id="btnChallenge">No</button>
<button class="ctrl-btn" id="btnExcite">Excite</button>
<div class="ctrl-sep"></div>
<a href="math.html" class="ctrl-btn accent">Math</a>
</div>
</div>
<!-- Audio -->
<audio id="audioYes" preload="auto" src="audio/bit-yes.mp3"></audio>
<audio id="audioNo" preload="auto" src="audio/bit-no.mp3"></audio>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
(function () {
'use strict';
// ── Constants ──────────────────────────────────────────────
var PHI = (1 + Math.sqrt(5)) / 2;
var STATE_NEUTRAL = 'neutral';
var STATE_YES = 'yes';
var STATE_NO = 'no';
var COLORS = {
neutral: { color: 0x00ddff, emissive: 0x003366 },
yes: { color: 0xffdd00, emissive: 0x554400 },
no: { color: 0xff3300, emissive: 0x551100 }
};
var ROTATION_SPEEDS = { neutral: 0.008, yes: 0.025, no: 0.015 };
var RESPONSE_DURATION = 1500;
var EXCITE_INTERVAL = 440;
var EXCITE_COUNT = 5;
// ── DOM refs ───────────────────────────────────────────────
var canvas = document.getElementById('canvas');
var statusEl = document.getElementById('status');
var audioYes = document.getElementById('audioYes');
var audioNo = document.getElementById('audioNo');
// ── Audio (lazy AudioContext) ──────────────────────────────
var audioCtx = null;
function ensureAudioContext() {
if (!audioCtx) {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
}
if (audioCtx.state === 'suspended') {
audioCtx.resume();
}
}
function playSound(type, excited) {
var el = (type === STATE_YES) ? audioYes : audioNo;
var rate = excited ? 1.33 : 1.0;
try {
el.currentTime = 0;
el.playbackRate = rate;
var p = el.play();
if (p && p.catch) p.catch(function () { fallbackOscillator(type, excited); });
} catch (_) {
fallbackOscillator(type, excited);
}
}
function fallbackOscillator(type, excited) {
ensureAudioContext();
if (!audioCtx) return;
var osc = audioCtx.createOscillator();
var gain = audioCtx.createGain();
osc.connect(gain);
gain.connect(audioCtx.destination);
osc.type = 'square';
var baseFreq = (type === STATE_YES) ? 880 : 220;
osc.frequency.value = excited ? baseFreq * 1.33 : baseFreq;
gain.gain.setValueAtTime(0.15, audioCtx.currentTime);
gain.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + 0.3);
osc.start();
osc.stop(audioCtx.currentTime + 0.35);
}
// ── Three.js setup ─────────────────────────────────────────
var renderer = new THREE.WebGLRenderer({ canvas: canvas, antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
var scene = new THREE.Scene();
scene.background = new THREE.Color(0x000000);
var camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100);
camera.position.set(0, 0, 7);
window.addEventListener('resize', function () {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Lighting
scene.add(new THREE.AmbientLight(0x404040, 0.4));
var mainLight = new THREE.DirectionalLight(0xffffff, 1.0);
mainLight.position.set(2, 3, 2);
scene.add(mainLight);
var fillLight = new THREE.DirectionalLight(0x4488cc, 0.3);
fillLight.position.set(-2, -1, -2);
scene.add(fillLight);
var rimLight = new THREE.PointLight(0x00ddff, 0.5, 20);
rimLight.position.set(0, 2, 4);
scene.add(rimLight);
// ── Geometry helpers ───────────────────────────────────────
function pushTri(arr, a, b, c) {
arr.push(a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y, c.z);
}
function buildBufferGeo(positions) {
var geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(new Float32Array(positions), 3));
geo.computeVertexNormals();
return geo;
}
// ── Stellated icosahedron (Neutral) ────────────────────────
function createStellatedIcosahedron(spikeHeight) {
var base = new THREE.IcosahedronGeometry(1, 0);
var posAttr = base.getAttribute('position');
var indexAttr = base.getIndex();
// Extract unique vertices
var verts = [];
var seen = {};
var indexMap = [];
for (var i = 0; i < posAttr.count; i++) {
var x = posAttr.getX(i), y = posAttr.getY(i), z = posAttr.getZ(i);
var key = x.toFixed(5) + ',' + y.toFixed(5) + ',' + z.toFixed(5);
if (seen[key] === undefined) {
seen[key] = verts.length;
verts.push(new THREE.Vector3(x, y, z));
}
indexMap.push(seen[key]);
}
// Build face list
var faces = [];
if (indexAttr) {
for (var i = 0; i < indexAttr.count; i += 3) {
faces.push([
indexMap[indexAttr.getX(i)],
indexMap[indexAttr.getX(i + 1)],
indexMap[indexAttr.getX(i + 2)]
]);
}
} else {
for (var i = 0; i < posAttr.count; i += 3) {
faces.push([indexMap[i], indexMap[i + 1], indexMap[i + 2]]);
}
}
var positions = [];
var v1 = new THREE.Vector3(), v2 = new THREE.Vector3(), v3 = new THREE.Vector3();
var center = new THREE.Vector3(), normal = new THREE.Vector3();
var edge1 = new THREE.Vector3(), edge2 = new THREE.Vector3();
for (var i = 0; i < faces.length; i++) {
v1.copy(verts[faces[i][0]]);
v2.copy(verts[faces[i][1]]);
v3.copy(verts[faces[i][2]]);
center.set(0, 0, 0).add(v1).add(v2).add(v3).divideScalar(3);
edge1.subVectors(v2, v1);
edge2.subVectors(v3, v1);
normal.crossVectors(edge1, edge2).normalize();
// Ensure outward-pointing
if (normal.dot(center) < 0) normal.negate();
var apex = center.clone().addScaledVector(normal, spikeHeight);
pushTri(positions, v1, v2, apex);
pushTri(positions, v2, v3, apex);
pushTri(positions, v3, v1, apex);
}
base.dispose();
return buildBufferGeo(positions);
}
// ── Stellated dodecahedron (NO) ────────────────────────────
// Seeded random for deterministic spike variation per face
function seededRandom(seed) {
var x = Math.sin(seed * 127.1 + seed * 311.7) * 43758.5453;
return x - Math.floor(x);
}
function createStellatedDodecahedron(baseSpikeHeight) {
var base = new THREE.DodecahedronGeometry(1, 0);
var posAttr = base.getAttribute('position');
var indexAttr = base.getIndex();
// Collect all triangles from Three.js triangulation
var triangles = [];
if (indexAttr) {
for (var i = 0; i < indexAttr.count; i += 3) {
var a = indexAttr.getX(i), b = indexAttr.getX(i + 1), c = indexAttr.getX(i + 2);
triangles.push([
new THREE.Vector3(posAttr.getX(a), posAttr.getY(a), posAttr.getZ(a)),
new THREE.Vector3(posAttr.getX(b), posAttr.getY(b), posAttr.getZ(b)),
new THREE.Vector3(posAttr.getX(c), posAttr.getY(c), posAttr.getZ(c))
]);
}
} else {
for (var i = 0; i < posAttr.count; i += 3) {
triangles.push([
new THREE.Vector3(posAttr.getX(i), posAttr.getY(i), posAttr.getZ(i)),
new THREE.Vector3(posAttr.getX(i + 1), posAttr.getY(i + 1), posAttr.getZ(i + 1)),
new THREE.Vector3(posAttr.getX(i + 2), posAttr.getY(i + 2), posAttr.getZ(i + 2))
]);
}
}
// Group triangles by pentagonal face (same face normal + proximity)
var assigned = [];
for (var i = 0; i < triangles.length; i++) assigned.push(false);
var faceGroups = [];
for (var i = 0; i < triangles.length; i++) {
if (assigned[i]) continue;
var tri = triangles[i];
var n = computeTriNormal(tri[0], tri[1], tri[2]);
var c = computeTriCenter(tri[0], tri[1], tri[2]);
var group = [i];
assigned[i] = true;
for (var j = i + 1; j < triangles.length; j++) {
if (assigned[j]) continue;
var n2 = computeTriNormal(triangles[j][0], triangles[j][1], triangles[j][2]);
if (Math.abs(n.dot(n2) - 1) < 0.05) {
var c2 = computeTriCenter(triangles[j][0], triangles[j][1], triangles[j][2]);
if (c.distanceTo(c2) < 1.2) {
group.push(j);
assigned[j] = true;
}
}
}
faceGroups.push(group);
}
// Stellate each pentagonal face group with varying spike heights
var positions = [];
for (var g = 0; g < faceGroups.length; g++) {
var groupIndices = faceGroups[g];
// Compute pentagon center
var faceCenter = new THREE.Vector3();
var vertCount = 0;
for (var t = 0; t < groupIndices.length; t++) {
var tri = triangles[groupIndices[t]];
faceCenter.add(tri[0]).add(tri[1]).add(tri[2]);
vertCount += 3;
}
faceCenter.divideScalar(vertCount);
// Face normal from first triangle, pointing outward
var firstTri = triangles[groupIndices[0]];
var faceNormal = computeTriNormal(firstTri[0], firstTri[1], firstTri[2]);
if (faceNormal.dot(faceCenter) < 0) faceNormal.negate();
// Vary spike height per face: some short, some very long
// Range from 0.8x to 1.8x of base height for aggressive variation
var variation = seededRandom(g * 7 + 3);
var spikeHeight = baseSpikeHeight * (0.8 + variation * 1.0);
var apex = faceCenter.clone().addScaledVector(faceNormal, spikeHeight);
// Build spike triangles — narrow, sharp spikes
for (var t = 0; t < groupIndices.length; t++) {
var tri = triangles[groupIndices[t]];
pushTri(positions, tri[0], tri[1], apex);
pushTri(positions, tri[1], tri[2], apex);
pushTri(positions, tri[2], tri[0], apex);
}
}
base.dispose();
return buildBufferGeo(positions);
}
function computeTriNormal(a, b, c) {
var e1 = new THREE.Vector3().subVectors(b, a);
var e2 = new THREE.Vector3().subVectors(c, a);
return new THREE.Vector3().crossVectors(e1, e2).normalize();
}
function computeTriCenter(a, b, c) {
return new THREE.Vector3().add(a).add(b).add(c).divideScalar(3);
}
// ── State management ───────────────────────────────────────
var currentState = STATE_NEUTRAL;
var currentMesh = null;
var responseTimer = null;
var isExcited = false;
function createMaterial(state) {
var c = COLORS[state];
return new THREE.MeshPhongMaterial({
color: c.color,
emissive: c.emissive,
flatShading: true,
transparent: true,
opacity: 0.92,
shininess: 80,
side: THREE.DoubleSide
});
}
function createGeometry(state) {
switch (state) {
case STATE_YES: return new THREE.OctahedronGeometry(1.2, 0);
case STATE_NO: return createStellatedDodecahedron(1.8);
default: return createStellatedIcosahedron(0.3);
}
}
function setMesh(state) {
if (currentMesh) {
currentMesh.geometry.dispose();
currentMesh.material.dispose();
scene.remove(currentMesh);
}
var geo = createGeometry(state);
var mat = createMaterial(state);
currentMesh = new THREE.Mesh(geo, mat);
scene.add(currentMesh);
currentState = state;
}
function updateStatus(state) {
if (state === STATE_YES) {
statusEl.textContent = 'YES!';
statusEl.style.color = '#ffdd00';
} else if (state === STATE_NO) {
statusEl.textContent = 'NO!';
statusEl.style.color = '#ff3300';
} else {
statusEl.innerHTML = ' ';
statusEl.style.color = '#00ddff';
}
}
// ── Particles ──────────────────────────────────────────────
function spawnParticles(state) {
var rect = canvas.getBoundingClientRect();
var cx = rect.left + rect.width / 2;
var cy = rect.top + rect.height / 2;
var color = (state === STATE_YES) ? '#ffdd00' : '#ff3300';
for (var i = 0; i < 16; i++) {
var el = document.createElement('div');
el.className = 'particle';
var angle = (Math.PI * 2 * i) / 16 + (Math.random() - 0.5) * 0.4;
var dist = 60 + Math.random() * 80;
el.style.left = cx + 'px';
el.style.top = cy + 'px';
el.style.background = color;
el.style.boxShadow = '0 0 6px ' + color;
el.style.setProperty('--dx', (Math.cos(angle) * dist) + 'px');
el.style.setProperty('--dy', (Math.sin(angle) * dist) + 'px');
document.body.appendChild(el);
el.addEventListener('animationend', function () { this.remove(); });
}
}
// ── Response logic ─────────────────────────────────────────
function respond(type, excited) {
if (responseTimer) {
clearTimeout(responseTimer);
responseTimer = null;
}
ensureAudioContext();
var state = (type === 'random')
? (Math.random() < 0.5 ? STATE_YES : STATE_NO)
: type;
setMesh(state);
updateStatus(state);
playSound(state, excited);
spawnParticles(state);
responseTimer = setTimeout(function () {
setMesh(STATE_NEUTRAL);
updateStatus(STATE_NEUTRAL);
responseTimer = null;
}, RESPONSE_DURATION);
}
function excite() {
if (isExcited) return;
isExcited = true;
var count = 0;
function next() {
count++;
respond('random', true);
if (count < EXCITE_COUNT) {
setTimeout(next, EXCITE_INTERVAL);
} else {
isExcited = false;
}
}
next();
}
// ── Controls ───────────────────────────────────────────────
canvas.addEventListener('click', function () { respond('random', false); });
document.addEventListener('keydown', function (e) {
if (e.repeat) return;
switch (e.key.toLowerCase()) {
case 'y': respond(STATE_YES, false); break;
case 'n': respond(STATE_NO, false); break;
case ' ':
e.preventDefault();
respond('random', false);
break;
case 'e': excite(); break;
}
});
document.getElementById('btnAsk').addEventListener('click', function () {
respond(STATE_YES, false);
});
document.getElementById('btnChallenge').addEventListener('click', function () {
respond(STATE_NO, false);
});
document.getElementById('btnExcite').addEventListener('click', function () {
excite();
});
// ── Animation loop ─────────────────────────────────────────
var clock = new THREE.Clock();
setMesh(STATE_NEUTRAL);
function animate() {
requestAnimationFrame(animate);
var t = clock.getElapsedTime();
var speed = ROTATION_SPEEDS[currentState] || ROTATION_SPEEDS.neutral;
if (currentMesh) {
currentMesh.rotation.y += speed;
currentMesh.rotation.x += speed * 0.4;
currentMesh.position.y = Math.sin(t * 1.5) * 0.15;
}
renderer.render(scene, camera);
}
animate();
})();
</script>
</body>
</html>