* 事件代理 * @param move 移动事件处理函数 * @param orientation 陀螺仪事件处理函数
(
move: (left: number, top: number) => void,
orientation: (beta: number, gamma: number) => void
)
| 371 | * @param orientation 陀螺仪事件处理函数 |
| 372 | */ |
| 373 | private eventProxy( |
| 374 | move: (left: number, top: number) => void, |
| 375 | orientation: (beta: number, gamma: number) => void |
| 376 | ): void { |
| 377 | const { eventElem } = this.options |
| 378 | let handleOrientation: (e: DeviceOrientationEvent) => void |
| 379 | |
| 380 | if (orientationSupport) { |
| 381 | handleOrientation = (e) => { |
| 382 | if (this.isPaused || isNull(e.beta)) return |
| 383 | |
| 384 | // 转换 beta 范围 [-180, 180] 成 [-90, 90] |
| 385 | orientation(Math.min(Math.max(e.beta!, -90), 90), e.gamma!) |
| 386 | } |
| 387 | |
| 388 | window.addEventListener('deviceorientation', handleOrientation) |
| 389 | } |
| 390 | |
| 391 | const handleMove = (e: MouseEvent) => { |
| 392 | if (this.isPaused) return |
| 393 | |
| 394 | let left = e.pageX |
| 395 | let top = e.pageY |
| 396 | |
| 397 | const offset = this.getEventElemOffset() |
| 398 | if (offset) { |
| 399 | left -= offset.left |
| 400 | top -= offset.top |
| 401 | } |
| 402 | move(left, top) |
| 403 | } |
| 404 | |
| 405 | eventElem!.addEventListener('mousemove', handleMove as EventListener) |
| 406 | |
| 407 | // 实例销毁时移除绑定的事件 |
| 408 | this.onDestroy(() => { |
| 409 | window.removeEventListener('deviceorientation', handleOrientation) |
| 410 | eventElem!.removeEventListener('mousemove', handleMove as EventListener) |
| 411 | }) |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * 鼠标位置事件,根据鼠标的坐标将范围内的粒子连接起来 |
no test coverage detected