import { computed, MaybeRef, unref } from 'vue'; import { AnchorPoint } from '@3cr/types-ts'; import { useElementBounding } from '@vueuse/core'; export function useLayout2x2( anchor: MaybeRef, canvas: MaybeRef, padding: MaybeRef, ) { const { width, height } = useElementBounding(canvas); const layout = computed(() => { const w = width.value; const h = height.value; const p = unref(padding) ?? 0; const a = unref(anchor) ?? AnchorPoint.DEFAULT; switch (a) { case AnchorPoint.TOP_LEFT: { const x = p; const y = p; const width = Math.max(w / 2 - p * 1.5, 0); const height = Math.max(h / 2 - p * 1.5, 0); return { x, y, width, height }; } case AnchorPoint.TOP_RIGHT: { const x = w / 2 + p / 2; const y = p; const width = Math.max(w / 2 - p * 1.5, 0); const height = Math.max(h / 2 - p * 1.5, 0); return { x, y, width, height }; } case AnchorPoint.BOTTOM_LEFT: { const x = p; const y = h / 2 + p / 2; const width = Math.max(w / 2 - p * 1.5, 0); const height = Math.max(h / 2 - p * 1.5, 0); return { x, y, width, height }; } case AnchorPoint.BOTTOM_RIGHT: { const x = w / 2 + p / 2; const y = h / 2 + p / 2; const width = Math.max(w / 2 - p * 1.5, 0); const height = Math.max(h / 2 - p * 1.5, 0); return { x, y, width, height }; } default: return { x: 0, y: 0, width: 0, height: 0 }; } }); return { layout }; }