All files / repo/src/hooks useCanvasLayout.ts

100% Statements 44/44
88.88% Branches 8/9
100% Functions 3/3
100% Lines 44/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60      1x 4x 3x 3x   3x 3x 3x 3x 3x   3x 3x 2x 2x 3x   4x 2x 2x   1x 1x 1x 1x   1x 1x   1x 1x   1x 1x 1x   1x   1x 1x   1x 12x 12x 12x 12x 1x   1x 2x   4x 4x 4x 4x 4x  
import type { Tile } from '@/components/GameCanvas/GameCanvas.types';
import type { Ref } from 'vue';
 
export function useCanvasLayout(canvasRef: Ref<HTMLCanvasElement | null>, tiles: Ref<Tile[]>, currentTileSize: Ref<number>) {
  function layoutCanvas(width: number, height: number) {
    const canvas = canvasRef.value;
    if (!canvas) return;
 
    const dpr = window.devicePixelRatio || 1;
    canvas.width = width * dpr;
    canvas.height = height * dpr;
    canvas.style.width = `${width}px`;
    canvas.style.height = `${height}px`;
 
    const ctx = canvas.getContext('2d');
    if (ctx) {
      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
    }
  }
 
  function applyTileLayout() {
    const container = canvasRef.value?.parentElement;
    if (!container) return;
 
    const tileCount = tiles.value.length;
    const maxCols = 6;
    const spacing = 10;
    const padding = 20;
 
    const cols = Math.min(maxCols, Math.ceil(Math.sqrt(tileCount)));
    const rows = Math.ceil(tileCount / cols);
 
    const maxWidth = container.clientWidth;
    const maxHeight = window.innerHeight - 92 - 16 - 2;
 
    const tileSizeByWidth = Math.floor((maxWidth - padding * 2 - spacing * (cols - 1)) / cols);
    const tileSizeByHeight = Math.floor((maxHeight - padding * 2 - spacing * (rows - 1)) / rows);
    const tileSize = Math.min(tileSizeByWidth, tileSizeByHeight);
 
    currentTileSize.value = tileSize;
 
    const totalWidth = cols * tileSize + (cols - 1) * spacing + padding * 2;
    const totalHeight = rows * tileSize + (rows - 1) * spacing + padding * 2;
 
    tiles.value.forEach((tile, index) => {
      const row = Math.floor(index / cols);
      const col = index % cols;
      tile.x = col * (tileSize + spacing) + padding;
      tile.y = row * (tileSize + spacing) + padding;
    });
 
    layoutCanvas(totalWidth, totalHeight);
  }
 
  return {
    layoutCanvas,
    applyTileLayout,
  };
}