-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
plane_photo.js
105 lines (81 loc) · 2.46 KB
/
plane_photo.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
import { dispatch as d3_dispatch } from 'd3-dispatch';
import { zoom as d3_zoom, zoomIdentity as d3_zoomIdentity } from 'd3-zoom';
import { utilSetTransform, utilRebind } from '../util';
const dispatch = d3_dispatch('viewerChanged');
let _photo;
let _wrapper;
let imgZoom;
let _widthOverflow;
function zoomPan (d3_event) {
let t = d3_event.transform;
_photo.call(utilSetTransform, t.x, t.y, t.k);
}
function zoomBeahvior () {
const {width: wrapperWidth, height: wrapperHeight} = _wrapper.node().getBoundingClientRect();
const {naturalHeight, naturalWidth} = _photo.node();
const intrinsicRatio = naturalWidth / naturalHeight;
_widthOverflow = wrapperHeight * intrinsicRatio - wrapperWidth;
return d3_zoom()
.extent([[0, 0], [wrapperWidth, wrapperHeight]])
.translateExtent([[0, 0], [wrapperWidth + _widthOverflow, wrapperHeight]])
.scaleExtent([1, 15])
.on('zoom', zoomPan);
}
function loadImage (selection, path) {
return new Promise((resolve) => {
selection.attr('src', path);
selection.on('load', () => {
resolve(selection);
});
});
}
export default {
init: async function(context, selection) {
this.event = utilRebind(this, dispatch, 'on');
_wrapper = selection
.append('div')
.attr('class', 'photo-frame plane-frame')
.classed('hide', true);
_photo = _wrapper
.append('img')
.attr('class', 'plane-photo');
context.ui().photoviewer.on('resize.plane', () => {
imgZoom = zoomBeahvior();
_wrapper.call(imgZoom);
});
await Promise.resolve();
return this;
},
showPhotoFrame: function (context) {
const isHidden = context.selectAll('.photo-frame.plane-frame.hide').size();
if (isHidden) {
context
.selectAll('.photo-frame:not(.plane-frame)')
.classed('hide', true);
context
.selectAll('.photo-frame.plane-frame')
.classed('hide', false);
}
return this;
},
hidePhotoFrame: function (context) {
context
.select('photo-frame.plane-frame')
.classed('hide', false);
return this;
},
selectPhoto: function (data) {
dispatch.call('viewerChanged');
loadImage(_photo, '');
loadImage(_photo, data.image_path)
.then(() => {
imgZoom = zoomBeahvior();
_wrapper.call(imgZoom);
_wrapper.call(imgZoom.transform, d3_zoomIdentity.translate(-_widthOverflow / 2, 0));
});
return this;
},
getYaw: function() {
return 0;
}
};