Rect
APJS Script API reference for the Rect class.
| Type | Name | Interface Description |
|---|---|---|
| Variables | height: number | • Function: The height of the rectangle. |
| Variables | width: number | • Function: The width of the rectangle. |
| Variables | x: number | • Function: The x-coordinate of the rectangle's position. |
| Variables | y: number | • Function: The y-coordinate of the rectangle's position. |
| Functions | constructor(x?: number, y?: number, width?: number, height?: number) | Parameters • • • • |
| Functions | clone(): Rect | • Function: Creates and returns a deep copy of the current rectangle. Returns A new instance of Rect with the same properties as the original. |
| Functions | equals(other: Rect): boolean | • Function: Determines if this rectangle is equal to another rectangle. Parameters • Returns Whether the rectangles are equal. |
| Functions | toString(): string | • Function: Returns a string representation of the rectangle. Returns A formatted string describing the rectangle. |
Examples
constructor(x?: number, y?: number, width?: number, height?: number)
let obj = new APJS.Rect();
Use Case
Move a 2D object by dragging it with finger using touch delta in screen space
@component()
export class DragMovement extends APJS.BasicScriptComponent {
private isDragging: boolean = false;
private screenTransform: APJS.ScreenTransform;
private onTouchEvent = (event: APJS.IEvent) => {
const touchInfo = event.args[0] as APJS.TouchData;
const touchPos = new APJS.Vector2f(touchInfo.position.x, 1.0 - touchInfo.position.y);
const screenPos = touchPos.subtract(new APJS.Vector2f(0.5, 0.5)).multiply(new APJS.Vector2f(720, 1280));
if (touchInfo.phase === APJS.TouchPhase.Began) {
this.isDragging = true;
} else if (touchInfo.phase === APJS.TouchPhase.Moved && this.isDragging) {
if (this.screenTransform) {
const delta = touchInfo.delta;
const screenDelta = new APJS.Vector2f(delta.x * 720, -delta.y * 1280);
const currentOffset = this.screenTransform.offsets;
this.screenTransform.offsets = new APJS.Rect(
currentOffset.x + screenDelta.x,
currentOffset.y + screenDelta.y,
currentOffset.z + screenDelta.x,
currentOffset.w + screenDelta.y
);
}
} else if (touchInfo.phase === APJS.TouchPhase.Ended || touchInfo.phase === APJS.TouchPhase.Canceled) {
this.isDragging = false;
}
};
onStart(): void {
this.screenTransform = this.getSceneObject().getComponent("ScreenTransform") as APJS.ScreenTransform;
APJS.EventManager.getGlobalEmitter().on(APJS.EventType.Touch, this.onTouchEvent);
}
onDestroy(): void {
APJS.EventManager.getGlobalEmitter().off(APJS.EventType.Touch, this.onTouchEvent);
}
}