2025-03-25 06:42:27 +00:00
|
|
|
import { Box2 } from "../geometry/Box2";
|
|
|
|
import { Vector2 } from "../geometry/Vector2";
|
|
|
|
|
|
|
|
export class DOMHitTest
|
|
|
|
{
|
|
|
|
static get scrollOffset():Vector2
|
|
|
|
{
|
|
|
|
return new Vector2( window.scrollX, window.scrollY );
|
|
|
|
}
|
|
|
|
|
|
|
|
static getPageRect( e:Element ):ClientRect
|
|
|
|
{
|
|
|
|
let pageBox = this.getPageBox( e );
|
|
|
|
|
|
|
|
return Box2.toDomRect( pageBox );
|
|
|
|
}
|
|
|
|
|
|
|
|
static other()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static isPointerOver( me:MouseEvent|TouchEvent, e:Element )
|
|
|
|
{
|
|
|
|
if ( ! me || ! e )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let relative = DOMHitTest.getNormalizedPointerPosition( me, e );
|
|
|
|
|
|
|
|
if ( relative.x < 0 || relative.x > 1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( relative.y < 0 || relative.y > 1 )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static getPageBox( e:Element ):Box2
|
|
|
|
{
|
|
|
|
let pageBox = Box2.fromClientRect( e.getBoundingClientRect() );
|
|
|
|
|
|
|
|
pageBox.translate( this.scrollOffset );
|
|
|
|
|
|
|
|
return pageBox;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static _lastPageX:number = null;
|
|
|
|
private static _lastPageY:number = null;
|
|
|
|
|
|
|
|
static getPointerPosition( e:MouseEvent|TouchEvent ):Vector2
|
|
|
|
{
|
|
|
|
let isMouseEvent = /mouse|click/.test( e.type );
|
|
|
|
console.log( e.type );
|
|
|
|
let mouseEvent = isMouseEvent ? e as MouseEvent : null;
|
|
|
|
let touchEvent = isMouseEvent ? null : ( e as TouchEvent );
|
|
|
|
let pageX = isMouseEvent ? mouseEvent.pageX : touchEvent.touches.length === 0 ? null : touchEvent.touches[ 0 ].pageX;
|
|
|
|
let pageY = isMouseEvent ? mouseEvent.pageY : touchEvent.touches.length === 0 ? null : touchEvent.touches[ 0 ].pageY;
|
|
|
|
|
|
|
|
// if ( isMouseEvent && CustomMouse.active )
|
|
|
|
// {
|
|
|
|
// pageX = CustomMouse.x;
|
|
|
|
// pageY = CustomMouse.y;
|
|
|
|
// }
|
|
|
|
|
|
|
|
if ( pageX === null )
|
|
|
|
{
|
|
|
|
pageX = DOMHitTest._lastPageX === undefined ? window.innerWidth / 2 : DOMHitTest._lastPageX;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( pageY === null )
|
|
|
|
{
|
|
|
|
pageY = DOMHitTest._lastPageY === undefined ? window.innerHeight / 2 : DOMHitTest._lastPageY;
|
|
|
|
}
|
|
|
|
|
|
|
|
DOMHitTest._lastPageX = pageX;
|
|
|
|
DOMHitTest._lastPageY = pageY;
|
|
|
|
|
|
|
|
return new Vector2( pageX, pageY );
|
|
|
|
}
|
|
|
|
|
|
|
|
static getRelativePointerPosition( e:MouseEvent|TouchEvent, element:Element )
|
|
|
|
{
|
|
|
|
let position = this.getPointerPosition( e );
|
|
|
|
let pageRect = this.getPageBox( element );
|
|
|
|
|
|
|
|
return position.sub( pageRect.min );
|
|
|
|
}
|
|
|
|
|
|
|
|
static getRelativeWindowPosition( e:MouseEvent|TouchEvent )
|
|
|
|
{
|
|
|
|
let position = this.getPointerPosition( e );
|
2025-09-06 11:33:04 +00:00
|
|
|
let windowBox = Box2.create( new Vector2( 0, 0 ), new Vector2( window.innerWidth, window.innerHeight ) );
|
2025-03-25 06:42:27 +00:00
|
|
|
|
|
|
|
position.sub( windowBox.min );
|
|
|
|
let size = windowBox.size;
|
|
|
|
position.x /= size.x;
|
|
|
|
position.y /= size.y;
|
|
|
|
|
|
|
|
return position;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static getNormalizedPointerPosition( e:MouseEvent|TouchEvent, element:Element )
|
|
|
|
{
|
|
|
|
let position = this.getPointerPosition( e );
|
|
|
|
let pageRect = this.getPageBox( element );
|
|
|
|
|
|
|
|
position.sub( pageRect.min );
|
|
|
|
let size = pageRect.size;
|
|
|
|
position.x /= size.x;
|
|
|
|
position.y /= size.y;
|
|
|
|
|
|
|
|
return position;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static getNormalizedPosition( position:Vector2, element:Element )
|
|
|
|
{
|
|
|
|
let pageRect = this.getPageBox( element );
|
|
|
|
|
|
|
|
position.sub( pageRect.min );
|
|
|
|
let size = pageRect.size;
|
|
|
|
position.x /= size.x;
|
|
|
|
position.y /= size.y;
|
|
|
|
|
|
|
|
return position;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|