export interface Vec2 { x : number ; y : number ; } export interface Vec3 { x : number ; y : number ; z : number ; } param event export function getEventClientPos ( event ) { return { x : event . clientX , y : event . clientY , } ; } /** 判断点是否在矩形内 */ export function pointInRect ( p , rect ) { const [ r1 , r2 ] = rect ; const c1 = ( p . x - r1 . x ) * ( p . x - r2 . x ) ; const c2 = ( p . y - r1 . y ) * ( p . y - r2 . y ) ; return c1 <= 0 && c2 <= 0 ; } param vec param canvas export function getRelativePositionFromEvent ( vec , canvas ) { const rect = canvas . getBoundingClientRect ( ) as ClientRect ; const point = { x : vec . x - rect . left , y : vec . y - rect . top , } ; if ( point . x <= 0 || point . y <= 0 || point . x > rect . width || point . y > rect . height ) { return undefined ; } return point ; }