Spaces:
Running
Running
Update index.js
Browse files
index.js
CHANGED
@@ -1,19 +1,18 @@
|
|
1 |
(function() {
|
2 |
-
// Helper
|
3 |
function getScriptQueryParam(param) {
|
4 |
-
|
5 |
if (document.currentScript && document.currentScript.src.indexOf('?') !== -1) {
|
6 |
-
|
7 |
params = new URLSearchParams(queryString);
|
8 |
} else {
|
9 |
-
// Fallback: check window.location if needed.
|
10 |
params = new URLSearchParams(window.location.search);
|
11 |
}
|
12 |
return params.get(param);
|
13 |
}
|
14 |
|
15 |
// Inject CSS styles into the document head.
|
16 |
-
|
17 |
styleEl.textContent = `
|
18 |
/* Ply Viewer Widget Styles */
|
19 |
#ply-widget-container {
|
@@ -70,7 +69,7 @@
|
|
70 |
transition: transform 0.1s ease;
|
71 |
}
|
72 |
|
73 |
-
/*
|
74 |
.widget-button {
|
75 |
width: 45px;
|
76 |
height: 45px;
|
@@ -121,23 +120,24 @@
|
|
121 |
`;
|
122 |
document.head.appendChild(styleEl);
|
123 |
|
124 |
-
// Create the widget container and its inner HTML.
|
125 |
-
|
126 |
widgetContainer.id = 'ply-widget-container';
|
127 |
widgetContainer.innerHTML = `
|
128 |
-
<!-- GIF Preview Container (initially
|
129 |
<div id="gif-preview-container">
|
130 |
-
<img id="preview-image" alt="Preview">
|
131 |
</div>
|
132 |
|
133 |
-
<!-- Viewer Container (initially
|
134 |
<div id="viewer-container" class="zoomable-iframe-widget">
|
135 |
<div class="iframe-container">
|
136 |
<object
|
137 |
id="viewer-object"
|
138 |
data=""
|
139 |
type="text/html"
|
140 |
-
class="gsplat"
|
|
|
141 |
</object>
|
142 |
</div>
|
143 |
|
@@ -154,34 +154,37 @@
|
|
154 |
`;
|
155 |
document.body.appendChild(widgetContainer);
|
156 |
|
157 |
-
//
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
|
168 |
-
//
|
169 |
-
|
170 |
-
|
171 |
|
172 |
-
|
173 |
-
|
174 |
-
|
|
|
|
|
|
|
175 |
|
176 |
-
|
177 |
|
178 |
-
//
|
179 |
gifPreview.addEventListener('click', function() {
|
180 |
gifPreview.style.display = 'none';
|
181 |
viewerContainer.style.display = 'block';
|
182 |
});
|
183 |
|
184 |
-
// Toggle help text when clicking the "?" button.
|
185 |
helpToggle.addEventListener('click', function(event) {
|
186 |
helpText.style.display = (helpText.style.display === 'none' || helpText.style.display === '')
|
187 |
? 'block'
|
@@ -189,7 +192,7 @@
|
|
189 |
event.stopPropagation();
|
190 |
});
|
191 |
|
192 |
-
// The "X" button
|
193 |
closeToggle.addEventListener('click', function() {
|
194 |
if (document.fullscreenElement === widgetContainer) {
|
195 |
if (document.exitFullscreen) {
|
@@ -205,27 +208,64 @@
|
|
205 |
viewerContainer.style.display = 'none';
|
206 |
helpText.style.display = 'none';
|
207 |
gifPreview.style.display = 'block';
|
208 |
-
// Re-enable page scrolling when the viewer is closed.
|
209 |
document.body.style.overflow = 'auto';
|
210 |
});
|
211 |
|
212 |
-
// Disable page scrolling
|
213 |
-
viewerContainer.addEventListener('mouseenter', ()
|
214 |
document.body.style.overflow = 'hidden';
|
215 |
});
|
216 |
-
viewerContainer.addEventListener('mouseleave', ()
|
217 |
document.body.style.overflow = 'auto';
|
218 |
});
|
219 |
|
220 |
-
//
|
221 |
-
|
222 |
-
iframeContainer.addEventListener('wheel', (event) => {
|
223 |
event.preventDefault();
|
224 |
event.stopPropagation();
|
225 |
-
|
226 |
if (event.deltaY < 0) {
|
227 |
scale += zoomSpeed;
|
228 |
} else {
|
229 |
scale -= zoomSpeed;
|
230 |
}
|
231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
(function() {
|
2 |
+
// Helper: Get query parameters from THIS script’s src URL.
|
3 |
function getScriptQueryParam(param) {
|
4 |
+
var params = new URLSearchParams("");
|
5 |
if (document.currentScript && document.currentScript.src.indexOf('?') !== -1) {
|
6 |
+
var queryString = document.currentScript.src.split('?')[1];
|
7 |
params = new URLSearchParams(queryString);
|
8 |
} else {
|
|
|
9 |
params = new URLSearchParams(window.location.search);
|
10 |
}
|
11 |
return params.get(param);
|
12 |
}
|
13 |
|
14 |
// Inject CSS styles into the document head.
|
15 |
+
var styleEl = document.createElement('style');
|
16 |
styleEl.textContent = `
|
17 |
/* Ply Viewer Widget Styles */
|
18 |
#ply-widget-container {
|
|
|
69 |
transition: transform 0.1s ease;
|
70 |
}
|
71 |
|
72 |
+
/* Button styling */
|
73 |
.widget-button {
|
74 |
width: 45px;
|
75 |
height: 45px;
|
|
|
120 |
`;
|
121 |
document.head.appendChild(styleEl);
|
122 |
|
123 |
+
// Create the widget container and set its inner HTML.
|
124 |
+
var widgetContainer = document.createElement('div');
|
125 |
widgetContainer.id = 'ply-widget-container';
|
126 |
widgetContainer.innerHTML = `
|
127 |
+
<!-- GIF Preview Container (shown initially) -->
|
128 |
<div id="gif-preview-container">
|
129 |
+
<img id="preview-image" alt="Preview" crossorigin="anonymous">
|
130 |
</div>
|
131 |
|
132 |
+
<!-- Viewer Container (hidden initially) -->
|
133 |
<div id="viewer-container" class="zoomable-iframe-widget">
|
134 |
<div class="iframe-container">
|
135 |
<object
|
136 |
id="viewer-object"
|
137 |
data=""
|
138 |
type="text/html"
|
139 |
+
class="gsplat"
|
140 |
+
crossorigin="anonymous">
|
141 |
</object>
|
142 |
</div>
|
143 |
|
|
|
154 |
`;
|
155 |
document.body.appendChild(widgetContainer);
|
156 |
|
157 |
+
// Grab element references.
|
158 |
+
var gifPreview = document.getElementById('gif-preview-container');
|
159 |
+
var viewerContainer = document.getElementById('viewer-container');
|
160 |
+
var iframeContainer = viewerContainer.querySelector('.iframe-container');
|
161 |
+
var contentObject = viewerContainer.querySelector('.gsplat');
|
162 |
+
var fullscreenToggle = viewerContainer.querySelector('.fullscreen-toggle');
|
163 |
+
var helpToggle = viewerContainer.querySelector('.help-toggle');
|
164 |
+
var closeToggle = viewerContainer.querySelector('.close-toggle');
|
165 |
+
var helpText = viewerContainer.querySelector('.help-text');
|
166 |
+
var previewImage = document.getElementById("preview-image");
|
167 |
|
168 |
+
// Read the URLs from the script’s query parameters.
|
169 |
+
var gifUrl = getScriptQueryParam("gif_url");
|
170 |
+
var plyUrl = getScriptQueryParam("ply_url");
|
171 |
|
172 |
+
if (gifUrl) {
|
173 |
+
previewImage.src = gifUrl;
|
174 |
+
}
|
175 |
+
if (plyUrl) {
|
176 |
+
contentObject.data = plyUrl;
|
177 |
+
}
|
178 |
|
179 |
+
var scale = 1;
|
180 |
|
181 |
+
// On clicking the preview, show the 3D viewer.
|
182 |
gifPreview.addEventListener('click', function() {
|
183 |
gifPreview.style.display = 'none';
|
184 |
viewerContainer.style.display = 'block';
|
185 |
});
|
186 |
|
187 |
+
// Toggle the help text when clicking the "?" button.
|
188 |
helpToggle.addEventListener('click', function(event) {
|
189 |
helpText.style.display = (helpText.style.display === 'none' || helpText.style.display === '')
|
190 |
? 'block'
|
|
|
192 |
event.stopPropagation();
|
193 |
});
|
194 |
|
195 |
+
// The "X" button returns to the preview and exits fullscreen if active.
|
196 |
closeToggle.addEventListener('click', function() {
|
197 |
if (document.fullscreenElement === widgetContainer) {
|
198 |
if (document.exitFullscreen) {
|
|
|
208 |
viewerContainer.style.display = 'none';
|
209 |
helpText.style.display = 'none';
|
210 |
gifPreview.style.display = 'block';
|
|
|
211 |
document.body.style.overflow = 'auto';
|
212 |
});
|
213 |
|
214 |
+
// Disable page scrolling while hovering over the viewer.
|
215 |
+
viewerContainer.addEventListener('mouseenter', function() {
|
216 |
document.body.style.overflow = 'hidden';
|
217 |
});
|
218 |
+
viewerContainer.addEventListener('mouseleave', function() {
|
219 |
document.body.style.overflow = 'auto';
|
220 |
});
|
221 |
|
222 |
+
// Enable zooming with the mouse wheel.
|
223 |
+
iframeContainer.addEventListener('wheel', function(event) {
|
|
|
224 |
event.preventDefault();
|
225 |
event.stopPropagation();
|
226 |
+
var zoomSpeed = 0.1;
|
227 |
if (event.deltaY < 0) {
|
228 |
scale += zoomSpeed;
|
229 |
} else {
|
230 |
scale -= zoomSpeed;
|
231 |
}
|
232 |
+
scale = Math.min(Math.max(scale, 0.5), 3);
|
233 |
+
contentObject.style.transform = 'scale(' + scale + ')';
|
234 |
+
}, { passive: false });
|
235 |
+
|
236 |
+
// Toggle fullscreen on clicking the fullscreen button.
|
237 |
+
fullscreenToggle.addEventListener('click', function() {
|
238 |
+
if (!document.fullscreenElement) {
|
239 |
+
if (widgetContainer.requestFullscreen) {
|
240 |
+
widgetContainer.requestFullscreen();
|
241 |
+
} else if (widgetContainer.webkitRequestFullscreen) {
|
242 |
+
widgetContainer.webkitRequestFullscreen();
|
243 |
+
} else if (widgetContainer.mozRequestFullScreen) {
|
244 |
+
widgetContainer.mozRequestFullScreen();
|
245 |
+
} else if (widgetContainer.msRequestFullscreen) {
|
246 |
+
widgetContainer.msRequestFullscreen();
|
247 |
+
}
|
248 |
+
} else {
|
249 |
+
if (document.fullscreenElement === widgetContainer) {
|
250 |
+
if (document.exitFullscreen) {
|
251 |
+
document.exitFullscreen();
|
252 |
+
} else if (document.webkitExitFullscreen) {
|
253 |
+
document.webkitExitFullscreen();
|
254 |
+
} else if (document.mozCancelFullScreen) {
|
255 |
+
document.mozCancelFullScreen();
|
256 |
+
} else if (document.msExitFullscreen) {
|
257 |
+
document.msExitFullscreen();
|
258 |
+
}
|
259 |
+
}
|
260 |
+
}
|
261 |
+
});
|
262 |
+
|
263 |
+
// Change the fullscreen button icon on fullscreen change.
|
264 |
+
document.addEventListener('fullscreenchange', function() {
|
265 |
+
if (document.fullscreenElement === widgetContainer) {
|
266 |
+
fullscreenToggle.textContent = '⇲';
|
267 |
+
} else {
|
268 |
+
fullscreenToggle.textContent = '⇱';
|
269 |
+
}
|
270 |
+
});
|
271 |
+
})();
|