import { Vec2 , Vec3 } from '@turbox3d/shared' ; import { CoordinateType } from './type' ; export abstract class CoordinateController { private canvas : HTMLCanvasElement ; constructor ( canvas ) { this . canvas = canvas ; } /**
     * 将坐标进行转化
     */ transform ( point , type ) { switch ( type ) { case CoordinateType . ScreenToCanvas : return this . screenToCanvas ( point ) ; case CoordinateType . ScreenToScene : return this . screenToScene ( point ) ; case CoordinateType . CanvasToScreen : return this . canvasToScreen ( point ) ; case CoordinateType . CanvasToScene : return this . canvasToScene ( point ) ; case CoordinateType . SceneToScreen : return this . sceneToScreen ( point ) ; case CoordinateType . SceneToCanvas : return this . sceneToCanvas ( point ) ; default : return point ; } } private screenToCanvas ( point ) { const { x , y } = this . getRect ( ) ; return { x : point . x - x , y : point . y - y , } ; } private canvasToScreen ( point ) { const { x , y } = this . getRect ( ) ; return { x : point . x + x , y : point . y + y , } ; } private canvasToScene ( point ) { return this . canvasToSceneImpl ( point ) ; } private sceneToCanvas ( point ) { return this . sceneToCanvasImpl ( point ) ; } /** 画布屏幕坐标转场景世界坐标的实现 */ abstract canvasToSceneImpl ( point ) ; /** 场景世界坐标转画布屏幕坐标的实现 */ abstract sceneToCanvasImpl ( point ) ; private screenToScene ( point ) { return this . canvasToScene ( this . screenToCanvas ( point ) ) ; } private sceneToScreen ( point ) { return this . canvasToScreen ( this . sceneToCanvas ( point ) ) ; } private getRect ( ) { return this . canvas . getBoundingClientRect ( ) ; } }