HTML5 Canvas 简单拖动边界教程
如需限制使用 Konva 拖放的图形的移动范围,
可以使用 dragmove 事件,并在事件处理程序中重新设置拖放位置。
可以使用此事件以多种方式限制拖放时的移动。例如,只允许水平、垂直、对角线或径向移动,甚至可以将节点 限制在方框、圆形或其他路径内。
shape.on('dragmove', () => {
// lock position of the shape on x axis
// keep y position as is
shape.x(0);
});
提示:你可以使用 shape.absolutePosition() 方法获取或设置节点的绝对位置,而不是使用相对的 x 和 y。
操作说明: 拖放横向文本,并观察它只能沿水平方向移动。 拖放纵向文本,并观察它只能沿垂直方向移动。
- 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 horizontalText = new Konva.Text({
x: 50,
y: 50,
text: 'Drag me horizontally',
fontSize: 16,
draggable: true,
fill: 'black',
});
horizontalText.on('dragmove', function () {
// horizontal only
this.y(50);
});
const verticalText = new Konva.Text({
x: 200,
y: 50,
text: 'Drag me vertically',
fontSize: 16,
draggable: true,
fill: 'black',
});
verticalText.on('dragmove', function () {
// vertical only
this.x(200);
});
layer.add(horizontalText);
layer.add(verticalText);
import { Stage, Layer, Text } from 'react-konva';
import { useState } from 'react';
const App = () => {
const [horizontalPosition, setHorizontalPosition] = useState({ x: 50, y: 50 });
const [verticalPosition, setVerticalPosition] = useState({ x: 200, y: 50 });
const handleHorizontalDragMove = (e) => {
setHorizontalPosition({ x: e.target.x(), y: 50 });
};
const handleVerticalDragMove = (e) => {
setVerticalPosition({ x: 200, y: e.target.y() });
};
return (
<Stage width={window.innerWidth} height={window.innerHeight}>
<Layer>
<Text
x={horizontalPosition.x}
y={horizontalPosition.y}
text="Drag me horizontally"
fontSize={16}
draggable
fill="black"
onDragMove={handleHorizontalDragMove}
/>
<Text
x={verticalPosition.x}
y={verticalPosition.y}
text="Drag me vertically"
fontSize={16}
draggable
fill="black"
onDragMove={handleVerticalDragMove}
/>
</Layer>
</Stage>
);
};
export default App;
<template>
<v-stage :config="stageSize">
<v-layer>
<v-text
:config="horizontalTextConfig"
@dragmove="handleHorizontalDragMove"
/>
<v-text
:config="verticalTextConfig"
@dragmove="handleVerticalDragMove"
/>
</v-layer>
</v-stage>
</template>
<script setup>
const stageSize = {
width: window.innerWidth,
height: window.innerHeight
};
const horizontalTextConfig = {
x: 50,
y: 50,
text: 'Drag me horizontally',
fontSize: 16,
draggable: true,
fill: 'black'
};
const verticalTextConfig = {
x: 200,
y: 50,
text: 'Drag me vertically',
fontSize: 16,
draggable: true,
fill: 'black'
};
const handleHorizontalDragMove = (e) => {
e.target.y(50);
};
const handleVerticalDragMove = (e) => {
e.target.x(200);
};
</script>