-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab.js
More file actions
194 lines (176 loc) · 7.06 KB
/
Copy pathlab.js
File metadata and controls
194 lines (176 loc) · 7.06 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
// ========== LAB PAGE ==========
// Reads all images from window.projects.lab and renders them as "workshop"
// cards — subtle random tilts, tape flourishes, paper shadows. Clicks open
// the shared lightbox (overlay.js) scoped to just the lab items.
(function initLab() {
const grid = document.getElementById('labGrid');
const countEl = document.getElementById('labCount');
const yearsEl = document.getElementById('labYears');
const barCounter = document.getElementById('barCounter');
if (!grid) return;
const projects = window.projects || {};
const lab = projects.lab;
if (!lab || !Array.isArray(lab.images)) {
grid.innerHTML = '<p style="text-align:center;color:#888;">No experiments yet — check back soon.</p>';
return;
}
// Deterministic-ish "random" so layout is stable across reloads in a session
// but still feels organic — seeded by image index.
function pseudoRandom(seed) {
const x = Math.sin(seed * 9999) * 10000;
return x - Math.floor(x);
}
// Filter out screenshot/mockup clutter (same rule the main grid uses).
const excludeRe = window.gridExclude || /screenshot|zbrush|Mockup/i;
const items = lab.images.filter(src => !excludeRe.test(src));
// Shuffle lightly — pseudo-random sort so adjacent images aren't always the
// same project variant, but stable per session.
const ordered = items.map((src, i) => ({ src, key: pseudoRandom(i + 1) }))
.sort((a, b) => a.key - b.key)
.map(x => x.src);
// lightbox expects raw URL strings (not {src} objects)
const labLightboxItems = ordered;
const videoRe = /\.(webm|mp4|mov|m4v)$/i;
function isVideo(src) { return videoRe.test(src); }
function basename(src) {
const file = src.split('/').pop() || '';
return file.replace(/\.[^.]+$/, '').replace(/[_-]/g, ' ');
}
// Luminance histogram — draws the media to a 48x48 canvas, bins pixels by
// brightness, returns a compact SVG bar graph. Same-origin assets only
// (local files), so canvas.getImageData is safe.
const HIST_BINS = 24;
const HIST_W = 72;
const HIST_H = 18;
function buildHistogramSvg(mediaEl) {
try {
const c = document.createElement('canvas');
c.width = 48; c.height = 48;
const ctx = c.getContext('2d', { willReadFrequently: true });
ctx.drawImage(mediaEl, 0, 0, 48, 48);
const data = ctx.getImageData(0, 0, 48, 48).data;
const hist = new Array(HIST_BINS).fill(0);
for (let i = 0; i < data.length; i += 4) {
const lum = 0.2126 * data[i] + 0.7152 * data[i + 1] + 0.0722 * data[i + 2];
const bin = Math.min(HIST_BINS - 1, Math.floor((lum / 256) * HIST_BINS));
hist[bin]++;
}
const max = Math.max.apply(null, hist) || 1;
const barW = HIST_W / HIST_BINS;
const bars = hist.map((v, i) => {
const bh = Math.max(1, (v / max) * HIST_H);
const x = (i * barW).toFixed(2);
const y = (HIST_H - bh).toFixed(2);
return `<rect x="${x}" y="${y}" width="${(barW - 0.5).toFixed(2)}" height="${bh.toFixed(2)}" fill="currentColor"/>`;
}).join('');
return `<svg width="${HIST_W}" height="${HIST_H}" viewBox="0 0 ${HIST_W} ${HIST_H}" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">${bars}</svg>`;
} catch (e) {
return null;
}
}
const frag = document.createDocumentFragment();
ordered.forEach((src, idx) => {
const card = document.createElement('button');
card.type = 'button';
card.className = 'lab-card';
card.dataset.idx = String(idx);
const mediaWrap = document.createElement('div');
mediaWrap.className = 'media-wrap';
let media;
const vid = isVideo(src);
if (vid) {
media = document.createElement('video');
media.src = src;
media.muted = true;
media.loop = true;
media.playsInline = true;
media.preload = 'metadata';
card.addEventListener('mouseenter', () => { media.play().catch(() => {}); });
card.addEventListener('mouseleave', () => { media.pause(); media.currentTime = 0; });
} else {
media = document.createElement('img');
media.src = src;
media.loading = 'lazy';
media.decoding = 'async';
media.alt = '';
}
mediaWrap.appendChild(media);
card.appendChild(mediaWrap);
// HUD overlay — appears on hover
const hud = document.createElement('div');
hud.className = 'hud';
const hudTop = document.createElement('div');
hudTop.className = 'hud-top';
const hudIdx = document.createElement('span');
hudIdx.className = 'hud-idx';
hudIdx.textContent = String(idx + 1).padStart(3, '0');
const hudType = document.createElement('span');
hudType.className = 'hud-type';
hudType.textContent = vid ? 'VIDEO' : 'STILL';
hudTop.appendChild(hudIdx);
hudTop.appendChild(hudType);
const hudBottom = document.createElement('div');
hudBottom.className = 'hud-bottom';
const hudName = document.createElement('span');
hudName.className = 'hud-name';
hudName.textContent = basename(src);
const hudHist = document.createElement('span');
hudHist.className = 'hud-hist is-loading';
hudBottom.appendChild(hudName);
hudBottom.appendChild(hudHist);
hud.appendChild(hudTop);
hud.appendChild(hudBottom);
card.appendChild(hud);
// Histogram — computed on first hover, cached after.
let histReady = false;
const ensureHistogram = () => {
if (histReady) return;
// Video needs at least metadata loaded to draw a frame
if (vid && media.readyState < 2) {
media.addEventListener('loadeddata', ensureHistogram, { once: true });
return;
}
const svg = buildHistogramSvg(media);
if (svg) {
hudHist.classList.remove('is-loading');
hudHist.innerHTML = svg;
histReady = true;
}
};
card.addEventListener('mouseenter', ensureHistogram);
card.addEventListener('focus', ensureHistogram);
card.addEventListener('click', () => {
if (typeof window.setLightboxItems === 'function') {
window.setLightboxItems(labLightboxItems, true);
}
if (typeof window.openLightbox === 'function') {
window.openLightbox(idx);
}
});
frag.appendChild(card);
});
grid.appendChild(frag);
// Stats
if (countEl) countEl.textContent = String(ordered.length);
if (yearsEl) yearsEl.textContent = '14+';
if (barCounter) barCounter.textContent = `${ordered.length}`;
// Fade-in on scroll
const revealObs = new IntersectionObserver((entries) => {
for (const e of entries) {
if (e.isIntersecting) {
e.target.classList.add('in-view');
revealObs.unobserve(e.target);
}
}
}, { threshold: 0.1, rootMargin: '0px 0px -5% 0px' });
grid.querySelectorAll('.lab-card').forEach(c => revealObs.observe(c));
// Footer reveal (same pattern as other pages)
const footer = document.querySelector('.site-footer');
if (footer) {
const fobs = new IntersectionObserver((entries) => {
const visible = entries.some(e => e.isIntersecting);
document.body.classList.toggle('footer-visible', visible);
}, { threshold: 0.05 });
fobs.observe(footer);
}
})();