11import React , { useState , useEffect } from 'react' ;
2- import 'bootstrap/dist/css/bootstrap.min.css' ; // Import Bootstrap CSS
3- import { Modal , Button } from 'react-bootstrap' ; // Import React Bootstrap components
2+ import 'bootstrap/dist/css/bootstrap.min.css' ;
3+ import { Modal , Button } from 'react-bootstrap' ;
44
55function App ( ) {
66 const [ showModal , setShowModal ] = useState ( false ) ;
7+ const [ wasmScriptLoaded , setWasmScriptLoaded ] = useState ( false ) ;
78
8- const handleModalClose = ( ) => setShowModal ( false ) ;
9- const handleModalShow = ( ) => setShowModal ( true ) ;
9+ const handleModalClose = ( ) => {
10+ setShowModal ( false ) ;
11+ cleanupCanvas ( ) ; // Cleanup when the modal is closed
12+ } ;
1013
11- useEffect ( ( ) => {
12- // Load the WASM-related JavaScript file dynamically when modal is shown
13- if ( showModal ) {
14- const script = document . createElement ( 'script' ) ;
15- script . src = '/wasm/sorting_algorithms.js' ; // Path to your WASM JS file
16- script . async = true ;
17- script . onload = ( ) => {
18- console . log ( "WASM script loaded successfully." ) ;
14+ const handleModalShow = ( ) => {
15+ setShowModal ( true ) ;
16+ if ( ! wasmScriptLoaded ) {
17+ loadWasmScript ( ) ; // Load the WASM script only once
18+ } else {
19+ reassignCanvas ( ) ; // Reassign the canvas when the modal is opened
20+ }
21+ } ;
22+
23+ const loadWasmScript = ( ) => {
24+ const script = document . createElement ( 'script' ) ;
25+ script . src = '/wasm/sorting_algorithms.js' ; // Path to your WASM JS file
26+ script . async = true ;
27+ script . onload = ( ) => {
28+ console . log ( "WASM script loaded successfully." ) ;
29+ setWasmScriptLoaded ( true ) ; // Mark the script as loaded
30+ reassignCanvas ( ) ; // Initialize the WebAssembly after loading
31+ } ;
32+ document . body . appendChild ( script ) ;
33+ } ;
1934
20- // Ensure the canvas is available before initializing WebAssembly
21- const canvasElement = document . getElementById ( 'canvas' ) ;
22- if ( canvasElement ) {
23- console . log ( "Canvas element is available ." ) ;
24- // Initialize the WASM module here, ensuring it works with the canvas
25- if ( window . Module ) {
26- window . Module . canvas = canvasElement ;
27- }
28- }
29- } ;
30- document . body . appendChild ( script ) ;
35+ const reassignCanvas = ( ) => {
36+ const canvasElement = document . getElementById ( 'canvas' ) ;
37+ if ( canvasElement && window . Module ) {
38+ console . log ( "Reassigning canvas to WASM module ." ) ;
39+ // Set the canvas dimensions
40+ canvasElement . width = window . innerWidth ;
41+ canvasElement . height = 510 ;
42+
43+ window . Module . canvas = canvasElement ; // Reassign the canvas
44+ }
45+ } ;
3146
32- return ( ) => {
33- document . body . removeChild ( script ) ; // Cleanup the script tag when modal is closed
34- } ;
47+ const cleanupCanvas = ( ) => {
48+ const canvasElement = document . getElementById ( 'canvas' ) ;
49+ if ( canvasElement ) {
50+ canvasElement . width = 0 ;
51+ canvasElement . height = 0 ;
52+ }
53+ if ( window . Module ) {
54+ window . Module . canvas = null ; // Remove the canvas from the module
55+ }
56+ } ;
57+
58+ useEffect ( ( ) => {
59+ if ( showModal && wasmScriptLoaded ) {
60+ reassignCanvas ( ) ; // Reassign the canvas when the modal is opened
3561 }
36- } , [ showModal ] ) ;
62+ } , [ showModal , wasmScriptLoaded ] ) ;
3763
3864 return (
3965 < div className = "App" >
@@ -89,4 +115,4 @@ function App() {
89115 ) ;
90116}
91117
92- export default App ;
118+ export default App ;
0 commit comments