-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreview-cards.js
274 lines (231 loc) · 8.73 KB
/
preview-cards.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
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
// Preview cards in the projects section
// Debug flag - set to true to show console logs
const DEBUG = false;
// Basic variables
let mouseX = 0;
let mouseY = 0;
let activeProject = null;
let isTracking = false;
// Get DOM elements
const preview = document.querySelector('.preview-window');
const previewImg = document.getElementById('preview-image');
// Function to log debug messages
function log(message) {
if (DEBUG) console.log(`[Preview] ${message}`);
}
// Create a new standalone preview element
function createStandalonePreview() {
// Remove any existing preview first
const existingPreview = document.getElementById('standalone-preview');
if (existingPreview) existingPreview.remove();
// Create new preview container
const container = document.createElement('div');
container.id = 'standalone-preview';
container.style.cssText = `
position: fixed;
z-index: 9999;
pointer-events: none;
top: 0;
left: 0;
transform: translate(-50%, -50%);
transition: opacity 0.2s ease;
opacity: 0;
display: none;
`;
// Create image element
const img = document.createElement('img');
img.style.cssText = `
max-width: 250px;
border-radius: 10px;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
`;
// Add to DOM
container.appendChild(img);
document.body.appendChild(container);
log('Created standalone preview element');
return { container, img };
}
// Initialize the preview system
function initPreviewSystem() {
log('Initializing preview system');
// Create our custom preview element
const { container: previewEl, img: imgEl } = createStandalonePreview();
// Track mouse movement globally
document.addEventListener('mousemove', (e) => {
mouseX = e.clientX;
mouseY = e.clientY;
// If we're actively tracking, update the preview position immediately
if (isTracking) {
updatePreviewPosition(previewEl);
// Check if mouse is near any project links
if (activeProject) {
const isNearLinks = checkIfNearLinks(activeProject, mouseX, mouseY);
if (isNearLinks) {
previewEl.style.opacity = '0';
document.body.classList.remove('hide-cursor');
} else {
previewEl.style.opacity = '1';
document.body.classList.add('hide-cursor');
}
}
}
});
// Handle project hover
const projects = document.querySelectorAll('.project');
projects.forEach(project => {
// Mouse enter a project
project.addEventListener('mouseenter', (e) => {
activateProjectPreview(project, previewEl, imgEl);
});
// Mouse leave a project
project.addEventListener('mouseleave', () => {
log('Left project');
previewEl.style.opacity = '0';
setTimeout(() => {
if (!isTracking) previewEl.style.display = 'none';
}, 200);
isTracking = false;
activeProject = null;
// Show cursor
document.body.classList.remove('hide-cursor');
});
});
// Setup intersection observer to handle scrolling into projects
setupIntersectionObserver(previewEl, imgEl);
// Handle scroll events
window.addEventListener('scroll', () => {
if (!isTracking) return;
log('Scroll detected');
// Update position immediately during scroll
updatePreviewPosition(previewEl);
// Check what's under the cursor now
const elementAtPoint = document.elementFromPoint(mouseX, mouseY);
if (!elementAtPoint) {
hidePreview(previewEl);
return;
}
// Find if we're over a project
let currentProject = elementAtPoint;
while (currentProject && !currentProject.classList.contains('project')) {
currentProject = currentProject.parentElement;
}
// If we found a project and it's different from our active one
if (currentProject && currentProject !== activeProject) {
log(`Scrolled to new project: ${currentProject.querySelector('h3')?.textContent}`);
// Update the preview image
const newPreviewImg = currentProject.getAttribute('data-preview');
if (newPreviewImg) {
imgEl.src = newPreviewImg;
activeProject = currentProject;
}
} else if (!currentProject) {
// Not over a project anymore
hidePreview(previewEl);
}
});
// Function to update preview position
function updatePreviewPosition(element) {
element.style.transform = 'translate(-50%, -50%)';
element.style.left = `${mouseX}px`;
element.style.top = `${mouseY}px`;
}
// Function to hide preview
function hidePreview(element) {
log('Hiding preview');
element.style.opacity = '0';
setTimeout(() => {
if (!isTracking) element.style.display = 'none';
}, 200);
isTracking = false;
activeProject = null;
document.body.classList.remove('hide-cursor');
}
log('Preview system initialized');
}
// Helper function to activate preview for a project
function activateProjectPreview(project, previewEl, imgEl) {
const previewImg = project.getAttribute('data-preview');
if (!previewImg) return;
log(`Activating project: ${project.querySelector('h3')?.textContent}`);
activeProject = project;
imgEl.src = previewImg;
imgEl.onload = () => {
previewEl.style.opacity = '1';
previewEl.style.display = 'block';
isTracking = true;
// Update position
previewEl.style.transform = 'translate(-50%, -50%)';
previewEl.style.left = `${mouseX}px`;
previewEl.style.top = `${mouseY}px`;
// Check if near links before hiding cursor
const isNearLinks = checkIfNearLinks(project, mouseX, mouseY);
if (!isNearLinks) {
document.body.classList.add('hide-cursor');
}
};
}
// Set up intersection observer to detect when projects enter viewport
function setupIntersectionObserver(previewEl, imgEl) {
// Create the observer
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
// If a project is entering the viewport
if (entry.isIntersecting) {
const project = entry.target;
// Check if the mouse cursor is over this project
const rect = project.getBoundingClientRect();
if (
mouseX >= rect.left &&
mouseX <= rect.right &&
mouseY >= rect.top &&
mouseY <= rect.bottom
) {
log('Project scrolled into cursor position');
// If we're not already tracking another project
if (!isTracking) {
activateProjectPreview(project, previewEl, imgEl);
}
}
}
});
}, {
// Only trigger when at least 30% of the project is visible
threshold: 0.3
});
// Observe all project elements
document.querySelectorAll('.project').forEach(project => {
observer.observe(project);
});
}
// Function to check if cursor is near project links
function checkIfNearLinks(projectElement, x, y) {
// Find all link elements within the project-links container
const linksContainer = projectElement.querySelector('.project-links');
if (!linksContainer) return false;
// Get bounding rect of the links container
const rect = linksContainer.getBoundingClientRect();
// Define a larger detection area around the links (100px buffer)
const buffer = 100;
const extendedRect = {
left: rect.left - buffer,
right: rect.right + buffer,
top: rect.top - buffer,
bottom: rect.bottom + buffer
};
// Check if cursor is within the extended area
const isNear =
x >= extendedRect.left &&
x <= extendedRect.right &&
y >= extendedRect.top &&
y <= extendedRect.bottom;
if (isNear && isTracking) {
log('Cursor near links - hiding preview');
}
return isNear;
}
// Initialize when DOM is loaded
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initPreviewSystem);
} else {
initPreviewSystem();
}