11import { Application , Assets } from "pixi.js" ;
2-
32import {
43 deselectElement ,
54 loadFromFile ,
@@ -15,7 +14,6 @@ import { Viewport } from "./graphics/viewport";
1514import { GlobalContext } from "./context" ;
1615
1716// Assets
18- // Doing this includes the file in the build
1917import "./styles" ;
2018import RouterSvg from "./assets/router.svg" ;
2119import SwitchSvg from "./assets/switch.svg" ;
@@ -43,7 +41,11 @@ async function loadAssets(otherPromises: Promise<void>[]) {
4341
4442// IIFE to avoid errors
4543( async ( ) => {
46- // Initialization
44+ const canvasPlaceholder = document . getElementById ( "canvas" ) ;
45+ const lBar = document . getElementById ( "left-bar" ) ;
46+ const rBar = document . getElementById ( "right-bar" ) ;
47+ const tBar = document . getElementById ( "top-bar" ) ;
48+
4749 const app = new Application ( ) ;
4850 const appInitPromise = app . init ( {
4951 width : window . innerWidth ,
@@ -52,47 +54,61 @@ async function loadAssets(otherPromises: Promise<void>[]) {
5254 autoDensity : true ,
5355 antialias : true ,
5456 } ) ;
55- await loadAssets ( [ appInitPromise ] ) ;
5657
57- const canvasPlaceholder = document . getElementById ( "canvas" ) ;
58+ await loadAssets ( [ appInitPromise ] ) ;
5859 canvasPlaceholder . replaceWith ( app . canvas ) ;
5960
60- // Background container initialization
6161 const viewport = new Viewport ( app . renderer . events ) ;
6262 app . stage . addChild ( viewport ) ;
6363
64- // Context initialization
6564 const ctx = new GlobalContext ( viewport ) ;
6665
67- // Get the layer’s menu
6866 const layerSelect = document . getElementById (
6967 "layer-select" ,
7068 ) as HTMLSelectElement ;
71-
7269 layerSelect . value = layerToName ( ctx . getCurrentLayer ( ) ) ;
7370
74- // Initialize RightBar
7571 RightBar . getInstance ( ) ;
7672
77- // Left bar logic
7873 const leftBar = LeftBar . getFrom ( document , ctx ) ;
7974 leftBar . setButtonsByLayer ( layerSelect . value ) ;
8075
81- const lBar = document . getElementById ( "left-bar" ) ;
82- const rBar = document . getElementById ( "right-bar" ) ;
83- const tBar = document . getElementById ( "top-bar" ) ;
84-
85- // Resize logic
8676 function resize ( ) {
87- const newWidth = window . innerWidth - lBar . offsetWidth - rBar . offsetWidth ;
88- const newHeight = window . innerHeight - tBar . offsetHeight ;
77+ // Check if the layout should be stacked (based on window width)
78+ const isStacked = window . innerWidth <= 768 ;
79+
80+ // Determine the size of the left bar (width if not stacked, height if stacked)
81+ const leftSize = isStacked
82+ ? lBar ?. offsetHeight || 0
83+ : lBar ?. offsetWidth || 0 ;
84+
85+ // Determine the size of the right bar (width if not stacked, height if stacked)
86+ const rightSize = isStacked
87+ ? rBar ?. offsetHeight || 0
88+ : rBar ?. offsetWidth || 0 ;
89+
90+ // Get the height of the top bar
91+ const topHeight = tBar ?. offsetHeight || 0 ;
92+
93+ // Calculate the new width and height for the canvas
94+ // If stacked, reduce height by left and right sizes; otherwise, reduce width
95+ let newWidth = window . innerWidth - ( isStacked ? 0 : leftSize + rightSize ) ;
96+ let newHeight =
97+ window . innerHeight - ( isStacked ? leftSize + rightSize : topHeight ) ;
98+
99+ // Ensure minimum dimensions to prevent the canvas from becoming too small
100+ newWidth = Math . max ( 300 , newWidth ) ;
101+ newHeight = Math . max ( 200 , newHeight ) ;
89102
103+ // Log the new dimensions for debugging
104+ console . log ( "📏 Resizing canvas to:" , newWidth , "x" , newHeight ) ;
105+
106+ // Resize the app renderer and viewport accordingly
90107 app . renderer . resize ( newWidth , newHeight ) ;
91108 viewport . resize ( newWidth , newHeight ) ;
92109 }
93110
94111 resize ( ) ;
95-
96112 window . addEventListener ( "resize" , resize ) ;
97113
98114 const newButton = document . getElementById ( "new-button" ) ;
@@ -266,5 +282,21 @@ async function loadAssets(otherPromises: Promise<void>[]) {
266282 // Initialize with default value
267283 valueDisplay . textContent = `${ ( speedWheel as HTMLInputElement ) . value } x` ;
268284
269- console . log ( "initialized!" ) ;
285+ // Get the element with the ID "canvas-wrapper"
286+ const canvasWrapper = document . getElementById ( "canvas-wrapper" ) ;
287+
288+ // Check if the element exists before adding event listeners
289+ if ( canvasWrapper ) {
290+ // When the mouse enters the canvas wrapper, prevent scrolling
291+ canvasWrapper . addEventListener ( "mouseenter" , ( ) => {
292+ document . body . classList . add ( "no-scroll" ) ;
293+ } ) ;
294+
295+ // When the mouse leaves the canvas wrapper, allow scrolling again
296+ canvasWrapper . addEventListener ( "mouseleave" , ( ) => {
297+ document . body . classList . remove ( "no-scroll" ) ;
298+ } ) ;
299+ }
300+
301+ console . log ( "✅ Initialized!" ) ;
270302} ) ( ) ;
0 commit comments