Spaces:
Running
Running
Update js_scripts/index.js
Browse files- js_scripts/index.js +23 -25
js_scripts/index.js
CHANGED
@@ -17,13 +17,20 @@
|
|
17 |
return;
|
18 |
}
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
// --- Outer scope variables for camera state ---
|
21 |
let cameraInstance = null;
|
22 |
let controlsInstance = null;
|
23 |
let initialCameraPosition = null;
|
24 |
let initialCameraRotation = null;
|
25 |
-
// We'll save the imported SPLAT module here
|
26 |
-
let SPLAT = null;
|
27 |
|
28 |
// Generate a unique identifier for this widget instance.
|
29 |
const instanceId = Math.random().toString(36).substr(2, 8);
|
@@ -37,7 +44,7 @@
|
|
37 |
const maxAngle = parseFloat(config.maxAngle || "360");
|
38 |
const minAzimuth = config.minAzimuth !== undefined ? parseFloat(config.minAzimuth) : -Infinity;
|
39 |
const maxAzimuth = config.maxAzimuth !== undefined ? parseFloat(config.maxAzimuth) : Infinity;
|
40 |
-
|
41 |
// Read initial orbit parameters for desktop.
|
42 |
const initAlphaDesktop = config.initAlpha !== undefined ? parseFloat(config.initAlpha) : 0.5;
|
43 |
const initBetaDesktop = config.initBeta !== undefined ? parseFloat(config.initBeta) : 0.5;
|
@@ -82,7 +89,7 @@
|
|
82 |
}
|
83 |
}
|
84 |
|
85 |
-
// Create the widget container
|
86 |
const widgetContainer = document.createElement('div');
|
87 |
widgetContainer.id = 'ply-widget-container-' + instanceId;
|
88 |
widgetContainer.classList.add('ply-widget-container');
|
@@ -149,7 +156,6 @@
|
|
149 |
gifPreview.style.display = 'none';
|
150 |
viewerContainer.style.display = 'block';
|
151 |
closeBtn.style.display = 'none';
|
152 |
-
// Start the viewer immediately.
|
153 |
initializeViewer();
|
154 |
}
|
155 |
|
@@ -171,7 +177,6 @@
|
|
171 |
if (widgetContainer.classList.contains('fake-fullscreen')) {
|
172 |
widgetContainer.classList.remove('fake-fullscreen');
|
173 |
fullscreenToggle.textContent = '⇱';
|
174 |
-
// For fake-fullscreen, reset camera immediately.
|
175 |
resetCamera();
|
176 |
}
|
177 |
viewerContainer.style.display = 'none';
|
@@ -184,7 +189,6 @@
|
|
184 |
widgetContainer.classList.add('fake-fullscreen');
|
185 |
} else {
|
186 |
widgetContainer.classList.remove('fake-fullscreen');
|
187 |
-
// Reset camera when exiting fake-fullscreen.
|
188 |
resetCamera();
|
189 |
}
|
190 |
fullscreenToggle.textContent = widgetContainer.classList.contains('fake-fullscreen') ? '⇲' : '⇱';
|
@@ -208,8 +212,6 @@
|
|
208 |
});
|
209 |
|
210 |
// Listen for native fullscreen changes.
|
211 |
-
// Update the widget container styles so that in fullscreen
|
212 |
-
// the aspect ratio style is removed (height = 100% and padding-bottom = 0) and then reset the camera.
|
213 |
document.addEventListener('fullscreenchange', function() {
|
214 |
if (document.fullscreenElement === widgetContainer) {
|
215 |
fullscreenToggle.textContent = '⇲';
|
@@ -218,7 +220,6 @@
|
|
218 |
resetCamera();
|
219 |
} else {
|
220 |
fullscreenToggle.textContent = '⇱';
|
221 |
-
// Restore the original aspect ratio style when not fullscreen.
|
222 |
widgetContainer.style.height = '0';
|
223 |
widgetContainer.style.paddingBottom = aspectPercent;
|
224 |
resetCamera();
|
@@ -234,23 +235,20 @@
|
|
234 |
function resetCamera() {
|
235 |
console.log("Resetting camera to initial position.");
|
236 |
if (cameraInstance && initialCameraPosition && initialCameraRotation) {
|
237 |
-
// Reset camera position and rotation.
|
238 |
cameraInstance.position = initialCameraPosition.clone();
|
239 |
cameraInstance.rotation = initialCameraRotation.clone();
|
240 |
if (typeof cameraInstance.update === 'function') {
|
241 |
cameraInstance.update();
|
242 |
}
|
243 |
-
// Dispose of the current controls.
|
244 |
if (controlsInstance && typeof controlsInstance.dispose === 'function') {
|
245 |
controlsInstance.dispose();
|
246 |
}
|
247 |
-
// Re-create OrbitControls with the original chosen parameters.
|
248 |
controlsInstance = new SPLAT.OrbitControls(
|
249 |
cameraInstance,
|
250 |
canvas,
|
251 |
-
0.5,
|
252 |
-
0.5,
|
253 |
-
5,
|
254 |
true,
|
255 |
new SPLAT.Vector3(),
|
256 |
chosenInitAlpha,
|
@@ -268,13 +266,11 @@
|
|
268 |
}
|
269 |
}
|
270 |
|
271 |
-
// Reset button now calls the resetCamera function.
|
272 |
resetCameraBtn.addEventListener('click', async function() {
|
273 |
console.log("Reset camera button clicked.");
|
274 |
resetCamera();
|
275 |
});
|
276 |
|
277 |
-
// Add keydown listener to exit fullscreen with Esc (or Echap)
|
278 |
document.addEventListener('keydown', function(e) {
|
279 |
if (e.key === 'Escape' || e.key === 'Esc') {
|
280 |
let wasFullscreen = false;
|
@@ -302,7 +298,7 @@
|
|
302 |
progressDialog.style.display = 'block';
|
303 |
const renderer = new SPLAT.WebGLRenderer(canvas);
|
304 |
const scene = new SPLAT.Scene();
|
305 |
-
|
306 |
// Construct the camera.
|
307 |
const camera = new SPLAT.Camera();
|
308 |
|
@@ -310,9 +306,9 @@
|
|
310 |
controlsInstance = new SPLAT.OrbitControls(
|
311 |
camera,
|
312 |
canvas,
|
313 |
-
0.5,
|
314 |
-
0.5,
|
315 |
-
5,
|
316 |
true,
|
317 |
new SPLAT.Vector3(),
|
318 |
chosenInitAlpha,
|
@@ -321,11 +317,13 @@
|
|
321 |
);
|
322 |
|
323 |
cameraInstance = camera;
|
324 |
-
// Save the initial camera state
|
325 |
initialCameraPosition = camera.position.clone();
|
326 |
initialCameraRotation = camera.rotation.clone();
|
327 |
|
328 |
-
|
|
|
|
|
329 |
controlsInstance.maxZoom = maxZoom;
|
330 |
controlsInstance.minZoom = minZoom;
|
331 |
controlsInstance.minAngle = minAngle;
|
@@ -366,5 +364,5 @@
|
|
366 |
}
|
367 |
|
368 |
// If a gif_url exists, the viewer is started on preview click;
|
369 |
-
// otherwise, it
|
370 |
})();
|
|
|
17 |
return;
|
18 |
}
|
19 |
|
20 |
+
// Load the external CSS file if provided in the config.
|
21 |
+
if (config.css_url) {
|
22 |
+
const linkEl = document.createElement("link");
|
23 |
+
linkEl.rel = "stylesheet";
|
24 |
+
linkEl.href = config.css_url;
|
25 |
+
document.head.appendChild(linkEl);
|
26 |
+
}
|
27 |
+
|
28 |
// --- Outer scope variables for camera state ---
|
29 |
let cameraInstance = null;
|
30 |
let controlsInstance = null;
|
31 |
let initialCameraPosition = null;
|
32 |
let initialCameraRotation = null;
|
33 |
+
let SPLAT = null; // We'll save the imported SPLAT module here.
|
|
|
34 |
|
35 |
// Generate a unique identifier for this widget instance.
|
36 |
const instanceId = Math.random().toString(36).substr(2, 8);
|
|
|
44 |
const maxAngle = parseFloat(config.maxAngle || "360");
|
45 |
const minAzimuth = config.minAzimuth !== undefined ? parseFloat(config.minAzimuth) : -Infinity;
|
46 |
const maxAzimuth = config.maxAzimuth !== undefined ? parseFloat(config.maxAzimuth) : Infinity;
|
47 |
+
|
48 |
// Read initial orbit parameters for desktop.
|
49 |
const initAlphaDesktop = config.initAlpha !== undefined ? parseFloat(config.initAlpha) : 0.5;
|
50 |
const initBetaDesktop = config.initBeta !== undefined ? parseFloat(config.initBeta) : 0.5;
|
|
|
89 |
}
|
90 |
}
|
91 |
|
92 |
+
// Create the widget container.
|
93 |
const widgetContainer = document.createElement('div');
|
94 |
widgetContainer.id = 'ply-widget-container-' + instanceId;
|
95 |
widgetContainer.classList.add('ply-widget-container');
|
|
|
156 |
gifPreview.style.display = 'none';
|
157 |
viewerContainer.style.display = 'block';
|
158 |
closeBtn.style.display = 'none';
|
|
|
159 |
initializeViewer();
|
160 |
}
|
161 |
|
|
|
177 |
if (widgetContainer.classList.contains('fake-fullscreen')) {
|
178 |
widgetContainer.classList.remove('fake-fullscreen');
|
179 |
fullscreenToggle.textContent = '⇱';
|
|
|
180 |
resetCamera();
|
181 |
}
|
182 |
viewerContainer.style.display = 'none';
|
|
|
189 |
widgetContainer.classList.add('fake-fullscreen');
|
190 |
} else {
|
191 |
widgetContainer.classList.remove('fake-fullscreen');
|
|
|
192 |
resetCamera();
|
193 |
}
|
194 |
fullscreenToggle.textContent = widgetContainer.classList.contains('fake-fullscreen') ? '⇲' : '⇱';
|
|
|
212 |
});
|
213 |
|
214 |
// Listen for native fullscreen changes.
|
|
|
|
|
215 |
document.addEventListener('fullscreenchange', function() {
|
216 |
if (document.fullscreenElement === widgetContainer) {
|
217 |
fullscreenToggle.textContent = '⇲';
|
|
|
220 |
resetCamera();
|
221 |
} else {
|
222 |
fullscreenToggle.textContent = '⇱';
|
|
|
223 |
widgetContainer.style.height = '0';
|
224 |
widgetContainer.style.paddingBottom = aspectPercent;
|
225 |
resetCamera();
|
|
|
235 |
function resetCamera() {
|
236 |
console.log("Resetting camera to initial position.");
|
237 |
if (cameraInstance && initialCameraPosition && initialCameraRotation) {
|
|
|
238 |
cameraInstance.position = initialCameraPosition.clone();
|
239 |
cameraInstance.rotation = initialCameraRotation.clone();
|
240 |
if (typeof cameraInstance.update === 'function') {
|
241 |
cameraInstance.update();
|
242 |
}
|
|
|
243 |
if (controlsInstance && typeof controlsInstance.dispose === 'function') {
|
244 |
controlsInstance.dispose();
|
245 |
}
|
|
|
246 |
controlsInstance = new SPLAT.OrbitControls(
|
247 |
cameraInstance,
|
248 |
canvas,
|
249 |
+
0.5,
|
250 |
+
0.5,
|
251 |
+
5,
|
252 |
true,
|
253 |
new SPLAT.Vector3(),
|
254 |
chosenInitAlpha,
|
|
|
266 |
}
|
267 |
}
|
268 |
|
|
|
269 |
resetCameraBtn.addEventListener('click', async function() {
|
270 |
console.log("Reset camera button clicked.");
|
271 |
resetCamera();
|
272 |
});
|
273 |
|
|
|
274 |
document.addEventListener('keydown', function(e) {
|
275 |
if (e.key === 'Escape' || e.key === 'Esc') {
|
276 |
let wasFullscreen = false;
|
|
|
298 |
progressDialog.style.display = 'block';
|
299 |
const renderer = new SPLAT.WebGLRenderer(canvas);
|
300 |
const scene = new SPLAT.Scene();
|
301 |
+
|
302 |
// Construct the camera.
|
303 |
const camera = new SPLAT.Camera();
|
304 |
|
|
|
306 |
controlsInstance = new SPLAT.OrbitControls(
|
307 |
camera,
|
308 |
canvas,
|
309 |
+
0.5,
|
310 |
+
0.5,
|
311 |
+
5,
|
312 |
true,
|
313 |
new SPLAT.Vector3(),
|
314 |
chosenInitAlpha,
|
|
|
317 |
);
|
318 |
|
319 |
cameraInstance = camera;
|
320 |
+
// Save the initial camera state.
|
321 |
initialCameraPosition = camera.position.clone();
|
322 |
initialCameraRotation = camera.rotation.clone();
|
323 |
|
324 |
+
// Use the canvas_background from config.
|
325 |
+
canvas.style.background = config.canvas_background || "#FEFEFD";
|
326 |
+
|
327 |
controlsInstance.maxZoom = maxZoom;
|
328 |
controlsInstance.minZoom = minZoom;
|
329 |
controlsInstance.minAngle = minAngle;
|
|
|
364 |
}
|
365 |
|
366 |
// If a gif_url exists, the viewer is started on preview click;
|
367 |
+
// otherwise, it is started immediately.
|
368 |
})();
|