# Media Viewer API

Use the API for advanced control over the images, spins and videos in [Sirv Media Viewer](/help/sirv-media-viewer/).

Using an AI coding agent?

Install the [`sirv-media-viewer`](https://www.skills.sh/sirv/image-optimization-skill/sirv-media-viewer) skill, and your agent can add the viewer to your site. Ask in plain words, such as “Add a gallery with zoom and a [360 spin](/help/spin/getting-started/how-spin-files-are-created/) to the product page.” [See all 8 skills](/help/rest-api/sirv-skills-for-ai-agents/)

## Global API

| Method | Parameters | Description |
| --- | --- | --- |
| Sirv.start | (selector or element) | Starts Sirv instance(s) by DOM query selector, or by HTML element |
| Sirv.stop | (selector or element) | Stops Sirv instance(s) by DOM query selector, or by HTML element |
| Sirv.on | (event name) | Attach an event handler for Sirv event |
| Sirv.off | (event name) | Remove an event handler |
| Sirv.getInstance | (selector or element) | Get viewer or lazy image instance by DOM query selector, or by HTML element |
| Sirv.viewer.getInstance | (selector or element) | Get viewer instance by DOM query selector, or by HTML element |
| Sirv.lazyImage.getInstance | (selector or element) | Get lazy image instance by DOM query selector, or by HTML element |

Example code:

```html
<script>
// Start all Sirv instances on the page:
Sirv.start();

// Start Sirv instances that match the provided selector:
Sirv.start('.product-view .Sirv');

// Attach event handler
Sirv.on('viewer:beforeSlideIn', (nextItem) => {
    console.log(nextItem);
});
</script>
```

## Viewer API

| Method | Parameters | Description |
| --- | --- | --- |
| isReady |  | Check if a viewer instance is ready |
| isFullscreen |  | Check if a viewer instance is in fullscreen |
| fullscreen |  | Switch to full screen mode |
| next |  | Switch to next item |
| prev |  | Switch to previous item |
| jump | (index or item id) | Switch to a particular item by index or by id (data-id) |
| play | (delay) | Start viewer autoplay. If the delay argument is passed, it will override the slide.delay setting. |
| pause |  | Pause viewer autoplay |
| items | (filters) | Get viewer items. Filters: `{ enabled: true }` - only enabled items; `{ enabled: false }` - only disabled items. |
| itemsCount | (filters) | Get the number of viewer items. Filters: `{ enabled: true }` - only enabled items; `{ enabled: false }` - only disabled items. |
| child | (index or item id) | Get reference to a particular item by index or by id (data-id) |
| enableItem | (index or item id) | Enable a particular item by index or by id (data-id) |
| disableItem | (index or item id) | Disable a particular item by index or by id (data-id) |
| insertItem | (DOM element, index) | Add item to the viewer at specified index. If index omitted, add item to the end. |
| removeItem | (index or item id) | Remove item from the viewer by index or by id (data-id) |
| enableGroup | (name of group or array from names) | Enable a particular group by group name (data-group) |
| disableGroup | (name of group or array from names) | Disable a particular group by group name (data-group) |
| switchGroup | (name of group or array from names) | Disable enabled groups and enable particular group |
| sortItems | (array from component names or empty) | Sort slides by itemsOrder option or array from component names |

Example script to replace one gallery with another:

```html
<script>
  // Check if viewer with `id="my-viewer"` is ready:
  Sirv.viewer.getInstance('#my-viewer').isReady();

  // Switch to the next item:
  Sirv.viewer.getInstance('#my-viewer').next();

  // Disable (exclude) 4th item in the viewer:
  Sirv.viewer.getInstance('#my-viewer').disableItem(3);
</script>
```

Example script to pause a slide on hover:

```html
<script>
  // Pause on entry:
  document.querySelector('#slideshow').addEventListener('mouseenter', () => { Sirv.viewer.getInstance('#slideshow').pause(); });

  // Play on exit:
  document.querySelector('#slideshow').addEventListener('mouseleave', () => { Sirv.viewer.getInstance('#slideshow').play(); });
</script>
```

Example script to show/hide items in a gallery:

```html
<script>
  // Hide items in the "color-navy" group
  Sirv.getInstance('#example-viewer-1').disableGroup('color-navy');

  // Show items from the "color-brown" and "size-m" groups
  Sirv.getInstance('#example-viewer-1').enableGroup(['color-brown', 'size-m']);

  // Show items from the "color-yellow" group and hide all other items
  Sirv.getInstance('#example-viewer-1').switchGroup('color-yellow');
</script>
```

## Spin API

| Method | Parameters | Description |
| --- | --- | --- |
| isInitialized |  | Check if Spin instance is initialized |
| isReady |  | Check if Spin instance is ready |
| play | (duration, type) | Start auto-spin rotation |
| pause |  | Stop [autospin](/help/sirv-media-viewer/media-types/360-spin/) |
| rotate | (columns, \[rows\]) | Rotate by X number of frames on X-axis and/or Y-axis |
| rotateX | (frames) | Rotate by X number of frames on X-axis |
| rotateY | (frames) | Rotate by X number of frames on Y-axis |
| jumpRows | (row) | Jump to a specific row |
| jumpCols | (column) | Jump to a specific column |
| zoomIn |  | Zoom in to current frame |
| zoomOut |  | Zoom out of current frame |
| isZoomed |  | Check if the current frame is zoomed |
| currentFrame |  | Get current frame: `{rows: XX, cols: XX}` |

Example script to rotate the spin:

```html
<script>
// Check if Spin component spin is initialized:
Sirv.getInstance('#my-viewer-id').child(2).spin.isInitialized();

// Check if Spin component is ready:
Sirv.getInstance('.product-view .Sirv').child('spin-item').spin.isReady();

// Start rotate of spin:
Sirv.getInstance('#my-viewer-id').child(2).spin.play(3600, 'row');
</script>
```

## Zoom API

| Method | Parameters | Description |
| --- | --- | --- |
| isReady |  | Check if Zoom instance ready |
| isZoomed |  | Check if image is zoomed in |
| zoomIn |  | Zoom in to image. |
| zoomOut |  | Zoom out of image |

Example code:

```html
<script>
// Check if component zoom is ready:
Sirv.getInstance('#my-viewer').child(3).zoom.isReady();

// Zoom in to image:
Sirv.getInstance('.product-view .Sirv').child('zoom-item').zoom.zoomIn();
</script>
```

## Video API

| Method | Parameters | Description |
| --- | --- | --- |
| isReady |  | Check if video instance is ready |

Example code:

```html
<script>
// Check if Video component is ready:
Sirv.viewer.getInstance('#my-viewer').child('video-item').video.isReady();
</script>
```

## Model API

| Method | Parameters | Description |
| --- | --- | --- |
| getMaterial | name or number | Choose a material from your model |
| createTexture | (uri, type) | Apply a new texture from an image URL |

Each material in the model has a number, with the first material being number 0.

### Material instance API

| Method | Parameters | Description |
| --- | --- | --- |
| setColor | number 0.00 to 1.00 for r, g, b, opacity | Set color of a particular material in the model e.g. \[r(0.54), g(0.17), b(0.63), a(0.26)\] |
| setTexture | name or null | Choose a texture name or clear the texture with null |
| setMetallicFactor | number 0.00 to 1.00 | Set metallic factor of a particular material in the model |
| setRoughnessFactor | number 0.00 to 1.00 | Set roughness factor of a particular material in the model |
| setMetallicTexture | name or null | Choose a metallic texture name or clear the texture with null |

Example code:

```html
<script>
// Set color of one material and metallic factor of another material:
const model = Sirv.viewer.getInstance('#my-viewer').child('model-item').model;
model.getMaterial(0).setColor('#abf0c4');
model.getMaterial(2).setMetallicFactor(0.74);
</script>
```

## Image API

| Method | Parameters | Description |
| --- | --- | --- |
| isReady |  | Check if image instance is ready |
| resize |  | Force image resize |

Example code:

```html
<script>
// Check if Image component is ready:
Sirv.viewer.getInstance('#my-viewer').child('image-item').image.isReady();

// Force image resize:
Sirv.viewer.getInstance('#my-viewer').child(4).image.resize();
</script>
```

## Lazy image API

| Method | Parameters | Description |
| --- | --- | --- |
| isReady |  | Check if lazy image instance is ready |
| resize |  | Force image resize |

Example code:

```html
<script>
// Check if Lazy Image is ready:
Sirv.lazyimage.getInstance('#my-lazy-image').isReady();

// Force image resize:
Sirv.lazyimage.getInstance('#my-lazy-image').resize();
</script>
```

## Hotspots API

| Method | Parameters | Description |
| --- | --- | --- |
| .add(hotspotSettings) | hotspotSettings (JSON | Array<JSON>) |
| .remove(index) | index (number) | Remove one [hotspot](/help/spin/advanced-features/hotspots-on-360-spin/) |
| .removeAll() |  | Remove all hotspots |
| .enable(index) | index (number) | Enable a particular hotspot |
| .disable(index) | index (number) | Disable a particular hotspot |
| .list() |  | List all hotspots |
| .setVisibility(index) | index (number) | Show/hide message popup or tooltip |

Example code to hide hotspots in fullscreen:

```html
<script>
const disableHotspots = (hotspotsApi) => {
  hotspotsApi.list().forEach((node, index) => hotspotsApi.disable(index));
};

const enableHotspots = (hotspotsApi) => {
  hotspotsApi.list().forEach((node, index) => hotspotsApi.enable(index));
};

Sirv.on('viewer:fullscreenIn', (e) => {
  e.items().forEach(item => {
    const componentApi = item[item.component];
    if (componentApi.hotspots && componentApi.isReady()) {
      disableHotspots(componentApi.hotspots);
    }
  });
});

Sirv.on('viewer:fullscreenOut', (e) => {
  e.items().forEach(item => {
    const componentApi = item[item.component];
    if (componentApi.hotspots && componentApi.isReady()) {
      enableHotspots(componentApi.hotspots);
    }
  });
});

Sirv.on('spin:ready', (e) => {
  if (e.parent().parent().isFullscreen() && e.hotspots) {
    disableHotspots(e.hotspots);
  }
});
</script>
```

This example hides the hotspots whenever the spin is shown in fullscreen.
