Skip to main content

Rect

APJS Script API reference for the Rect class.

TypeNameInterface Description
Variablesheight: number

Function: The height of the rectangle.

Variableswidth: number

Function: The width of the rectangle.

Variablesx: number

Function: The x-coordinate of the rectangle's position.

Variablesy: number

Function: The y-coordinate of the rectangle's position.

Functionsconstructor(x?: number, y?: number, width?: number, height?: number)

Parameters

x: - The x-coordinate of the rectangle or another Rect instance to clone.

y: - The y-coordinate of the rectangle. Defaults to 0 if not provided.

width: - The width of the rectangle. Defaults to 0 if not provided.

height: - The height of the rectangle. Defaults to 0 if not provided.

Functionsclone(): 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.

Functionsequals(other: Rect): boolean

Function: Determines if this rectangle is equal to another rectangle.

Parameters

other: - The rectangle to compare with.

Returns Whether the rectangles are equal.

FunctionstoString(): 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);
}
}
Copyright © 2026 TikTok. All rights reserved.
About TikTokHelp CenterCareersContactLegalTerms of ServicePrivacy PolicyCookies