bilca commited on
Commit
54bc759
·
verified ·
1 Parent(s): 7c56aa9

Create index_2.js

Browse files
Files changed (1) hide show
  1. index_2.js +257 -0
index_2.js ADDED
@@ -0,0 +1,257 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ // Read required URLs and optional limits from the query parameters.
15
+ var gifUrl = getScriptQueryParam("gif_url");
16
+ var plyUrl = getScriptQueryParam("ply_url");
17
+ // Optional parameters for zoom and angle limits:
18
+ var minZoom = parseFloat(getScriptQueryParam("minZoom")) || 1.5;
19
+ var maxZoom = parseFloat(getScriptQueryParam("maxZoom")) || 5;
20
+ var minAngle = parseFloat(getScriptQueryParam("minAngle")) || 0;
21
+ var maxAngle = parseFloat(getScriptQueryParam("maxAngle")) || 90;
22
+
23
+ // Inject CSS styles into the document head.
24
+ var styleEl = document.createElement('style');
25
+ styleEl.textContent = `
26
+ /* Widget container styling */
27
+ #ply-widget-container {
28
+ position: relative;
29
+ width: 100%;
30
+ height: 0;
31
+ padding-bottom: 100%;
32
+ }
33
+ /* GIF Preview styling */
34
+ #gif-preview-container {
35
+ position: absolute;
36
+ top: 0;
37
+ left: 0;
38
+ width: 100%;
39
+ height: 100%;
40
+ border: 1px solid #474558;
41
+ border-radius: 10px;
42
+ overflow: hidden;
43
+ cursor: pointer;
44
+ }
45
+ #gif-preview-container img {
46
+ width: 100%;
47
+ height: 100%;
48
+ object-fit: cover;
49
+ }
50
+ /* Viewer Container styling */
51
+ #viewer-container {
52
+ display: none;
53
+ position: absolute;
54
+ top: 0;
55
+ left: 0;
56
+ width: 100%;
57
+ height: 100%;
58
+ background: #FEFEFD;
59
+ border: 1px solid #474558;
60
+ border-radius: 10px;
61
+ }
62
+ /* Canvas fills the viewer container */
63
+ #canvas {
64
+ width: 100%;
65
+ height: 100%;
66
+ display: block;
67
+ }
68
+ /* Progress dialog styling (as a centered div) */
69
+ #progress-dialog {
70
+ position: absolute;
71
+ top: 50%;
72
+ left: 50%;
73
+ transform: translate(-50%, -50%);
74
+ border: none;
75
+ background: rgba(255,255,255,0.9);
76
+ padding: 20px;
77
+ border-radius: 5px;
78
+ z-index: 1000;
79
+ display: none;
80
+ }
81
+ /* Menu (instructions) content styling */
82
+ #menu-content {
83
+ display: none;
84
+ position: absolute;
85
+ top: 62px;
86
+ right: 70px;
87
+ background: #FFFEF4;
88
+ border: 1px solid #474558;
89
+ border-radius: 5px;
90
+ padding: 10px;
91
+ font-size: 15px;
92
+ line-height: 1.4;
93
+ color: #474558;
94
+ }
95
+ /* Button styling */
96
+ .widget-button {
97
+ position: absolute;
98
+ width: 45px;
99
+ height: 45px;
100
+ background-color: #FFFEF4;
101
+ border: 1px solid #474558;
102
+ border-radius: 50%;
103
+ cursor: pointer;
104
+ font-size: 14px;
105
+ color: #474558;
106
+ display: flex;
107
+ align-items: center;
108
+ justify-content: center;
109
+ }
110
+ /* Positions: Close at top-left, fullscreen at top-right, help (instructions) below fullscreen */
111
+ #close-btn {
112
+ top: 17px;
113
+ left: 15px;
114
+ }
115
+ #fullscreen-toggle {
116
+ top: 17px;
117
+ right: 15px;
118
+ }
119
+ #help-toggle {
120
+ top: 72px;
121
+ right: 15px;
122
+ }
123
+ `;
124
+ document.head.appendChild(styleEl);
125
+
126
+ // Create the widget container and set its inner HTML.
127
+ var widgetContainer = document.createElement('div');
128
+ widgetContainer.id = 'ply-widget-container';
129
+ widgetContainer.innerHTML = `
130
+ <!-- GIF Preview Container -->
131
+ <div id="gif-preview-container">
132
+ <img id="preview-image" alt="Preview" crossorigin="anonymous">
133
+ </div>
134
+ <!-- Viewer Container -->
135
+ <div id="viewer-container">
136
+ <canvas id="canvas"></canvas>
137
+ <div id="progress-dialog">
138
+ <progress id="progress-indicator" max="100" value="0"></progress>
139
+ </div>
140
+ <button id="close-btn" class="widget-button">X</button>
141
+ <button id="fullscreen-toggle" class="widget-button">⇱</button>
142
+ <button id="help-toggle" class="widget-button">?</button>
143
+ <div id="menu-content">
144
+ - Rotate with right click<br>
145
+ - Zoom in/out with middle click<br>
146
+ - Translate with left click
147
+ </div>
148
+ </div>
149
+ `;
150
+ // Append widgetContainer to the current script's parent so it appears in place.
151
+ document.currentScript.parentNode.appendChild(widgetContainer);
152
+
153
+ // Grab element references.
154
+ var gifPreview = document.getElementById('gif-preview-container');
155
+ var viewerContainer = document.getElementById('viewer-container');
156
+ var previewImage = document.getElementById('preview-image');
157
+ var closeBtn = document.getElementById('close-btn');
158
+ var fullscreenToggle = document.getElementById('fullscreen-toggle');
159
+ var helpToggle = document.getElementById('help-toggle');
160
+ var menuContent = document.getElementById('menu-content');
161
+ var canvas = document.getElementById('canvas');
162
+ var progressDialog = document.getElementById('progress-dialog');
163
+ var progressIndicator = document.getElementById('progress-indicator');
164
+
165
+ // Set the preview image if provided.
166
+ if (gifUrl) {
167
+ previewImage.src = gifUrl;
168
+ }
169
+
170
+ // --- Button Event Handlers ---
171
+
172
+ // When the preview image is clicked, hide it, show the viewer, and initialize the 3D viewer.
173
+ gifPreview.addEventListener('click', function() {
174
+ gifPreview.style.display = 'none';
175
+ viewerContainer.style.display = 'block';
176
+ initializeViewer();
177
+ });
178
+
179
+ // Close button: hide the viewer and show the preview.
180
+ closeBtn.addEventListener('click', function() {
181
+ // Exit fullscreen if active.
182
+ if (document.fullscreenElement === widgetContainer) {
183
+ if (document.exitFullscreen) {
184
+ document.exitFullscreen();
185
+ }
186
+ }
187
+ viewerContainer.style.display = 'none';
188
+ gifPreview.style.display = 'block';
189
+ });
190
+
191
+ // Fullscreen toggle: toggle fullscreen on the widget container.
192
+ fullscreenToggle.addEventListener('click', function() {
193
+ if (!document.fullscreenElement) {
194
+ if (widgetContainer.requestFullscreen) {
195
+ widgetContainer.requestFullscreen();
196
+ } else if (widgetContainer.webkitRequestFullscreen) {
197
+ widgetContainer.webkitRequestFullscreen();
198
+ } else if (widgetContainer.mozRequestFullScreen) {
199
+ widgetContainer.mozRequestFullScreen();
200
+ } else if (widgetContainer.msRequestFullscreen) {
201
+ widgetContainer.msRequestFullscreen();
202
+ }
203
+ } else {
204
+ if (document.exitFullscreen) {
205
+ document.exitFullscreen();
206
+ }
207
+ }
208
+ });
209
+
210
+ // Update the fullscreen button icon on fullscreen change.
211
+ document.addEventListener('fullscreenchange', function() {
212
+ if (document.fullscreenElement === widgetContainer) {
213
+ fullscreenToggle.textContent = '⇲';
214
+ } else {
215
+ fullscreenToggle.textContent = '⇱';
216
+ }
217
+ });
218
+
219
+ // Help (instructions) toggle: show/hide the instructions.
220
+ helpToggle.addEventListener('click', function(e) {
221
+ e.stopPropagation();
222
+ menuContent.style.display = (menuContent.style.display === 'block') ? 'none' : 'block';
223
+ });
224
+
225
+ // --- Initialize the 3D PLY Viewer using PlayCanvas Model Viewer ---
226
+ async function initializeViewer() {
227
+ // Show the progress dialog.
228
+ progressDialog.style.display = 'block';
229
+
230
+ try {
231
+ // Dynamically import the PlayCanvas Model Viewer module.
232
+ const { ModelViewer } = await import("https://cdn.jsdelivr.net/npm/@playcanvas/model-viewer@latest/dist/model-viewer.module.js");
233
+
234
+ // Create a new ModelViewer instance on the canvas.
235
+ const viewer = new ModelViewer(canvas, {
236
+ url: plyUrl, // URL of the PLY model to load
237
+ backgroundColor: "#FEFEFD",
238
+ minZoom: minZoom,
239
+ maxZoom: maxZoom,
240
+ minAngle: minAngle,
241
+ maxAngle: maxAngle,
242
+ // Callback to update the progress indicator (value between 0 and 1)
243
+ onProgress: function(progress) {
244
+ progressIndicator.value = progress * 100;
245
+ }
246
+ });
247
+
248
+ // Load the model.
249
+ await viewer.load();
250
+ // Hide the progress dialog once the model is loaded.
251
+ progressDialog.style.display = 'none';
252
+ } catch (error) {
253
+ console.error("Error loading model:", error);
254
+ progressDialog.innerHTML = `<p style="color: red">Error loading model: ${error.message}</p>`;
255
+ }
256
+ }
257
+ })();