|
1 |
| -document.addEventListener('DOMContentLoaded', function() { |
| 1 | +document.addEventListener('DOMContentLoaded', async function() { |
2 | 2 | // Get references to HTML elements
|
3 | 3 | const canvas = document.getElementById('renderCanvas');
|
4 | 4 | const animationList = document.getElementById('animationList');
|
@@ -87,8 +87,8 @@ document.addEventListener('DOMContentLoaded', function() {
|
87 | 87 |
|
88 | 88 | // Create the scene
|
89 | 89 | const createScene = async function() {
|
90 |
| - // Create a basic scene |
91 |
| - scene = new BABYLON.Scene(engine); |
| 90 | + const scene = new BABYLON.Scene(engine); |
| 91 | + window.scene = scene; // Make scene globally accessible |
92 | 92 |
|
93 | 93 | // Set transparent background by setting alpha to 0
|
94 | 94 | scene.clearColor = new BABYLON.Color4(0, 0, 0, 0);
|
@@ -180,6 +180,9 @@ document.addEventListener('DOMContentLoaded', function() {
|
180 | 180 | // Create camera adjustment UI
|
181 | 181 | createCameraControls();
|
182 | 182 |
|
| 183 | + // Dispatch event when scene is ready |
| 184 | + window.dispatchEvent(new CustomEvent('sceneReady')); |
| 185 | + |
183 | 186 | return scene;
|
184 | 187 | };
|
185 | 188 |
|
@@ -979,46 +982,62 @@ document.addEventListener('DOMContentLoaded', function() {
|
979 | 982 | }
|
980 | 983 |
|
981 | 984 | // Create and set up the scene
|
982 |
| - createScene().then(() => { |
983 |
| - // Register event listeners |
984 |
| - playBtn.addEventListener('click', () => { |
985 |
| - const selectedIndex = parseInt(animationList.value); |
986 |
| - playAnimation(selectedIndex); |
987 |
| - }); |
988 |
| - |
989 |
| - pauseBtn.addEventListener('click', () => { |
990 |
| - if (currentAnimationGroup) { |
991 |
| - currentAnimationGroup.pause(); |
992 |
| - showStatus(`Paused animation: ${currentAnimationGroup.name}`); |
993 |
| - } |
994 |
| - }); |
995 |
| - |
996 |
| - animationList.addEventListener('change', () => { |
997 |
| - const selectedIndex = parseInt(animationList.value); |
998 |
| - playAnimation(selectedIndex); |
999 |
| - }); |
1000 |
| - |
1001 |
| - // Add sprite sheet recording event listener |
1002 |
| - recordBtn.addEventListener('click', startRecording); |
| 985 | + try { |
| 986 | + scene = await createScene(); |
1003 | 987 |
|
1004 |
| - // Run the render loop with frame capture REMOVED from here |
| 988 | + // Only start render loop after scene is created |
1005 | 989 | engine.runRenderLoop(() => {
|
1006 |
| - scene.render(); |
1007 |
| - // We'll handle frame capture separately with the recordingLoop function |
| 990 | + if (scene) { |
| 991 | + scene.render(); |
| 992 | + } |
1008 | 993 | });
|
1009 |
| - |
| 994 | + |
1010 | 995 | // Handle window resize
|
1011 | 996 | window.addEventListener('resize', () => {
|
1012 |
| - // Resize the engine |
1013 | 997 | engine.resize();
|
1014 |
| - |
1015 |
| - // Ensure camera maintains square ratio |
1016 |
| - updateOrthoCamera(); |
1017 |
| - |
1018 |
| - // Update canvas size to remain square |
1019 |
| - const size = Math.min(window.innerHeight, window.innerWidth); |
1020 |
| - canvas.style.width = `${size}px`; |
1021 |
| - canvas.style.height = `${size}px`; |
1022 | 998 | });
|
| 999 | + } catch (error) { |
| 1000 | + console.error("Error creating scene:", error); |
| 1001 | + } |
| 1002 | + |
| 1003 | + // Register event listeners |
| 1004 | + playBtn.addEventListener('click', () => { |
| 1005 | + const selectedIndex = parseInt(animationList.value); |
| 1006 | + playAnimation(selectedIndex); |
| 1007 | + }); |
| 1008 | + |
| 1009 | + pauseBtn.addEventListener('click', () => { |
| 1010 | + if (currentAnimationGroup) { |
| 1011 | + currentAnimationGroup.pause(); |
| 1012 | + showStatus(`Paused animation: ${currentAnimationGroup.name}`); |
| 1013 | + } |
| 1014 | + }); |
| 1015 | + |
| 1016 | + animationList.addEventListener('change', () => { |
| 1017 | + const selectedIndex = parseInt(animationList.value); |
| 1018 | + playAnimation(selectedIndex); |
| 1019 | + }); |
| 1020 | + |
| 1021 | + // Add sprite sheet recording event listener |
| 1022 | + recordBtn.addEventListener('click', startRecording); |
| 1023 | + |
| 1024 | + // Run the render loop with frame capture REMOVED from here |
| 1025 | + engine.runRenderLoop(() => { |
| 1026 | + scene.render(); |
| 1027 | + // We'll handle frame capture separately with the recordingLoop function |
| 1028 | + }); |
| 1029 | + |
| 1030 | + // Handle window resize |
| 1031 | + window.addEventListener('resize', () => { |
| 1032 | + // Resize the engine |
| 1033 | + engine.resize(); |
| 1034 | + |
| 1035 | + // Ensure camera maintains square ratio |
| 1036 | + updateOrthoCamera(); |
| 1037 | + |
| 1038 | + // Update canvas size to remain square |
| 1039 | + const size = Math.min(window.innerHeight, window.innerWidth); |
| 1040 | + canvas.style.width = `${size}px`; |
| 1041 | + canvas.style.height = `${size}px`; |
1023 | 1042 | });
|
1024 | 1043 | });
|
0 commit comments