page: 📄 绑定事件

This commit is contained in:
bunny 2024-07-12 10:06:43 +08:00
parent d7a88e6fac
commit 1a1bb6893d
3 changed files with 115 additions and 1 deletions

View File

@ -1 +1 @@
{"version":1720656560503} {"version":1720748515458}

View File

@ -0,0 +1,52 @@
<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>

View File

@ -0,0 +1,62 @@
<template>
<div class="container">
<button type="button" class="btn btn-primary" @click="onRemoveEvent">移出事件</button>
</div>
<div id="container"></div>
</template>
<script setup lang="ts">
import { useWindowSize } from '@vueuse/core';
import { onMounted, ref } from 'vue';
import Konva from 'konva/lib';
import { Text } from 'konva/lib/shapes/Text';
import { Circle } from 'konva/lib/shapes/Circle';
const { width, height } = useWindowSize();
const circle = ref<Circle>();
const text = ref<Text>();
const initial = () => {
const stage = new Konva.Stage({ container: 'container', width: width.value, height: height.value });
const layer = new Konva.Layer({ draggable: true });
//
text.value = new Konva.Text({
x: 10,
y: 10,
fontFamily: 'Calibri',
fontSize: 20,
text: '',
fill: 'black',
});
layer.add(text.value);
//
circle.value = new Konva.Circle({
x: stage.width() / 2,
y: stage.height() / 2 + 10,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
});
layer.add(circle.value);
//
circle.value?.on('click', function () {
text.value?.text('点击了');
});
stage.add(layer);
};
//
const onRemoveEvent = () => {
circle.value?.off('click');
text.value?.text('移出事件');
};
onMounted(() => {
initial();
});
</script>