|
| 1 | +import { Application } from "pixi.js"; |
| 2 | +import { Viewport } from "../graphics/viewport"; |
| 3 | + |
| 4 | +export class ResponsiveHandler { |
| 5 | + private app: Application; |
| 6 | + private viewport: Viewport; |
| 7 | + private lBar: HTMLElement | null; |
| 8 | + private rBar: HTMLElement | null; |
| 9 | + private tBar: HTMLElement | null; |
| 10 | + |
| 11 | + constructor(app: Application, viewport: Viewport) { |
| 12 | + this.app = app; |
| 13 | + this.viewport = viewport; |
| 14 | + this.lBar = document.getElementById("left-bar"); |
| 15 | + this.rBar = document.getElementById("right-bar"); |
| 16 | + this.tBar = document.getElementById("top-bar"); |
| 17 | + |
| 18 | + this.resize(); |
| 19 | + window.addEventListener("resize", () => this.resize()); |
| 20 | + this.setupCanvasWrapper(); |
| 21 | + } |
| 22 | + |
| 23 | + private resize() { |
| 24 | + // Check if the layout should be stacked (based on window width) |
| 25 | + const isStacked = window.innerWidth <= 768; |
| 26 | + |
| 27 | + // Determine the size of the left bar (width if not stacked, height if stacked) |
| 28 | + const leftSize = isStacked |
| 29 | + ? this.lBar?.offsetHeight || 0 |
| 30 | + : this.lBar?.offsetWidth || 0; |
| 31 | + |
| 32 | + // Determine the size of the right bar (width if not stacked, height if stacked) |
| 33 | + const rightSize = isStacked |
| 34 | + ? this.rBar?.offsetHeight || 0 |
| 35 | + : this.rBar?.offsetWidth || 0; |
| 36 | + |
| 37 | + // Get the height of the top bar |
| 38 | + const topHeight = this.tBar?.offsetHeight || 0; |
| 39 | + |
| 40 | + // Calculate the new width and height for the canvas |
| 41 | + let newWidth = window.innerWidth - (isStacked ? 0 : leftSize + rightSize); |
| 42 | + let newHeight = |
| 43 | + window.innerHeight - (isStacked ? leftSize + rightSize : topHeight); |
| 44 | + |
| 45 | + // Ensure minimum dimensions to prevent the canvas from becoming too small |
| 46 | + newWidth = Math.max(300, newWidth); |
| 47 | + newHeight = Math.max(200, newHeight); |
| 48 | + |
| 49 | + // Log the new dimensions for debugging |
| 50 | + console.log("📏 Resizing canvas to:", newWidth, "x", newHeight); |
| 51 | + |
| 52 | + // Resize the app renderer and viewport accordingly |
| 53 | + this.app.renderer.resize(newWidth, newHeight); |
| 54 | + this.viewport.resize(newWidth, newHeight); |
| 55 | + } |
| 56 | + |
| 57 | + private setupCanvasWrapper() { |
| 58 | + // Get the element with the ID "canvas-wrapper" |
| 59 | + const canvasWrapper = document.getElementById("canvas-wrapper"); |
| 60 | + |
| 61 | + // Check if the element exists before adding event listeners |
| 62 | + if (canvasWrapper) { |
| 63 | + // When the mouse enters the canvas wrapper, prevent scrolling |
| 64 | + canvasWrapper.addEventListener("mouseenter", () => { |
| 65 | + document.body.classList.add("no-scroll"); |
| 66 | + }); |
| 67 | + |
| 68 | + // When the mouse leaves the canvas wrapper, allow scrolling again |
| 69 | + canvasWrapper.addEventListener("mouseleave", () => { |
| 70 | + document.body.classList.remove("no-scroll"); |
| 71 | + }); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments