# Image Tracking

Image tracking finds images that you provide, such as a poster, a product label or a page of a book, in the real world, and follows their position and rotation. Use it to put content on a printed page, or to start an experience when the user points their phone at a poster.

Image tracking is experimental. Only Chrome on Android phones implements it, behind the **WebXR Incubations** flag (`chrome://flags/#webxr-incubations`), so users can't use it without changing their browser's settings. See [Platforms](https://developer.playcanvas.com/user-manual/xr/platforms.md#ar-features). To track images in any mobile browser today, use a [third-party framework](https://developer.playcanvas.com/user-manual/xr/ar/third-party-frameworks.md).

## Providing Images

Give the engine the images to track before the session starts, with each one's real-world width in meters. `app.xr.imageTracking.add()` returns an [`XrTrackedImage`](https://api.playcanvas.com/engine/classes/XrTrackedImage.html), which follows that image during the session:

```javascript
// A poster that is 40 cm wide
const poster = new Image();
poster.src = 'poster.jpg';
await poster.decode();

const trackedPoster = app.xr.imageTracking.add(poster, 0.4);
```

The image can be an `<img>`, a `<canvas>`, a video frame, an `ImageBitmap`, a `Blob` or `ImageData`. `add()` returns `null` while a session is running, or when the browser doesn't implement image tracking, which `app.xr.imageTracking.supported` tells you. `app.xr.imageTracking.images` lists the images, and `remove()` removes one, also only between sessions.

Then ask for image tracking when you start the session:

```javascript
camera.camera.startXr(XRTYPE_AR, XRSPACE_LOCALFLOOR, {
    imageTracking: true
});
```

When the session starts, the device checks each image. `trackedPoster.trackable` is `false` for an image it can't track, and `app.xr.imageTracking` fires `error` if it can't process the images at all. `available` is `true` once the images are ready.

## Choosing Images

How well an image tracks depends on the image:

- **Detail.** An image with plenty of detail and contrast, spread across all of it, tracks well. Large areas of flat color, repeating patterns and text alone track badly.
- **Resolution.** 300 × 300 pixels or more. A higher resolution doesn't track any better.
- **Color.** The color doesn't matter, so grayscale images are smaller to download.
- **Width.** Measure the printed image. A wrong width puts content at the wrong distance until the device has measured the image itself.

## Following an Image

A tracked image's `getPosition()` and `getRotation()` return its pose in [tracking space](https://developer.playcanvas.com/user-manual/xr/sessions.md#tracking-space). Its local Y axis points out of the image's surface, toward the viewer. Make what follows it a child of the [rig](https://developer.playcanvas.com/user-manual/xr/sessions.md#the-camera-and-its-rig), and set its local position and rotation every frame while the image is tracked:

```javascript
const model = new Entity('model');
model.addComponent('render', { type: 'box' });
model.setLocalScale(0.1, 0.1, 0.1);
model.enabled = false;
rig.addChild(model);

trackedPoster.on('tracked', () => {
    model.enabled = true;
});

trackedPoster.on('untracked', () => {
    model.enabled = false;
});

app.on('update', () => {
    if (!trackedPoster.tracking) return;
    model.setLocalPosition(trackedPoster.getPosition());
    model.setLocalRotation(trackedPoster.getRotation());
    // Stand the box on the poster
    model.translateLocal(0, 0.05, 0);
});
```

A tracked image fires `tracked` when the device finds it and `untracked` when it loses it, and `tracking` says which. While the image is out of view, the device can still report its last pose, assuming that it hasn't moved. Then `emulated` is `true`, so you can keep content in place on a poster on a wall, while hiding content on a page that the user could have moved.

## See Also

- [Third-Party Frameworks](https://developer.playcanvas.com/user-manual/xr/ar/third-party-frameworks.md) - Image tracking in browsers without WebXR
- [WebXR: AR Image Tracking](https://developer.playcanvas.com/tutorials/webxr-ar-image-tracking/) - A tutorial that tracks an image in the Editor
- [XrImageTracking](https://api.playcanvas.com/engine/classes/XrImageTracking.html) and [XrTrackedImage](https://api.playcanvas.com/engine/classes/XrTrackedImage.html) - API reference
