使用 Konva 实现 HTML5 Canvas 事件委托
要使用 Konva 获取事件目标,可以访问事件对象的 target 属性。
这对事件委托特别有用。我们可以将事件处理程序绑定到父节点,
并监听其子节点上发生的事件。
操作说明:单击星形,观察图层绑定的事件是否正确识别被单击的图形。
- Vanilla
- React
- Vue
import Konva from 'konva';
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
});
const layer = new Konva.Layer();
stage.add(layer);
const text = new Konva.Text({
x: 10,
y: 10,
fontFamily: 'Calibri',
fontSize: 24,
text: '',
fill: 'black',
});
layer.add(text);
const star = new Konva.Star({
x: stage.width() / 2,
y: stage.height() / 2,
numPoints: 5,
innerRadius: 40,
outerRadius: 70,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
});
layer.add(star);
// add event delegation
layer.on('click', function (evt) {
const shape = evt.target;
text.text('click on ' + shape.getClassName());
});
import { Stage, Layer, Star, Text } from 'react-konva';
import { useState } from 'react';
const App = () => {
const [message, setMessage] = useState('');
const handleLayerClick = (e) => {
const shape = e.target;
setMessage('click on ' + shape.getClassName());
};
return (
<Stage width={window.innerWidth} height={window.innerHeight}>
<Layer onClick={handleLayerClick}>
<Text
x={10}
y={10}
fontFamily="Calibri"
fontSize={24}
text={message}
fill="black"
/>
<Star
x={window.innerWidth / 2}
y={window.innerHeight / 2}
numPoints={5}
innerRadius={40}
outerRadius={70}
fill="red"
stroke="black"
strokeWidth={4}
/>
</Layer>
</Stage>
);
};
export default App;
<template>
<v-stage :config="stageSize">
<v-layer @click="handleLayerClick">
<v-text :config="textConfig" />
<v-star :config="starConfig" />
</v-layer>
</v-stage>
</template>
<script setup>
import { ref, computed } from 'vue';
const message = ref('');
const stageSize = {
width: window.innerWidth,
height: window.innerHeight
};
const textConfig = computed(() => ({
x: 10,
y: 10,
fontFamily: 'Calibri',
fontSize: 24,
text: message.value,
fill: 'black'
}));
const starConfig = {
x: window.innerWidth / 2,
y: window.innerHeight / 2,
numPoints: 5,
innerRadius: 40,
outerRadius: 70,
fill: 'red',
stroke: 'black',
strokeWidth: 4
};
const handleLayerClick = (e) => {
const shape = e.target;
message.value = 'click on ' + shape.getClassName();
};
</script>