# XrImageTracking

Class · extends [`EventHandler`](https://api.playcanvas.com/engine/classes/EventHandler.md) · category: XR

Source: https://github.com/playcanvas/engine/blob/f059dc005842f76e052cdf44d8370c8c7ec475ac/src/framework/xr/xr-image-tracking.js#L17

Image Tracking provides the ability to track real world images using provided image samples and
their estimated sizes. The underlying system will assume that the tracked image can move and
rotate in the real world and will try to provide transformation estimates and its tracking
state.

## Accessors

### available

```ts
get available(): boolean
```

True if Image Tracking is available. This information is only available when the
XR session has started, and will be true if image tracking is supported and
images were provided and they have been processed successfully.

### images

```ts
get images(): XrTrackedImage[]
```

List of [XrTrackedImage](https://api.playcanvas.com/engine/classes/XrTrackedImage.md) that contain tracking information.

### supported

```ts
get supported(): boolean
```

True if Image Tracking is supported.

## Methods

### add

```ts
add(image: Blob | ImageBitmap | HTMLCanvasElement | HTMLImageElement | SVGImageElement | HTMLVideoElement | ImageData, width: number): XrTrackedImage | null
```

Add an image for image tracking. A width can also be provided to help the underlying system
estimate the appropriate transformation. Modifying the tracked images list is only possible
before an AR session is started.

**Parameters**

- `image` (`Blob | ImageBitmap | HTMLCanvasElement | HTMLImageElement | SVGImageElement | HTMLVideoElement | ImageData`): Image that is matching real world image as close as possible. Resolution of images should be
  at least 300x300. High resolution does _not_ improve tracking performance. The color of the
  image is irrelevant, so grayscale images can be used. Images with too many geometric
  features or repeating patterns will reduce tracking stability.
- `width` (`number`): Width (in meters) of image in the real world. Providing this value
  as close to the real value will improve tracking quality.

**Returns** [`XrTrackedImage`](https://api.playcanvas.com/engine/classes/XrTrackedImage.md) `| null`: Tracked image object that will contain tracking information.
Returns null if image tracking is not supported or if the XR manager is not active.

**Example**

```ts
// image of a book cover that has width of 20cm (0.2m)
app.xr.imageTracking.add(bookCoverImg, 0.2);
```

### remove

```ts
remove(trackedImage: XrTrackedImage): void
```

Remove an image from image tracking.

**Parameters**

- `trackedImage` ([`XrTrackedImage`](https://api.playcanvas.com/engine/classes/XrTrackedImage.md)): Tracked image to be removed. Modifying the tracked
  images list is only possible before an AR session is started.

## Events

### EVENT_ERROR

```ts
static EVENT_ERROR: string = 'error'
```

Fired when the XR session is started, but image tracking failed to process the provided
images. The handler is passed the Error object.

**Example**

```ts
app.xr.imageTracking.on('error', (err) => {
    console.error(err.message);
});
```

## Inherited from [EventHandler](https://api.playcanvas.com/engine/classes/EventHandler.md)

- `fire(name: string, arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any, arg6?: any, arg7?: any, arg8?: any): EventHandler`
- `hasEvent(name: string): boolean`
- `off(name?: string, callback?: HandleEventCallback, scope?: any): EventHandler`
- `on(name: string, callback: HandleEventCallback, scope?: any): EventHandle`
- `once(name: string, callback: HandleEventCallback, scope?: any): EventHandle`
