-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
152 lines (126 loc) · 5.98 KB
/
scripts.js
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
document.addEventListener('DOMContentLoaded', () => {
const layer1 = document.getElementById('layer1');
const layer2 = document.getElementById('layer2');
const modeSwitch = document.getElementById('modeSwitch');
const maxParticles = 50;
const minSize = 15;
const maxSize = 25;
const particles = [];
const savedMode = localStorage.getItem('nightMode');
if (savedMode === 'true') {
document.body.classList.add('night-mode');
modeSwitch.checked = true;
}
modeSwitch.addEventListener('change', () => {
const isChecked = modeSwitch.checked;
document.body.classList.toggle('night-mode', isChecked);
localStorage.setItem('nightMode', isChecked);
updateParticleImages();
updateFavicon();
});
function createParticle(layer) {
const particle = document.createElement('div');
particle.classList.add('particle');
const size = Math.random() * (maxSize - minSize) + minSize;
particle.style.width = `${size}px`;
particle.style.height = `${size}px`;
particle.style.left = `${Math.random() * 100}%`;
const duration = Math.random() * 8 + 5;
const delay = Math.random() * 5;
particle.style.animationDuration = `${duration}s`;
particle.style.animationDelay = `${delay}s`;
particles.push(particle);
particle.addEventListener('click', (event) => {
const style = window.getComputedStyle(particle);
const matrix = new WebKitCSSMatrix(style.transform);
const rotation = matrix.a === 1 && matrix.b === 0 ? 0 : Math.atan2(matrix.b, matrix.a) * (180 / Math.PI);
const explosion = document.createElement('div');
explosion.classList.add('explosion');
explosion.style.width = particle.style.width;
explosion.style.height = particle.style.height;
explosion.style.left = particle.style.left;
explosion.style.top = `${event.clientY - size / 2}px`;
explosion.style.transform = `rotate(${rotation}deg)`;
explosion.style.animation = 'explode 1s forwards';
document.body.appendChild(explosion);
explosion.addEventListener('animationend', () => {
document.body.removeChild(explosion);
});
layer.removeChild(particle);
});
layer.appendChild(particle);
particle.addEventListener('animationend', () => {
if (particle.style.animationName !== 'explode') {
layer.removeChild(particle);
createParticle(layer);
}
});
}
function updateParticleImages() {
const isNightMode = document.body.classList.contains('night-mode');
const particles = document.querySelectorAll('.particle');
particles.forEach(particle => {
particle.style.backgroundImage = isNightMode ? "url('images/star.png')" : "url('images/sakura.png')";
});
}
function addParticles() {
const currentParticlesLayer1 = layer1.getElementsByClassName('particle').length;
const currentParticlesLayer2 = layer2.getElementsByClassName('particle').length;
if (currentParticlesLayer1 < maxParticles) {
createParticle(layer1);
}
if (currentParticlesLayer2 < maxParticles) {
createParticle(layer2);
}
requestAnimationFrame(addParticles);
}
function updateFavicon() {
const isNightMode = document.body.classList.contains('night-mode');
let link = document.querySelector("link[rel~='icon']");
if (!link) {
link = document.createElement('link');
link.rel = 'icon';
document.getElementsByTagName('head')[0].appendChild(link);
}
link.href = isNightMode ? 'images/star.png' : 'images/sakura.png';
}
// Function to handle device orientation
function handleOrientation(event) {
const x = event.gamma || 0; // 左右倾斜
const y = event.beta || 0; // 前后倾斜
const xPercentage = x / 45; // 标准化到[-1, 1]
const yPercentage = y / 45; // 标准化到[-1, 1]
const fov = 100 - Math.sqrt(xPercentage * xPercentage + yPercentage * yPercentage) * 50;
document.querySelector('.background').style.perspective = `${fov}vw`;
layer1.style.transform = `translate(${xPercentage * 20}px, ${yPercentage * 20}px)`;
layer2.style.transform = `translate(${xPercentage * -10}px, ${yPercentage * -10}px)`;
const shadowX = xPercentage * 20;
const shadowY = yPercentage * 20;
const particles = document.querySelectorAll('.particle');
particles.forEach(particle => {
particle.style.filter = `drop-shadow(${shadowX}px ${shadowY}px 15px rgba(0, 0, 0, 0.3))`;
});
}
// Add event listener for device orientation
if (window.DeviceOrientationEvent) {
window.addEventListener('deviceorientation', handleOrientation, true);
}
document.addEventListener('mousemove', (e) => {
const x = (e.clientX / window.innerWidth) - 0.5;
const y = (e.clientY / window.innerHeight) - 0.5;
const fov = 100 - Math.sqrt(x * x + y * y) * 50;
document.querySelector('.background').style.perspective = `${fov}vw`;
layer1.style.transform = `translate(${x * 20}px, ${y * 20}px)`;
layer2.style.transform = `translate(${x * -10}px, ${y * -10}px)`;
const shadowX = x * 20;
const shadowY = y * 20;
const particles = document.querySelectorAll('.particle');
particles.forEach(particle => {
particle.style.filter = `drop-shadow(${shadowX}px ${shadowY}px 15px rgba(0, 0, 0, 0.3))`;
});
});
requestAnimationFrame(addParticles);
addParticles();
updateParticleImages();
updateFavicon();
});