<script setup lang="ts">
import { useWindowSize } from '@vueuse/core';
import { onMounted } from 'vue';
import Konva from 'konva/lib';

const { width, height } = useWindowSize();
const MAX_WIDTH = 200;

const initial = () => {
	const stage = new Konva.Stage({ container: 'container', width: width.value, height: height.value });
	const layer = new Konva.Layer();
	stage.add(layer);

	const rect = new Konva.Rect({
		x: 160,
		y: 60,
		width: 100,
		height: 90,
		fill: 'red',
		name: 'rect',
		stroke: 'black',
		draggable: true,
	});

	layer.add(rect);

	const tr = new Konva.Transformer({
		boundBoxFunc: function (oldBoundBox, newBoundBox) {
			if (Math.abs(newBoundBox.width) > MAX_WIDTH) {
				return oldBoundBox;
			}

			return newBoundBox;
		},
		nodes: [rect],
	});
	layer.add(tr);
};

onMounted(() => {
	initial();
});
</script>

<template>
	<div id="container"></div>
</template>
