Image
A component for rendering images.
| Type | Name | Interface Description |
|---|---|---|
| Variables | color: Color | • Function: The base color of the current material. |
| Variables | flipX: boolean | • Function: Determine whether to flip the image horizontally |
| Variables | flipY: boolean | • Function: Determine whether to flip the image vertically |
| Variables | opacity: number | • Function: The opacity of the image. |
| Variables | pivot: Vector2f | • Function: The pivot point of the Image. Typical values are in the range [0, 1]. Values outside that range are still passed through and place the pivot outside the image rect. |
| Variables | size: Vector2f | • Function: The size of the image, takes effect according to the stretch mode |
| Variables | stretchMode: StretchMode | • Function: Determine how the image is stretched to the required size |
| Variables | texture: Texture | • Function: The main texture of the Image component. |
| Functions | constructor() | |
| Functions | getMaterialProperty(key: string): number | Vector2f | Vector3f | Vector4f | Texture | Matrix4x4f | • Function: Gets the current Image material property value for the specified key. For general material properties, Parameters • Returns The current value of the specified material property. |
| Functions | setMaterialProperty(key: string, arg1: number | Vector2f | Vector3f | Vector4f | Texture | Matrix4x4f): void | • Function: Sets a material property for the current Image. For general material properties, Parameters • • |
Examples
constructor()
let obj = new APJS.Image();
Use Case
Example 1 — Detect tap on specific 2D buttons using TouchUtils.isScreenPointOnImage for accurate hit-testing. Supports pressed/normal visual states and multiple buttons.
@component()
export class ButtonTapHandler extends APJS.BasicScriptComponent {
@serializeProperty
private btn1Obj: APJS.SceneObject;
@serializeProperty
private btn2Obj: APJS.SceneObject;
private btn1Image!: APJS.Image;
private btn2Image!: APJS.Image;
private initialized: boolean = false;
private touchCallback = (event: APJS.IEvent) => {
if (!this.initialized) return;
const touchInfo = event.args[0] as APJS.TouchData;
if (touchInfo.phase === APJS.TouchPhase.Began) {
if (APJS.TouchUtils.isScreenPointOnImage(touchInfo.position, this.btn1Image)) {
this.btn1Image.color = new APJS.Color(0.7, 0.7, 0.7, 1);
this.onButton1Tap();
} else if (APJS.TouchUtils.isScreenPointOnImage(touchInfo.position, this.btn2Image)) {
this.btn2Image.color = new APJS.Color(0.7, 0.7, 0.7, 1);
this.onButton2Tap();
}
} else if (touchInfo.phase === APJS.TouchPhase.Ended) {
this.btn1Image.color = new APJS.Color(1, 1, 1, 1);
this.btn2Image.color = new APJS.Color(1, 1, 1, 1);
}
};
onUpdate(dt: number): void {
if (!this.initialized && this.btn1Obj && this.btn2Obj) {
this.btn1Image = this.btn1Obj.getComponent("Image") as APJS.Image;
this.btn2Image = this.btn2Obj.getComponent("Image") as APJS.Image;
APJS.EventManager.getGlobalEmitter().on(APJS.EventType.Touch, this.touchCallback);
this.initialized = true;
}
}
private onButton1Tap(): void {
console.log("Button 1 tapped");
}
private onButton2Tap(): void {
console.log("Button 2 tapped");
}
onDestroy(): void {
APJS.EventManager.getGlobalEmitter().off(APJS.EventType.Touch, this.touchCallback);
}
}
Example 2 — Animated progress bar using Image filled mode with setMaterialProperty for health/loading display
@component()
export class ImageProgressBar extends APJS.BasicScriptComponent {
@serializeProperty
private progressBarObject: APJS.SceneObject;
@serializeProperty
private duration: number = 3.0;
private barImage: APJS.Image;
private elapsed: number = 0;
private filling: boolean = true;
onStart(): void {
this.barImage = this.progressBarObject.getComponent("Image") as APJS.Image;
if (this.barImage) {
// Horizontal fill from left
this.barImage.setMaterialProperty("_filledType", 0);
this.barImage.setMaterialProperty("_startPoint", 0.0);
this.barImage.setMaterialProperty("_filledRange", 0.0);
}
}
onUpdate(deltaTime: number): void {
if (!this.barImage) return;
this.elapsed += deltaTime;
const progress = Math.min(this.elapsed / this.duration, 1.0);
this.barImage.setMaterialProperty("_filledRange", this.filling ? progress : 1.0 - progress);
if (this.elapsed >= this.duration) {
this.elapsed = 0;
this.filling = !this.filling;
}
}
}