69 lines
1.3 KiB
TypeScript
69 lines
1.3 KiB
TypeScript
![]() |
import { Range } from "./Range";
|
||
|
import { Vector2 } from "./Vector2";
|
||
|
|
||
|
export class Box2
|
||
|
{
|
||
|
min:Vector2;
|
||
|
max:Vector2;
|
||
|
|
||
|
constructor( min:Vector2 = null, max:Vector2 = null )
|
||
|
{
|
||
|
this.min = min;
|
||
|
this.max = max;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
get size():Vector2
|
||
|
{
|
||
|
return this.max.clone().sub( this.min );
|
||
|
}
|
||
|
|
||
|
get center():Vector2
|
||
|
{
|
||
|
return this.max.clone().add( this.min ).multiply( 0.5 );
|
||
|
}
|
||
|
|
||
|
get rangeX():Range
|
||
|
{
|
||
|
return new Range( this.min.x, this.max.x );
|
||
|
}
|
||
|
|
||
|
get rangeY():Range
|
||
|
{
|
||
|
return new Range( this.min.y, this.max.y );
|
||
|
}
|
||
|
|
||
|
intersectsBox( box:Box2 )
|
||
|
{
|
||
|
return this.rangeX.overlaps( box.rangeX ) && this.rangeY.overlaps( box.rangeY );
|
||
|
}
|
||
|
|
||
|
translate( offset:Vector2 )
|
||
|
{
|
||
|
this.min.add( offset );
|
||
|
this.max.add( offset );
|
||
|
}
|
||
|
|
||
|
static fromClientRect( clientRect:ClientRect|DOMRect )
|
||
|
{
|
||
|
let min = new Vector2( clientRect.left, clientRect.top );
|
||
|
let max = new Vector2( clientRect.right, clientRect.bottom );
|
||
|
|
||
|
return new Box2( min, max );
|
||
|
}
|
||
|
|
||
|
|
||
|
static toDomRect( box:Box2 )
|
||
|
{
|
||
|
let size = box.size;
|
||
|
let domRect = new DOMRect( box.min.x, box.min.y, size.x, size.y );
|
||
|
return domRect;
|
||
|
}
|
||
|
|
||
|
static updatefromClientRect( box:Box2, clientRect:ClientRect|DOMRect )
|
||
|
{
|
||
|
box.min.set( clientRect.left, clientRect.top );
|
||
|
box.max.set( clientRect.right, clientRect.bottom );
|
||
|
}
|
||
|
}
|