From 5945edd41ff7c802a96876851c67128e83f82bc5 Mon Sep 17 00:00:00 2001 From: luoyifan Date: Fri, 6 Jun 2025 17:32:17 +0800 Subject: [PATCH] =?UTF-8?q?EsDragControl2=20=E6=8B=96=E6=8B=BD=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=99=A8=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/controls/EsDragControl2.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/core/controls/EsDragControl2.ts b/src/core/controls/EsDragControl2.ts index f727191..0bce29a 100644 --- a/src/core/controls/EsDragControl2.ts +++ b/src/core/controls/EsDragControl2.ts @@ -66,18 +66,20 @@ export default class DragControl2 implements IControls { * 清理拖拽状态 */ private cleanupDrag(): void { - this.viewport.renderer.domElement.style.cursor = 'auto' - this.isDragging = false - this.isPointerDown = false - this.dragStartMouse.set(NaN, NaN) - this.removeShadows() + if (this.domElement) { + this.domElement.style.cursor = 'auto' + this.isDragging = false + this.isPointerDown = false + this.dragStartMouse.set(NaN, NaN) + this.removeShadows() + } } /** * 获取当前鼠标坐标(归一化设备坐标) */ private getMousePosition(clientX: number, clientY: number): THREE.Vector2 { - const rect = this.viewport.renderer.domElement.getBoundingClientRect() + const rect = this.domElement.getBoundingClientRect() return new THREE.Vector2( ((clientX - rect.left) / rect.width) * 2 - 1, ((clientY - rect.top) / rect.height) * -2 + 1 @@ -187,12 +189,12 @@ export default class DragControl2 implements IControls { * pointermove 事件处理 */ private onPointerMove = (event: PointerEvent): void => { - if (!this.enabled) return + if (!this.enabled || !this.domElement) return if (!isNaN(this.dragStartMouse.x) && !isNaN(this.dragStartMouse.y) && this.dragShadows) { this.isDragging = true // 更新鼠标样式 - this.viewport.renderer.domElement.style.cursor = 'grabbing' + this.domElement.style.cursor = 'grabbing' // 更新阴影位置(锁定 Y 轴) this.updateShadows(new THREE.Vector2(CurrentMouseInfo.x, CurrentMouseInfo.z)) @@ -202,9 +204,9 @@ export default class DragControl2 implements IControls { const mouse = this.getMousePosition(event.clientX, event.clientY) const intersected = this.getIntersectedDraggableObject(mouse) if (intersected) { - this.viewport.renderer.domElement.style.cursor = 'grab' + this.domElement.style.cursor = 'grab' } else { - this.viewport.renderer.domElement.style.cursor = 'auto' + this.domElement.style.cursor = 'auto' } } } @@ -269,9 +271,10 @@ export default class DragControl2 implements IControls { * 取消当前拖拽状态 */ cancelDrag(): void { + if (!this.domElement) return if (this.isDragging || this.isPointerDown) { this.cleanupDrag() - this.viewport.renderer.domElement.style.cursor = 'auto' + this.domElement.style.cursor = 'auto' this.viewport.controls.enabled = true } }