-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
146 lines (124 loc) · 4.35 KB
/
script.js
File metadata and controls
146 lines (124 loc) · 4.35 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
document.addEventListener('DOMContentLoaded', () => {
const toggle = document.querySelector('.menu-toggle');
const nav = document.querySelector('header nav');
if (toggle && nav) {
toggle.addEventListener('click', () => {
const open = nav.classList.toggle('open');
toggle.setAttribute('aria-expanded', open);
});
}
// highlight active link
const links = document.querySelectorAll('nav a');
const path = location.pathname.split('/').pop() || 'index.html';
links.forEach(a => {
const href = a.getAttribute('href');
if (href === path || (href === 'index.html' && path === '')) {
a.classList.add('active');
}
// small keyboard focus affordance
a.addEventListener('focus', () => a.classList.add('focus'));
a.addEventListener('blur', () => a.classList.remove('focus'));
});
});
// thanks ryan for the fly stuff
let animationFrames = [];
const MAX_BIRDS = 10;
const birds = [];
function createAndMoveImage() {
if (birds.length >= MAX_BIRDS) {
showLimitMessage();
return;
}
const image = document.createElement('img');
image.src = 'assets/bird.webp';
image.alt = 'Flying Picture';
image.style.position = 'absolute';
const size = 50 + Math.random() * 70;
image.style.width = size + 'px';
image.style.height = size + 'px';
image.style.left = '0px';
image.style.top = '0px';
image.style.zIndex = 1000;
image.style.pointerEvents = 'auto';
image.style.transition = 'transform 0.12s linear';
document.body.appendChild(image);
const bird = {
el: image,
x: Math.random() * (window.innerWidth - size),
y: Math.random() * (window.innerHeight - size),
dx: (Math.random() * 3 + 1) * (Math.random() < 0.5 ? 1 : -1),
dy: (Math.random() * 3 + 1) * (Math.random() < 0.5 ? 1 : -1),
size,
frameId: null,
tOffset: Math.random() * 1000
};
birds.push(bird);
image.addEventListener('click', () => removeBird(bird));
function moveImage(timestamp) {
bird.x += bird.dx;
bird.y += bird.dy;
if (bird.x + bird.size >= window.innerWidth || bird.x <= 0) {
bird.dx = -bird.dx;
}
if (bird.y + bird.size >= window.innerHeight || bird.y <= 0) {
bird.dy = -bird.dy;
}
bird.el.style.left = bird.x + 'px';
bird.el.style.top = bird.y + 'px';
const angle = Math.atan2(bird.dy, bird.dx) * 180 / Math.PI;
const scale = 1 + 0.08 * Math.sin((timestamp + bird.tOffset) / 100);
bird.el.style.transform = `rotate(${angle}deg) scale(${scale})`;
bird.frameId = requestAnimationFrame(moveImage);
animationFrames.push(bird.frameId);
}
bird.frameId = requestAnimationFrame(moveImage);
}
function removeBird(bird) {
if (!bird) return;
cancelAnimationFrame(bird.frameId);
const idx = birds.indexOf(bird);
if (idx !== -1) birds.splice(idx, 1);
if (bird.el && bird.el.parentNode) bird.el.parentNode.removeChild(bird.el);
}
function showLimitMessage() {
if (document.getElementById('bird-limit-msg')) return;
const msg = document.createElement('div');
msg.id = 'bird-limit-msg';
msg.textContent = `Max birds (${MAX_BIRDS})`;
Object.assign(msg.style, {
position: 'fixed',
left: '50%',
top: '20px',
transform: 'translateX(-50%)',
background: 'rgba(0,0,0,0.75)',
color: '#fff',
padding: '8px 12px',
borderRadius: '6px',
zIndex: 9999
});
document.body.appendChild(msg);
setTimeout(() => {
msg.style.transition = 'opacity 400ms';
msg.style.opacity = '0';
setTimeout(() => msg.remove(), 500);
}, 900);
}
document.addEventListener('keydown', (event) => {
if (event.code === 'Space') {
event.preventDefault();
createAndMoveImage();
}
if (event.code === 'Backspace') {
birds.slice().forEach(removeBird);
}
});
window.addEventListener('beforeunload', () => {
animationFrames.forEach(id => cancelAnimationFrame(id));
});
const inputField = document.getElementById('magicBox');
inputField.addEventListener('input', (e) => {
const value = e.target.value.toLowerCase();
if (value === 'lore') {
window.location.href = 'https://asa-db.github.io/lore.txt';
}
});