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

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

const { width, height } = useWindowSize();

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

	// 创建文本
	const text = new Konva.Text({
		x: 10,
		y: 10,
		fontFamily: 'Calibri',
		fontSize: 20,
		text: '',
		fill: 'black',
	});
	layer.add(text);

	// 创建元素
	const circle = new Konva.Circle({
		x: stage.width() / 2,
		y: stage.height() / 2,
		radius: 70,
		fill: 'red',
		stroke: 'black',
		strokeWidth: 4,
	});
	layer.add(circle);

	// 添加监听事件
	circle.on('mouseover mousedown mouseup', function () {
		text.text('鼠标事件：mouseover mousedown mouseup');
	});
	circle.on('mouseout', function () {
		text.text('鼠标离开');
	});

	stage.add(layer);
};

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