-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
223 lines (184 loc) · 7.97 KB
/
Copy pathscript.js
File metadata and controls
223 lines (184 loc) · 7.97 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
document.addEventListener('DOMContentLoaded', () => {
const themeToggle = document.getElementById('theme-toggle');
const voidElement = document.querySelector('.void');
const impossibleShapeCanvas = document.getElementById('impossible-shape');
const particleCanvas = document.getElementById('particle-canvas');
const sandsCanvas = document.getElementById('shifting-sands');
// --- Theme Toggle ---
themeToggle.addEventListener('change', () => {
document.body.classList.toggle('dark-mode');
});
// Check for saved theme preference
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'dark') {
document.body.classList.add('dark-mode');
themeToggle.checked = true; // Ensure toggle reflects saved state.
}
themeToggle.addEventListener('change', () => {
if (themeToggle.checked) {
document.body.classList.add('dark-mode');
localStorage.setItem('theme', 'dark');
} else {
document.body.classList.remove('dark-mode');
localStorage.setItem('theme', 'light');
}
});
// --- Section 2: The Void ---
let voidExpanded = false;
voidElement.addEventListener('click', () => {
voidElement.classList.toggle('expanded');
voidExpanded = !voidExpanded;
});
// --- Section 3: Impossible Shape ---
const impossibleShapeCtx = impossibleShapeCanvas.getContext('2d');
let isDragging = false;
let startX, startY;
function drawImpossibleShape(x, y, size, color) {
impossibleShapeCtx.clearRect(0, 0, impossibleShapeCanvas.width, impossibleShapeCanvas.height);
impossibleShapeCtx.beginPath();
// Adjust coordinates based on the drag offset
const centerX = impossibleShapeCanvas.width / 2 + x;
const centerY = impossibleShapeCanvas.height / 2 + y;
const halfSize = size / 2;
// Define points for an "impossible triangle" (Penrose Triangle)
const p1 = { x: centerX, y: centerY - halfSize * 0.866 }; // sin(60deg) = 0.866
const p2 = { x: centerX - halfSize, y: centerY + halfSize * 0.433 }; // 0.866 / 2
const p3 = { x: centerX + halfSize, y: centerY + halfSize * 0.433 };
// Draw the three "bars" that create the illusion
drawBar(p1, p2, size * 0.2, color);
drawBar(p2, p3, size * 0.2, color);
drawBar(p3, p1, size * 0.2, color);
impossibleShapeCtx.closePath();
}
function drawBar(start, end, width, color) {
impossibleShapeCtx.beginPath();
const angle = Math.atan2(end.y - start.y, end.x - start.x);
const dx = Math.cos(angle) * width / 2;
const dy = Math.sin(angle) * width / 2;
impossibleShapeCtx.moveTo(start.x - dy, start.y + dx);
impossibleShapeCtx.lineTo(start.x + dy, start.y - dx);
impossibleShapeCtx.lineTo(end.x + dy, end.y - dx);
impossibleShapeCtx.lineTo(end.x - dy, end.y + dx);
impossibleShapeCtx.fillStyle = color;
impossibleShapeCtx.fill();
impossibleShapeCtx.closePath();
}
let dragOffsetX = 0;
let dragOffsetY = 0;
impossibleShapeCanvas.addEventListener('mousedown', (e) => {
isDragging = true;
startX = e.clientX - impossibleShapeCanvas.offsetLeft;
startY = e.clientY - impossibleShapeCanvas.offsetTop;
//Calculate offset
dragOffsetX = startX - (impossibleShapeCanvas.width / 2) - dragOffsetX;
dragOffsetY = startY - (impossibleShapeCanvas.height / 2) - dragOffsetY;
impossibleShapeCanvas.style.cursor = 'grabbing';
});
impossibleShapeCanvas.addEventListener('mousemove', (e) => {
if (!isDragging) return;
const mouseX = e.clientX - impossibleShapeCanvas.offsetLeft;
const mouseY = e.clientY - impossibleShapeCanvas.offsetTop;
// Update drag offset.
dragOffsetX = mouseX - impossibleShapeCanvas.width/2 ;
dragOffsetY = mouseY - impossibleShapeCanvas.height/2 ;
const shapeColor = document.body.classList.contains('dark-mode') ? '#eee' : '#333';
drawImpossibleShape(dragOffsetX, dragOffsetY, 70, shapeColor);
});
impossibleShapeCanvas.addEventListener('mouseup', () => {
isDragging = false;
impossibleShapeCanvas.style.cursor = 'grab';
});
impossibleShapeCanvas.addEventListener('mouseleave', () => {
isDragging = false; //Reset
impossibleShapeCanvas.style.cursor = 'grab';
});
// Initial draw
drawImpossibleShape(0, 0, 70, '#333');
// --- Section 4: Chaotic Particles ---
const particleCtx = particleCanvas.getContext('2d');
let particles = [];
class Particle {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = Math.random() * 5 + 1;
this.speedX = Math.random() * 3 - 1.5;
this.speedY = Math.random() * 3 - 1.5;
this.color = document.body.classList.contains('dark-mode') ? `hsl(${Math.random() * 360}, 100%, 70%)` : `hsl(${Math.random() * 360}, 100%, 50%)`; // Brighter in dark mode
}
update() {
this.x += this.speedX;
this.y += this.speedY;
this.size -= 0.1; // Shrink over time
}
draw() {
particleCtx.beginPath();
particleCtx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
particleCtx.fillStyle = this.color;
particleCtx.fill();
particleCtx.closePath();
}
}
function handleParticles() {
for (let i = 0; i < particles.length; i++) {
particles[i].update();
particles[i].draw();
if (particles[i].size <= 0.2) {
particles.splice(i, 1);
i--;
}
}
}
particleCanvas.addEventListener('click', (e) => {
const x = e.clientX - particleCanvas.offsetLeft;
const y = e.clientY - particleCanvas.offsetTop;
for (let i = 0; i < 10; i++) { // Burst of particles
particles.push(new Particle(x, y));
}
});
function animateParticles(){
particleCtx.clearRect(0, 0, particleCanvas.width, particleCanvas.height);
handleParticles();
requestAnimationFrame(animateParticles);
}
animateParticles();
// --- Section 5: Shifting Sands ---
const sandsCtx = sandsCanvas.getContext('2d');
let time = 0;
function drawSands() {
sandsCtx.clearRect(0, 0, sandsCanvas.width, sandsCanvas.height);
const numLayers = 5;
for (let i = 0; i < numLayers; i++) {
const amplitude = 20 + i * 10;
const frequency = 0.01 + i * 0.005;
sandsCtx.beginPath();
sandsCtx.moveTo(0, sandsCanvas.height / 2);
for (let x = 0; x < sandsCanvas.width; x++) {
const y = sandsCanvas.height / 2 + amplitude * Math.sin(frequency * x + time + i);
sandsCtx.lineTo(x, y);
}
// Create a gradient for each layer
const gradient = sandsCtx.createLinearGradient(0, 0, 0, sandsCanvas.height);
if (document.body.classList.contains('dark-mode')) {
// Dark mode gradients
gradient.addColorStop(0, `hsl(${200 + i * 20}, 100%, 50%)`); // Blue hues
gradient.addColorStop(1, `hsl(${240 + i * 10}, 100%, 30%)`); // Darker blue
} else {
// Light mode gradients
gradient.addColorStop(0, `hsl(${i * 40}, 100%, 50%)`); // Different hues
gradient.addColorStop(1, `hsl(${i * 40 + 20}, 100%, 70%)`);// Lighter shade
}
sandsCtx.lineTo(sandsCanvas.width, sandsCanvas.height);
sandsCtx.lineTo(0, sandsCanvas.height);
sandsCtx.fillStyle = gradient;
sandsCtx.fill();
sandsCtx.closePath();
}
time += 0.02;
}
function animateSand(){
drawSands();
requestAnimationFrame(animateSand);
}
animateSand();
});