-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
518 lines (431 loc) · 17.1 KB
/
index.html
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Photo Gallery</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.8.5/d3.min.js"></script>
<style>
.gallery-container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
height: 100vh;
}
.collection {
margin-bottom: 40px;
position: relative;
}
.collection canvas {
width: 100%;
height: auto;
cursor: pointer;
max-height: none;
}
.description {
margin-top: 10px;
font-family: sans-serif;
}
@media (max-width: 768px) {
.gallery-container {
padding: 10px;
}
}
/* Loading indicator */
.loading-bar {
height: 2px;
background: #777;
transition: width 0.3s ease;
}
</style>
</head>
<body>
<div class="gallery-container">
<div class="graphic">
<!-- Album template -->
<div class="collection" data-slug="album-1" data-size="13">
<!-- Removed the description div -->
</div>
</div>
</div>
<script>
(function() {
const DISTORTION_FACTOR = 6; // Base distortion factor
const FALLOFF_FACTOR = 1.5; // Base falloff factor
let width;
let height;
const graphic = d3.select(".graphic");
// First create canvas elements
let canvas = graphic.selectAll(".collection")
.datum(function() {
return {
slug: this.getAttribute("data-slug"),
size: parseInt(this.getAttribute("data-size"))
};
})
.append("canvas");
// Now we can get the context
const pixelRatio = window.devicePixelRatio || 1;
const storeRatio = (function() {
const context = canvas.node().getContext("2d");
const backingStoreRatio = context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio ||
context.backingStorePixelRatio || 1;
return pixelRatio / backingStoreRatio;
})();
canvas
.attr("height", height * storeRatio)
.style("height", height + "px");
// Add loading indicator
const loadingBar = canvas.append("div")
.attr("class", "loading-bar")
.style("width", "0%");
// Setup annotations
const annotations = canvas.select(".annotations");
annotations.selectAll("li")
.datum(function() {
return {
range: JSON.parse(this.getAttribute("data-range"))
};
});
// Event listeners
d3.select(window)
.on("scroll", scroll)
.on("resize", resize);
resize();
function resize() {
width = parseInt(graphic.style("width"));
height = window.innerHeight - 40;
// Add debug logging
console.log(`Resize called. New width: ${width}, height: ${height}`);
console.log(`Window DPI scaling: ${window.devicePixelRatio}`);
console.log(`Screen width: ${window.screen.width}`);
console.log(`Screen height: ${window.screen.height}`);
canvas
.attr("width", width)
.attr("height", height)
.style("width", width + "px")
.style("height", height + "px")
.each(function(d) {
d.context = this.getContext("2d");
d.context.strokeStyle = "rgba(0,0,0,0.8)";
if (d.enabled) {
console.log('Resizing enabled canvas');
d.resize();
}
});
scroll();
}
function scroll() {
const dy = window.innerHeight;
if (!canvas
.filter(function() {
const box = this.getBoundingClientRect();
return box.bottom > 0 && box.top < dy;
})
.each(enableFisheye)
.empty()) {
canvas = canvas.filter(d => !d.enabled);
}
}
function enableFisheye(d) {
d.enabled = true;
const that = this;
const dpr = window.devicePixelRatio || 1;
const normalWidth = width / d.size;
const imageWidth = Math.ceil(normalWidth * (dpr >= 1.5 ? dpr * 0.8 : dpr * 1.5));
const imageHeight = height;
let desiredDistortion = 0;
let desiredFocus = width / 2;
let progress = 0;
let idle = true;
let touchtime = 0;
let isTouching = false;
let touchEndTimer = null; // Track touch end timer
let lastTouchX = null; // Track last touch position
let hasMoved = false; // Track if touch has moved
// Add temporary link element for handling clicks
const link = document.createElement('a');
link.style.display = 'none';
document.body.appendChild(link);
const x = fisheye()
.distortion(0)
.extent([0, width])
.focus(width / 2);
const annotation = annotations.selectAll("li");
// Load and cache images with loading progress and error handling
const images = new Array(d.size).fill(null);
let loadedImages = 0;
const totalImages = d.size;
function constructImagePath(index) {
const baseUrl = window.location.hostname.includes('github.io')
? `/take-me-away`
: '';
const path = `${baseUrl}/albums/${d.slug}/photo${index + 1}.jpg`;
return path;
}
for (let i = 0; i < totalImages; i++) {
const img = new Image();
const imagePath = constructImagePath(i);
const index = i;
img.onload = () => {
// Create a canvas to process the image
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// For portrait images, crop to center square
if (img.height > img.width) {
const size = img.width; // Use width as the square size
const startY = (img.height - size) / 2; // Center vertically
canvas.width = size;
canvas.height = size;
// Draw only the center square portion
ctx.drawImage(img,
0, startY, size, size, // Source rectangle
0, 0, size, size // Destination rectangle
);
// Create new image from canvas
const squareImg = new Image();
squareImg.onload = () => {
images[index] = squareImg;
loadedImages++;
progress = (loadedImages / totalImages) * 100;
loadingBar.style("width", progress + "%");
if (loadedImages === totalImages) {
loadingBar.remove();
initialize();
}
render();
};
squareImg.src = canvas.toDataURL();
} else {
// For landscape or square images, use as is
images[index] = img;
loadedImages++;
progress = (loadedImages / totalImages) * 100;
loadingBar.style("width", progress + "%");
if (loadedImages === totalImages) {
loadingBar.remove();
initialize();
}
render();
}
};
img.onerror = (e) => {
console.error(`Failed to load image ${index + 1}: ${imagePath}`);
console.error('Error details:', e);
loadedImages++;
};
img.src = imagePath;
}
function initialize() {
d3.select(that)
.on("mousedown.fisheye", mousedown)
.on("mouseover.fisheye", mouseover)
.on("mousemove.fisheye", mousemove)
.on("mouseout.fisheye", mouseout)
.on("touchstart.fisheye", touchstart)
.on("touchmove.fisheye", touchmove)
.on("touchend.fisheye", touchend)
.on("touchcancel.fisheye", touchend); // Add touchcancel handler
render();
}
function render() {
if (this.renderTimeout) return;
this.renderTimeout = setTimeout(() => {
this.renderTimeout = null;
if (!d.context) {
console.error('No context available');
return;
}
const context = d.context;
context.clearRect(0, 0, width * storeRatio, height * storeRatio);
for (let i = 0; i < d.size; i++) {
const x0 = x(i * normalWidth);
const x1 = x((i + 1) * normalWidth);
const slotWidth = x1 - x0;
try {
const img = images[i];
if (!img || !img.complete) continue;
// Calculate scaling to fill height while maintaining aspect ratio
const scale = height / img.height;
const scaledWidth = img.width * scale;
// Calculate how much of the image width to use
const sourceWidth = slotWidth / scale;
const sourceX = (img.width - sourceWidth) / 2;
context.drawImage(
img,
sourceX, 0, sourceWidth, img.height, // Source rectangle
x0, 0, slotWidth, height // Destination rectangle
);
context.beginPath();
context.moveTo(x0, 0);
context.lineTo(x0, height);
context.stroke();
} catch (e) {
console.error(`Error rendering image ${i}:`, e);
}
}
context.strokeRect(0, 0, width, height);
}, 16);
}
function move() {
if (idle) {
idle = false;
if (this.moveTimer) {
this.moveTimer.stop();
}
const startDistortion = x.distortion();
const startFocus = x.focus();
// Changed duration to 200ms
const duration = 200;
this.moveTimer = d3.timer((elapsed) => {
const t = Math.min(1, elapsed / duration);
const progress = t < .5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
x.distortion(startDistortion + (desiredDistortion - startDistortion) * progress);
x.focus(startFocus + (desiredFocus - startFocus) * progress);
render();
if (t >= 1) {
idle = true;
return true;
}
});
}
}
function mouseover(event) {
// Back to previous multipliers
const dprAdjustment = dpr >= 1.5 ? dpr * 0.8 : dpr * 1.5;
desiredDistortion = (imageWidth / normalWidth - 1) * DISTORTION_FACTOR * dprAdjustment;
if (event) {
mousemove(event);
}
}
function mouseout(event) {
desiredDistortion = 0;
if (event) {
mousemove(event);
}
}
function mousemove(event) {
if (!event) return;
const [mouseX] = d3.pointer(event, that);
desiredFocus = Math.max(0, Math.min(width - 1e-6, mouseX));
move();
}
function mousedown(event) {
if (!event) return;
const m = Math.max(0, Math.min(width - 1e-6, d3.pointer(event, that)[0]));
let i;
// Find the correct image index
for (i = 0; i < d.size && x(i * normalWidth) < m; i++);
// Subtract 1 to correct the off-by-one error
i = Math.max(0, i - 1);
// Construct the full image path
const imagePath = constructImagePath(i);
// Open image in new tab/window
window.open(imagePath, '_blank');
}
function touchstart(event) {
event.preventDefault();
isTouching = true;
hasMoved = false; // Reset movement flag
if (touchEndTimer) {
clearTimeout(touchEndTimer);
touchEndTimer = null;
}
const touch = event.touches[0];
const rect = that.getBoundingClientRect();
const touchX = touch.clientX - rect.left;
lastTouchX = touchX; // Store initial touch position
desiredFocus = Math.max(0, Math.min(width - 1e-6, touchX));
desiredDistortion = (imageWidth / normalWidth - 1) * DISTORTION_FACTOR * (dpr >= 1.5 ? dpr * 0.8 : dpr * 1.5);
move();
}
function touchmove(event) {
if (!isTouching) return;
event.preventDefault();
if (touchEndTimer) {
clearTimeout(touchEndTimer);
touchEndTimer = null;
}
const touch = event.touches[0];
const rect = that.getBoundingClientRect();
const touchX = touch.clientX - rect.left;
// Check if touch has moved more than threshold
if (Math.abs(touchX - lastTouchX) > 5) { // 5px movement threshold
hasMoved = true;
}
desiredFocus = Math.max(0, Math.min(width - 1e-6, touchX));
move();
}
function touchend(event) {
event.preventDefault();
// Only open image if touch hasn't moved significantly
if (!hasMoved && lastTouchX !== null) {
const i = Math.floor(lastTouchX / normalWidth);
if (i >= 0 && i < d.size) {
const imagePath = constructImagePath(i);
window.open(imagePath, '_blank');
}
}
isTouching = false;
lastTouchX = null; // Reset last touch position
if (touchEndTimer) {
clearTimeout(touchEndTimer);
}
touchEndTimer = setTimeout(() => {
desiredDistortion = 0;
move();
touchEndTimer = null;
}, 250); // 250ms delay before collapse
}
d.resize = function() {
const f = x.focus() / x.extent()[1];
const d1 = imageWidth / normalWidth - 1;
const d0 = x.distortion() / d1;
normalWidth = width / d.size;
x.distortion(d0 * d1).extent([0, width]).focus(f * width);
render();
};
}
function fisheye() {
let min = 0,
max = 1,
distortion = 3,
focus = 0;
function G(x) {
// Calculate normalized distance from focus point
const distance = Math.abs(x);
// Reduced base distortion and gentler falloff
const baseDistortion = distortion * (DISTORTION_FACTOR/2); // Added division by 2 to reduce base distortion
const falloffCurve = Math.exp(-distance * FALLOFF_FACTOR);
const adjustedDistortion = baseDistortion * (falloffCurve + 0.05); // Reduced constant from 0.1 to 0.05
return (adjustedDistortion + 1) * x / ((adjustedDistortion * x) + 1);
}
function fisheye(x) {
const Dmax_x = (x < focus ? min : max) - focus;
const Dnorm_x = x - focus;
return G(Dnorm_x / Dmax_x) * Dmax_x + focus;
}
fisheye.extent = function(_) {
if (!arguments.length) return [min, max];
min = +_[0], max = +_[1];
return fisheye;
};
fisheye.distortion = function(_) {
if (!arguments.length) return distortion;
distortion = +_;
return fisheye;
};
fisheye.focus = function(_) {
if (!arguments.length) return focus;
focus = +_;
return fisheye;
};
return fisheye;
}
})();
</script>
</body>
</html>