feat: right mouse down to drag canvas
This commit is contained in:
parent
f1e2dbd9d8
commit
d18cf82813
|
@ -2300,9 +2300,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/brace-expansion": {
|
"node_modules/brace-expansion": {
|
||||||
"version": "2.0.1",
|
"version": "2.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.2.tgz",
|
||||||
"integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
|
"integrity": "sha512-Jt0vHyM+jmUBqojB7E1NIYadt0vI0Qxjxd2TErW94wDz+E2LAm5vKMXXwg6ZZBTHPuUlDgQHKXvjGBdfcF1ZDQ==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<template>
|
<template>
|
||||||
<div class="w-full h-full relative">
|
<div class="w-full h-full relative" @contextmenu.prevent>
|
||||||
<v-stage
|
<v-stage
|
||||||
class="h-full w-full"
|
class="h-full w-full"
|
||||||
ref="stageRef"
|
ref="stageRef"
|
||||||
|
@ -9,6 +9,7 @@
|
||||||
@mouseup="handleMouseUp"
|
@mouseup="handleMouseUp"
|
||||||
@wheel="handleWheel"
|
@wheel="handleWheel"
|
||||||
@click="handleStageClick"
|
@click="handleStageClick"
|
||||||
|
@contextmenu="handleContextMenu"
|
||||||
>
|
>
|
||||||
<v-layer ref="layerRef">
|
<v-layer ref="layerRef">
|
||||||
<template
|
<template
|
||||||
|
@ -200,6 +201,9 @@ const stageRef = useTemplateRef<VStage>("StageRef");
|
||||||
const isDragging = ref(false);
|
const isDragging = ref(false);
|
||||||
const dragItemId = ref<string | null>(null);
|
const dragItemId = ref<string | null>(null);
|
||||||
|
|
||||||
|
const isRightDragging = ref(false);
|
||||||
|
const lastPointerPosition = ref<{ x: number; y: number } | null>(null);
|
||||||
|
|
||||||
const isSelecting = ref(false);
|
const isSelecting = ref(false);
|
||||||
const selectedIds = ref<string[]>([]);
|
const selectedIds = ref<string[]>([]);
|
||||||
const selectionRectangle = reactive({
|
const selectionRectangle = reactive({
|
||||||
|
@ -303,9 +307,28 @@ function handleStageClick(e: { target: any; evt: MouseEvent }) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleContextMenu(e: Event) {
|
||||||
|
// 防止默认右键菜单弹出
|
||||||
|
e.preventDefault();
|
||||||
|
}
|
||||||
|
|
||||||
function handleMouseDown(e: Event) {
|
function handleMouseDown(e: Event) {
|
||||||
if (isNull(e.target)) return;
|
if (isNull(e.target)) return;
|
||||||
const target = e.target as unknown as Konva.Container;
|
const target = e.target as unknown as Konva.Container;
|
||||||
|
const stage = target.getStage();
|
||||||
|
|
||||||
|
// 获取鼠标事件信息
|
||||||
|
const mouseEvent = (e as any).evt as MouseEvent;
|
||||||
|
|
||||||
|
// 如果是右键按下,开始右键拖拽
|
||||||
|
if (mouseEvent.button === 2) {
|
||||||
|
isRightDragging.value = true;
|
||||||
|
const pos = stage?.getPointerPosition();
|
||||||
|
if (pos) {
|
||||||
|
lastPointerPosition.value = { x: pos.x, y: pos.y };
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// do nothing if we mousedown on any shape
|
// do nothing if we mousedown on any shape
|
||||||
if ((e.target as unknown) !== target.getStage()) {
|
if ((e.target as unknown) !== target.getStage()) {
|
||||||
|
@ -314,7 +337,6 @@ function handleMouseDown(e: Event) {
|
||||||
|
|
||||||
// start selection rectangle
|
// start selection rectangle
|
||||||
isSelecting.value = true;
|
isSelecting.value = true;
|
||||||
const stage = target.getStage();
|
|
||||||
const pos = stage?.getPointerPosition();
|
const pos = stage?.getPointerPosition();
|
||||||
if (!isNull(pos) && !isUndefined(pos) && !isNull(stage)) {
|
if (!isNull(pos) && !isUndefined(pos) && !isNull(stage)) {
|
||||||
// Convert pointer position to relative coordinates considering scale and position
|
// Convert pointer position to relative coordinates considering scale and position
|
||||||
|
@ -334,13 +356,31 @@ function handleMouseDown(e: Event) {
|
||||||
function handleMouseMove(e: Event) {
|
function handleMouseMove(e: Event) {
|
||||||
if (isNull(e.target)) return;
|
if (isNull(e.target)) return;
|
||||||
const target = e.target as unknown as Konva.Container;
|
const target = e.target as unknown as Konva.Container;
|
||||||
|
const stage = target.getStage();
|
||||||
|
|
||||||
|
// 如果正在右键拖拽,移动画布
|
||||||
|
if (isRightDragging.value && lastPointerPosition.value && stage) {
|
||||||
|
const pos = stage.getPointerPosition();
|
||||||
|
if (pos) {
|
||||||
|
const dx = pos.x - lastPointerPosition.value.x;
|
||||||
|
const dy = pos.y - lastPointerPosition.value.y;
|
||||||
|
|
||||||
|
const currentPos = stage.position();
|
||||||
|
stage.position({
|
||||||
|
x: currentPos.x + dx,
|
||||||
|
y: currentPos.y + dy,
|
||||||
|
});
|
||||||
|
|
||||||
|
lastPointerPosition.value = { x: pos.x, y: pos.y };
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// do nothing if we didn't start selection
|
// do nothing if we didn't start selection
|
||||||
if (!isSelecting.value) {
|
if (!isSelecting.value) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const stage = target.getStage();
|
|
||||||
const pos = stage?.getPointerPosition();
|
const pos = stage?.getPointerPosition();
|
||||||
if (!isNull(pos) && !isUndefined(pos) && !isNull(stage)) {
|
if (!isNull(pos) && !isUndefined(pos) && !isNull(stage)) {
|
||||||
// Convert pointer position to relative coordinates considering scale and position
|
// Convert pointer position to relative coordinates considering scale and position
|
||||||
|
@ -354,7 +394,15 @@ function handleMouseMove(e: Event) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleMouseUp() {
|
function handleMouseUp(e: Event) {
|
||||||
|
// 如果是右键释放,结束右键拖拽
|
||||||
|
const mouseEvent = (e as any).evt as MouseEvent;
|
||||||
|
if (mouseEvent.button === 2 && isRightDragging.value) {
|
||||||
|
isRightDragging.value = false;
|
||||||
|
lastPointerPosition.value = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// do nothing if we didn't start selection
|
// do nothing if we didn't start selection
|
||||||
if (!isSelecting.value) {
|
if (!isSelecting.value) {
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in New Issue