Spaces:
Running
Running
Update js_scripts/index.js
Browse files- js_scripts/index.js +43 -3
js_scripts/index.js
CHANGED
|
@@ -60,6 +60,7 @@ const currentScriptTag = document.currentScript;
|
|
| 60 |
let modelEntity = null;
|
| 61 |
let viewerInitialized = false;
|
| 62 |
let wheelHandlers = [];
|
|
|
|
| 63 |
|
| 64 |
// Generate a unique identifier for this widget instance.
|
| 65 |
const instanceId = Math.random().toString(36).substr(2, 8);
|
|
@@ -211,6 +212,8 @@ const currentScriptTag = document.currentScript;
|
|
| 211 |
// and hide the "close" button since there's no preview to return to.
|
| 212 |
if (gifUrl) {
|
| 213 |
previewImage.src = gifUrl;
|
|
|
|
|
|
|
| 214 |
} else {
|
| 215 |
gifPreview.style.display = 'none';
|
| 216 |
viewerContainer.style.display = 'block';
|
|
@@ -236,6 +239,12 @@ const currentScriptTag = document.currentScript;
|
|
| 236 |
}
|
| 237 |
wheelHandlers = [];
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
// Destroy PlayCanvas app if it exists
|
| 240 |
if (app) {
|
| 241 |
app.destroy();
|
|
@@ -246,6 +255,19 @@ const currentScriptTag = document.currentScript;
|
|
| 246 |
cameraEntity = null;
|
| 247 |
modelEntity = null;
|
| 248 |
viewerInitialized = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
}
|
| 250 |
|
| 251 |
closeBtn.addEventListener('click', function() {
|
|
@@ -455,8 +477,12 @@ const currentScriptTag = document.currentScript;
|
|
| 455 |
// --- Initialize the 3D PLY Viewer using PlayCanvas ---
|
| 456 |
async function initializeViewer() {
|
| 457 |
// Skip initialization if already initialized
|
| 458 |
-
if (viewerInitialized)
|
|
|
|
|
|
|
|
|
|
| 459 |
|
|
|
|
| 460 |
progressDialog.style.display = 'block';
|
| 461 |
|
| 462 |
// Initialize PlayCanvas
|
|
@@ -509,8 +535,14 @@ const currentScriptTag = document.currentScript;
|
|
| 509 |
app.resizeCanvas(canvas.clientWidth, canvas.clientHeight);
|
| 510 |
}
|
| 511 |
};
|
| 512 |
-
|
| 513 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 514 |
|
| 515 |
// Load required assets
|
| 516 |
const assets = {
|
|
@@ -548,6 +580,11 @@ const currentScriptTag = document.currentScript;
|
|
| 548 |
|
| 549 |
const progressChecker = setInterval(checkProgress, 100);
|
| 550 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 551 |
// Load assets and set up scene
|
| 552 |
assetListLoader.load(() => {
|
| 553 |
app.start();
|
|
@@ -648,11 +685,14 @@ const currentScriptTag = document.currentScript;
|
|
| 648 |
|
| 649 |
// Mark viewer as initialized
|
| 650 |
viewerInitialized = true;
|
|
|
|
|
|
|
| 651 |
});
|
| 652 |
|
| 653 |
} catch (error) {
|
| 654 |
console.error("Error initializing PlayCanvas viewer:", error);
|
| 655 |
progressDialog.innerHTML = `<p style="color: red">Error loading viewer: ${error.message}</p>`;
|
|
|
|
| 656 |
}
|
| 657 |
}
|
| 658 |
})();
|
|
|
|
| 60 |
let modelEntity = null;
|
| 61 |
let viewerInitialized = false;
|
| 62 |
let wheelHandlers = [];
|
| 63 |
+
let resizeHandler = null;
|
| 64 |
|
| 65 |
// Generate a unique identifier for this widget instance.
|
| 66 |
const instanceId = Math.random().toString(36).substr(2, 8);
|
|
|
|
| 212 |
// and hide the "close" button since there's no preview to return to.
|
| 213 |
if (gifUrl) {
|
| 214 |
previewImage.src = gifUrl;
|
| 215 |
+
viewerContainer.style.display = 'none';
|
| 216 |
+
gifPreview.style.display = 'block';
|
| 217 |
} else {
|
| 218 |
gifPreview.style.display = 'none';
|
| 219 |
viewerContainer.style.display = 'block';
|
|
|
|
| 239 |
}
|
| 240 |
wheelHandlers = [];
|
| 241 |
|
| 242 |
+
// Remove resize handler if it exists
|
| 243 |
+
if (resizeHandler) {
|
| 244 |
+
window.removeEventListener('resize', resizeHandler);
|
| 245 |
+
resizeHandler = null;
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
// Destroy PlayCanvas app if it exists
|
| 249 |
if (app) {
|
| 250 |
app.destroy();
|
|
|
|
| 255 |
cameraEntity = null;
|
| 256 |
modelEntity = null;
|
| 257 |
viewerInitialized = false;
|
| 258 |
+
|
| 259 |
+
// Reset the canvas
|
| 260 |
+
if (canvas) {
|
| 261 |
+
const ctx = canvas.getContext('webgl2') || canvas.getContext('webgl');
|
| 262 |
+
if (ctx) {
|
| 263 |
+
ctx.getExtension('WEBGL_lose_context')?.loseContext();
|
| 264 |
+
}
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
// Mark the viewer as not initialized
|
| 268 |
+
viewerInitialized = false;
|
| 269 |
+
|
| 270 |
+
console.log("Viewer cleanup complete");
|
| 271 |
}
|
| 272 |
|
| 273 |
closeBtn.addEventListener('click', function() {
|
|
|
|
| 477 |
// --- Initialize the 3D PLY Viewer using PlayCanvas ---
|
| 478 |
async function initializeViewer() {
|
| 479 |
// Skip initialization if already initialized
|
| 480 |
+
if (viewerInitialized) {
|
| 481 |
+
console.log("Viewer already initialized, skipping");
|
| 482 |
+
return;
|
| 483 |
+
}
|
| 484 |
|
| 485 |
+
console.log("Initializing PLY viewer...");
|
| 486 |
progressDialog.style.display = 'block';
|
| 487 |
|
| 488 |
// Initialize PlayCanvas
|
|
|
|
| 535 |
app.resizeCanvas(canvas.clientWidth, canvas.clientHeight);
|
| 536 |
}
|
| 537 |
};
|
| 538 |
+
|
| 539 |
+
// Store resize handler for cleanup
|
| 540 |
+
resizeHandler = resize;
|
| 541 |
+
window.addEventListener('resize', resizeHandler);
|
| 542 |
+
app.on('destroy', () => {
|
| 543 |
+
window.removeEventListener('resize', resizeHandler);
|
| 544 |
+
resizeHandler = null;
|
| 545 |
+
});
|
| 546 |
|
| 547 |
// Load required assets
|
| 548 |
const assets = {
|
|
|
|
| 580 |
|
| 581 |
const progressChecker = setInterval(checkProgress, 100);
|
| 582 |
|
| 583 |
+
// Store the interval for cleanup
|
| 584 |
+
app.once('destroy', () => {
|
| 585 |
+
clearInterval(progressChecker);
|
| 586 |
+
});
|
| 587 |
+
|
| 588 |
// Load assets and set up scene
|
| 589 |
assetListLoader.load(() => {
|
| 590 |
app.start();
|
|
|
|
| 685 |
|
| 686 |
// Mark viewer as initialized
|
| 687 |
viewerInitialized = true;
|
| 688 |
+
|
| 689 |
+
console.log("PLY viewer initialization complete");
|
| 690 |
});
|
| 691 |
|
| 692 |
} catch (error) {
|
| 693 |
console.error("Error initializing PlayCanvas viewer:", error);
|
| 694 |
progressDialog.innerHTML = `<p style="color: red">Error loading viewer: ${error.message}</p>`;
|
| 695 |
+
viewerInitialized = false;
|
| 696 |
}
|
| 697 |
}
|
| 698 |
})();
|