Tutorial: Marzipano Switch Images - 360 Image Viewer

For an introduction to Marzipano refer to Tutorial: Marzipano Getting Started with Free 360 Image Viewer from Google

organize 360 images

I have 7 RICOH THETA images stored in the ./images folder off of my root project.

In index.js, set up a constant to hold the images.

// create array of filenames
const imageFileNames = [
    "stanford.jpeg",
    "carlsbad_1.jpg",
    "carlsbad_2.jpg",
    "room_5_5k.jpeg",
    "palo_alto_office_11k.jpeg",
    "honda-insight.jpeg",
    "audi-car.jpeg"
];

index.html

add a div with id 'buttons`. Full HTML code below.

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<title>Marz Switch</title>
<link rel="icon" type="image/png" href="favicon.png">

</head>
<body>
<div id="buttons"></div>
<p>Marzipano 360 image viewer demo</p>
<div id="pano"></div>
<script src="marzipano.js" ></script>
<script src="index.js"></script>
</body>
</html>

apply basic styling in style.css

#pano {
    position: absolute;
    width: 100%;
    height: 85%;
}

#buttons {
    padding: 5px;
}

button {
    margin: 5px;
    padding: 10px 20px;
    font-size: 24px;
    font-weight: bold;
    color: grey;
    border: none;
    border-radius: 12px;
    background-color: #FADA7A;
}

p {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 20;
    padding: 5px;
}

create a button for every image

Get the HTML element buttons.

For every button, run scene.switchTo() on the click event. The switchTo() method from Marzipano is what causes the image to change. The value of scene changes every time through the loop.


// Create Marzipano scenes and HTML buttons to hold each Marzipano scene
const buttons = document.getElementById("buttons");

for (let i = 0; i < imageFileNames.length; i++) {

    // create an unique Marzipano scene for each image
    let scene = viewer.createScene({
        source: Marzipano.ImageUrlSource.fromString(
          `images/${imageFileNames[i]}`
        ),
        geometry: geometry,
        view: view,
      });
  
    // create HTML button and switch scene when button is clicked  
    let buttonSwitchImage = document.createElement("button");
    buttonSwitchImage.textContent = i + 1;
    buttonSwitchImage.addEventListener('click', function() {
        scene.switchTo();
    })
    buttons.appendChild(buttonSwitchImage);
    // Display first scene.
    if (i == 0) {scene.switchTo()}
}

index.js full listing

// Create viewer.
var viewer = new Marzipano.Viewer(document.getElementById('pano'));

// Create geometry.
var geometry = new Marzipano.EquirectGeometry([{ width: 4000 }]);

// Create view.
var limiter = Marzipano.RectilinearView.limit.traditional(1024, 100*Math.PI/180);
var view = new Marzipano.RectilinearView({ yaw: Math.PI }, limiter);

// create array of filenames
const imageFileNames = [
    "stanford.jpeg",
    "carlsbad_1.jpg",
    "carlsbad_2.jpg",
    "room_5_5k.jpeg",
    "palo_alto_office_11k.jpeg",
    "honda-insight.jpeg",
    "audi-car.jpeg"
];

// Create Marzipano scenes and HTML buttons to hold each Marzipano scene
const buttons = document.getElementById("buttons");

for (let i = 0; i < imageFileNames.length; i++) {

    // create an unique Marzipano scene for each image
    let scene = viewer.createScene({
        source: Marzipano.ImageUrlSource.fromString(
          `images/${imageFileNames[i]}`
        ),
        geometry: geometry,
        view: view,
      });
  
    // create HTML button and switch scene when button is clicked  
    let buttonSwitchImage = document.createElement("button");
    buttonSwitchImage.textContent = i + 1;
    buttonSwitchImage.addEventListener('click', function() {
        scene.switchTo();
    })
    buttons.appendChild(buttonSwitchImage);
    // Display first scene.
    if (i == 0) {scene.switchTo()}
}

GitHub Code and Live Site

OK, this is interesting. It’s nice having a simple app to focus in on how Marzipano is functioning.

  • The switchTo() method from Marzipano is what causes the image to change. You’re switching scenes, not images, correct?
  • What is the order of the scenes? In other words, which scene does switchTo() go to? How do you change the order?
  • Does the order of the images in the array (imageFileNames) matter?

yes. it is scene.switchTo()

There is no order. If you have 5 scenes, you can name each scene a different variable: sceneA, sceneB, sceneC, sceneD, sceneE.

If you want to switch to sceneD, then do sceneD.switchTo() and it will switch to sceneD

However, in this demo, I reuse the scene variable for each scene. Thus, the image for scene.switchTo() is whatever the value of scene was set to prior to the switch.

In this demonstration, it does. The buttons are from the listing of the files. thus, the first image will be button 1. Perhaps it is easier to understand if I used thumbnails?

What type of buttons do you want?

In this easy-to-use example, I think numbers are fine. Yes, having thumbnails for the buttons would be a more intuitive UI for the user. But I think this app is aimed at the developer working with Marzipano.

I added an example of using thumbnails.

The thumbnails are an overlay.

layer thumbnails over Marzipano viewer

Layers are controlled by the css, z-index.

#pano {
    position: relative;
    width: 100%;
    height: 100%;
}

#buttons {
    position: absolute;
    padding: 5px;
    bottom: 50px;
    z-index: 10;
}

.thumb-button {
    width: 140px;
    height: 70px;
    padding: 3px;
    border-radius: 5px;

}

replacing buttons with images

In the index.js file, I replaced the button html tag with img

  let thumbnail = document.createElement("img");
  thumbnail.src = `images/thumbs/${imageFileNames[i]}`; 
  thumbnail.className = "thumb-button"
  thumbnail.style.cursor = "pointer"; // Change cursor to pointer for interactivity
  thumbnail.addEventListener("click", function () {
    scene.switchTo();
  });

full code for modification

thumb-switch