-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
64 lines (57 loc) · 1.93 KB
/
script.js
File metadata and controls
64 lines (57 loc) · 1.93 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
// Variables
var widthString = d3.select('#wrapper').style('width'),
width = Number(widthString.substring(0, widthString.length - 2)),
height = 50.
scalingFactor = 2;
// Set up fisheye scale
var scale = d3.fisheye.scale(d3.scale.linear)
.domain([0, thumbnailSet.length])
.range([0, width])
// Set up list
var list = d3.select('#list')
// Display thumbnails
var thumbnail = list.selectAll('div')
.data(thumbnailSet)
.enter()
.append('div')
.attr('class', 'thumbnail')
.style('width', function(d, i) { return scale(i + 1) - scale(i); })
.style('height', height)
.style('background-image', function(d) { return 'url(' + d + ')' })
.on('mousemove', function(d, i) {
d3.select('#preview').style('background-image', 'url(' + photoSet[i] +')');
});
// Scale thumbnails
list.on('mousemove', function() { scaleThumbnailsDesktop(d3.mouse(this)[0]); });
list.on('touchmove', function() { scaleThumbnailsMobile(d3.touches(this)[0][0]); });
function scaleThumbnailsDesktop(coordinate) {
scale.distortion(scalingFactor).focus(coordinate);
thumbnail.style('width', function(d , i) {
return scale(i + 1) - scale(i);
});
}
function scaleThumbnailsMobile(coordinate) {
var previewHasUpdated = false,
margin = 1,
totalWidth = 0;
scale.distortion(scalingFactor).focus(coordinate);
thumbnail.style('width', function(d , i) {
// Get the thumbnail at the coordinate
var currentWidth = scale(i + 1) - scale(i);
totalWidth += currentWidth + margin;
// Update #preview with the proper image
if (totalWidth >= coordinate && !previewHasUpdated) {
d3.select('#preview').style('background-image', 'url(' + photoSet[i - 1] +')');
previewHasUpdated = true;
}
return currentWidth;
});
}
// Show and hide list
function showThumbnails(bool) {
if (bool === true) {
list.attr('class', 'show');
} else {
list.attr('class', '');
}
}