-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
392 lines (330 loc) · 12.8 KB
/
index.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
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
/*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
(function() {
var Marzipano = window.Marzipano;
var bowser = window.bowser;
var screenfull = window.screenfull;
var data = window.APP_DATA;
// Grab elements from DOM.
var panoElement = document.querySelector('#pano');
var sceneNameElement = document.querySelector('#titleBar .sceneName');
var sceneListElement = document.querySelector('#sceneList');
var sceneElements = document.querySelectorAll('#sceneList .scene');
var sceneListToggleElement = document.querySelector('#sceneListToggle');
var autorotateToggleElement = document.querySelector('#autorotateToggle');
var fullscreenToggleElement = document.querySelector('#fullscreenToggle');
// Detect desktop or mobile mode.
if (window.matchMedia) {
var setMode = function() {
if (mql.matches) {
document.body.classList.remove('desktop');
document.body.classList.add('mobile');
} else {
document.body.classList.remove('mobile');
document.body.classList.add('desktop');
}
};
var mql = matchMedia("(max-width: 500px), (max-height: 500px)");
setMode();
mql.addListener(setMode);
} else {
document.body.classList.add('desktop');
}
// Detect whether we are on a touch device.
document.body.classList.add('no-touch');
window.addEventListener('touchstart', function() {
document.body.classList.remove('no-touch');
document.body.classList.add('touch');
});
// Use tooltip fallback mode on IE < 11.
if (bowser.msie && parseFloat(bowser.version) < 11) {
document.body.classList.add('tooltip-fallback');
}
// Viewer options.
var viewerOpts = {
controls: {
mouseViewMode: data.settings.mouseViewMode
}
};
// Initialize viewer.
var viewer = new Marzipano.Viewer(panoElement, viewerOpts);
// Create scenes.
var scenes = data.scenes.map(function(data) {
var urlPrefix = "tiles";
var source = Marzipano.ImageUrlSource.fromString(
urlPrefix + "/" + data.id + "/{z}/{f}/{y}/{x}.jpg",
{ cubeMapPreviewUrl: urlPrefix + "/" + data.id + "/preview.jpg" });
var geometry = new Marzipano.CubeGeometry(data.levels);
var limiter = Marzipano.RectilinearView.limit.traditional(data.faceSize, 100*Math.PI/180, 120*Math.PI/180);
var view = new Marzipano.RectilinearView(data.initialViewParameters, limiter);
var scene = viewer.createScene({
source: source,
geometry: geometry,
view: view,
pinFirstLevel: true
});
// Create link hotspots.
data.linkHotspots.forEach(function(hotspot) {
var element = createLinkHotspotElement(hotspot);
scene.hotspotContainer().createHotspot(element, { yaw: hotspot.yaw, pitch: hotspot.pitch });
});
// Create info hotspots.
data.infoHotspots.forEach(function(hotspot) {
var element = createInfoHotspotElement(hotspot);
scene.hotspotContainer().createHotspot(element, { yaw: hotspot.yaw, pitch: hotspot.pitch });
});
return {
data: data,
scene: scene,
view: view
};
});
// Set up autorotate, if enabled.
var autorotate = Marzipano.autorotate({
yawSpeed: 0.03,
targetPitch: 0,
targetFov: Math.PI/2
});
if (data.settings.autorotateEnabled) {
autorotateToggleElement.classList.add('enabled');
}
// Set handler for autorotate toggle.
autorotateToggleElement.addEventListener('click', toggleAutorotate);
// Set up fullscreen mode, if supported.
if (screenfull.enabled && data.settings.fullscreenButton) {
document.body.classList.add('fullscreen-enabled');
fullscreenToggleElement.addEventListener('click', toggleFullscreen);
} else {
document.body.classList.add('fullscreen-disabled');
}
// Set handler for scene list toggle.
sceneListToggleElement.addEventListener('click', toggleSceneList);
// Start with the scene list open on desktop.
if (!document.body.classList.contains('mobile')) {
showSceneList();
}
// Set handler for scene switch.
scenes.forEach(function(scene) {
var el = document.querySelector('#sceneList .scene[data-id="' + scene.data.id + '"]');
el.addEventListener('click', function() {
switchScene(scene);
// On mobile, hide scene list after selecting a scene.
if (document.body.classList.contains('mobile')) {
hideSceneList();
}
});
});
// DOM elements for view controls.
var viewUpElement = document.querySelector('#viewUp');
var viewDownElement = document.querySelector('#viewDown');
var viewLeftElement = document.querySelector('#viewLeft');
var viewRightElement = document.querySelector('#viewRight');
var viewInElement = document.querySelector('#viewIn');
var viewOutElement = document.querySelector('#viewOut');
// Dynamic parameters for controls.
var velocity = 0.7;
var friction = 3;
// Associate view controls with elements.
var controls = viewer.controls();
controls.registerMethod('upElement', new Marzipano.ElementPressControlMethod(viewUpElement, 'y', -velocity, friction), true);
controls.registerMethod('downElement', new Marzipano.ElementPressControlMethod(viewDownElement, 'y', velocity, friction), true);
controls.registerMethod('leftElement', new Marzipano.ElementPressControlMethod(viewLeftElement, 'x', -velocity, friction), true);
controls.registerMethod('rightElement', new Marzipano.ElementPressControlMethod(viewRightElement, 'x', velocity, friction), true);
controls.registerMethod('inElement', new Marzipano.ElementPressControlMethod(viewInElement, 'zoom', -velocity, friction), true);
controls.registerMethod('outElement', new Marzipano.ElementPressControlMethod(viewOutElement, 'zoom', velocity, friction), true);
function sanitize(s) {
return s.replace('&', '&').replace('<', '<').replace('>', '>');
}
function switchScene(scene) {
stopAutorotate();
scene.view.setParameters(scene.data.initialViewParameters);
scene.scene.switchTo();
startAutorotate();
updateSceneName(scene);
updateSceneList(scene);
}
function updateSceneName(scene) {
sceneNameElement.innerHTML = sanitize(scene.data.name);
}
function updateSceneList(scene) {
for (var i = 0; i < sceneElements.length; i++) {
var el = sceneElements[i];
if (el.getAttribute('data-id') === scene.data.id) {
el.classList.add('current');
} else {
el.classList.remove('current');
}
}
}
function showSceneList() {
sceneListElement.classList.add('enabled');
sceneListToggleElement.classList.add('enabled');
}
function hideSceneList() {
sceneListElement.classList.remove('enabled');
sceneListToggleElement.classList.remove('enabled');
}
function toggleSceneList() {
sceneListElement.classList.toggle('enabled');
sceneListToggleElement.classList.toggle('enabled');
}
function startAutorotate() {
if (!autorotateToggleElement.classList.contains('enabled')) {
return;
}
viewer.startMovement(autorotate);
viewer.setIdleMovement(3000, autorotate);
}
function stopAutorotate() {
viewer.stopMovement();
viewer.setIdleMovement(Infinity);
}
function toggleAutorotate() {
if (autorotateToggleElement.classList.contains('enabled')) {
autorotateToggleElement.classList.remove('enabled');
stopAutorotate();
} else {
autorotateToggleElement.classList.add('enabled');
startAutorotate();
}
}
function toggleFullscreen() {
screenfull.toggle();
if (screenfull.isFullscreen) {
fullscreenToggleElement.classList.add('enabled');
} else {
fullscreenToggleElement.classList.remove('enabled');
}
}
function createLinkHotspotElement(hotspot) {
// Create wrapper element to hold icon and tooltip.
var wrapper = document.createElement('div');
wrapper.classList.add('hotspot');
wrapper.classList.add('link-hotspot');
// Create image element.
var icon = document.createElement('img');
icon.src = 'img/link.png';
icon.classList.add('link-hotspot-icon');
// Set rotation transform.
var transformProperties = [ '-ms-transform', '-webkit-transform', 'transform' ];
for (var i = 0; i < transformProperties.length; i++) {
var property = transformProperties[i];
icon.style[property] = 'rotate(' + hotspot.rotation + 'rad)';
}
// Add click event handler.
wrapper.addEventListener('click', function() {
switchScene(findSceneById(hotspot.target));
});
// Prevent touch and scroll events from reaching the parent element.
// This prevents the view control logic from interfering with the hotspot.
stopTouchAndScrollEventPropagation(wrapper);
// Create tooltip element.
var tooltip = document.createElement('div');
tooltip.classList.add('hotspot-tooltip');
tooltip.classList.add('link-hotspot-tooltip');
tooltip.innerHTML = findSceneDataById(hotspot.target).name;
wrapper.appendChild(icon);
wrapper.appendChild(tooltip);
return wrapper;
}
function createInfoHotspotElement(hotspot) {
// Create wrapper element to hold icon and tooltip.
var wrapper = document.createElement('div');
wrapper.classList.add('hotspot');
wrapper.classList.add('info-hotspot');
// Create hotspot/tooltip header.
var header = document.createElement('div');
header.classList.add('info-hotspot-header');
// Create image element.
var iconWrapper = document.createElement('div');
iconWrapper.classList.add('info-hotspot-icon-wrapper');
var icon = document.createElement('img');
icon.src = 'img/info.png';
icon.classList.add('info-hotspot-icon');
iconWrapper.appendChild(icon);
// Create title element.
var titleWrapper = document.createElement('div');
titleWrapper.classList.add('info-hotspot-title-wrapper');
var title = document.createElement('div');
title.classList.add('info-hotspot-title');
title.innerHTML = hotspot.title;
titleWrapper.appendChild(title);
// Create close element.
var closeWrapper = document.createElement('div');
closeWrapper.classList.add('info-hotspot-close-wrapper');
var closeIcon = document.createElement('img');
closeIcon.src = 'img/close.png';
closeIcon.classList.add('info-hotspot-close-icon');
closeWrapper.appendChild(closeIcon);
// Construct header element.
header.appendChild(iconWrapper);
header.appendChild(titleWrapper);
header.appendChild(closeWrapper);
// Create text element.
var text = document.createElement('div');
text.classList.add('info-hotspot-text');
text.innerHTML = hotspot.text;
// Place header and text into wrapper element.
wrapper.appendChild(header);
wrapper.appendChild(text);
// Create a modal for the hotspot content to appear on mobile mode.
var modal = document.createElement('div');
modal.innerHTML = wrapper.innerHTML;
modal.classList.add('info-hotspot-modal');
document.body.appendChild(modal);
var toggle = function() {
wrapper.classList.toggle('visible');
modal.classList.toggle('visible');
};
// Show content when hotspot is clicked.
wrapper.querySelector('.info-hotspot-header').addEventListener('click', toggle);
// Hide content when close icon is clicked.
modal.querySelector('.info-hotspot-close-wrapper').addEventListener('click', toggle);
// Prevent touch and scroll events from reaching the parent element.
// This prevents the view control logic from interfering with the hotspot.
stopTouchAndScrollEventPropagation(wrapper);
return wrapper;
}
// Prevent touch and scroll events from reaching the parent element.
function stopTouchAndScrollEventPropagation(element, eventList) {
var eventList = [ 'touchstart', 'touchmove', 'touchend', 'touchcancel',
'wheel', 'mousewheel' ];
for (var i = 0; i < eventList.length; i++) {
element.addEventListener(eventList[i], function(event) {
event.stopPropagation();
});
}
}
function findSceneById(id) {
for (var i = 0; i < scenes.length; i++) {
if (scenes[i].data.id === id) {
return scenes[i];
}
}
return null;
}
function findSceneDataById(id) {
for (var i = 0; i < data.scenes.length; i++) {
if (data.scenes[i].id === id) {
return data.scenes[i];
}
}
return null;
}
// Display the initial scene.
switchScene(scenes[0]);
})();