What steps can I take to transform these two fixed images into an array of multiple images?

After coming across this Pen a while ago, I had it on my to-do list and finally decided to work on it. However, my coding knowledge is not as sharp as it used to be, and I'm currently struggling with transforming it from two hardcoded images to a dynamic array of images.

If you're interested, here is the Pen

I attempted to add:

var slide3 = new Slide(width, height, 'middle');
    var l3 = new THREE.ImageLoader();
    l3.setCrossOrigin('Anonymous');
    l3.load('/assets/ScreenSaver/space3.jpg', function(img) {
        slide3.setImage(img);
    })
        
    root.scene.add(slide3);

However, it seems that just adding this code snippet is not enough; it appears that I need to include an additional control variable.

I would appreciate any tips or advice on how to proceed with this.

Answer №1

By utilizing this method, an individual was able to accomplish the task in React, and fortunately for me, I also have the same goal.

Check out the solution here

loadNextImages (images, index) {
let first = index
let second
const tmp = (first + 1) % (images.length)
if (index % 2 === 0) {
  second = tmp
} else {
  second = first
  first = tmp
}
this.slide0.setImage(images[first])
this.slide1.setImage(images[second])
return tmp

Answer №2

If you want to include another slide, follow these steps:

      var slide3 = new Slide(width, height, 'in');
  var l3 = new THREE.ImageLoader();
    l3.setCrossOrigin('Anonymous');
    l3.load('https://example.com/another-image.jpg', function(img) {
        slide3.setImage(img);
    })

root.scene.add(slide3);
  tl.add(slide3.transition(), 0);

Don't forget the last line. Test it out with an additional image by clicking this link:

https://example.com/codepen-link

Answer №3

An efficient technique to handle this situation is by creating an array variable where each element represents the image source and then loading them dynamically. This approach eliminates the need to repeatedly write the chunk of code. By updating the image array, you can easily add new images, remove existing ones, or modify their order.

var images = ['https://s3-us-west-2.amazonaws.com/s.cdpn.io/175711/winter.jpg', 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/175711/spring.jpg'];
  
for (let x = 0; x < images.length; x++) {
  var slide[x] = new Slide(width, height, 'out');
  var l[x] = new THREE.ImageLoader();
  l[x].setCrossOrigin('Anonymous');
  l[x].load(images[x], function(img) {
    slide.setImage(img);
  })
  root.scene.add(slide);

  tl.add(slide[x].transition(), 0);
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Navigational link behavior in the React component with the <tr> element

I am in the process of developing a React application with a component that shares a resemblance to the following: class TableRow extends React.Component { constructor(props) { super(props) } render() { const handleClick = () ...

What is the best way to navigate to the top of a form panel?

I'm currently developing a Sencha Touch mobile app. I have a form panel with multiple fields, so I made it scrollable. However, every time I open the screen (panel), scroll down, navigate to other screens, and then return to the form panel, it stays s ...

The mobile devices are not showing my HTML website

I have implemented the following CSS link code on my website: <link rel="stylesheet" href="index_files/front.css" media="all" type="text/css" > Additionally, I have included the following code <meta name="HandheldFriendly" content="True"> & ...

What is the best way to incorporate multiple versions of jQuery on one webpage?

Is it possible to use multiple versions of jQuery on a single page? I need two different versions for various functions, but they seem to be conflicting with each other. If I implement jQuery noconflict, will the product scripts still work? <script typ ...

Creating a custom route that is specific to a single ejs file

I'm trying to set up a URL like localhost:3000/hh234due with a unique ID that routes to a specific page. However, my current implementation is conflicting with other routes. How can I make the /:id route unique to one view? module.exports = fun ...

Assigning an array of objects within an AJAX request

I have a MediaObject object that includes: a Media object an array and a function called getMedia() When attempting to create a Media object and push it into the array inside the getMedia function after making an AJAX call, I encountered issues referenc ...

Vue's dynamic component list now accurately removes incorrect HTML nodes

Exploring the capabilities of Vue.JS by creating components dynamically and composing them together. Encountered an unusual issue where, even though data seems to be updating correctly, removing one of the boxes using splice() always results in the remova ...

Comparing boolean values in React JS

I am working with a React JavaScript code in which I am iterating through a series of boolean values. The issue I am facing is that when 'data.nextrow' is false, I expect nextrow1 to also be false but it ends up being set to true instead. co ...

How can I submit a form using Ajax and save the response data?

I'm currently using a form to send SMS data to the sms.php page for processing. However, I want to submit the form data without redirecting to the sms.php page. Instead, I would like the data to be processed on the same page using Ajax. Although I hav ...

Add MySQL JSON data to an array

Having an issue with a MySQL query in Node.js. After executing the query and receiving a JSON result, I'm trying to store the query result in an array variable. However, I'm having trouble accessing the elements of the array as it always returns ...

Limit access to Google Fusion Table to specific types of maps. Eliminate Google Fusion Table for selected map formats

Currently, I am in the process of creating a web map using the Google Maps Javascript API. My objective is to display a Google Fusion Table containing buildings in Boston exclusively on a stylized map named "Buildings." When I switch to the Buildings map t ...

Steps for uploading an item to an API using an Excel document

I'm facing an issue where I am trying to send a large object along with an Excel file to an API. However, only my photo is being recognized and the object is not sent, resulting in an [object Object] error. I understand that this error occurs due to i ...

Exploring the possibilities of combining AngularJS with a Bootstrap modal for a

Hello, I am currently attempting to update a modal select box using Angular and Bootstrap modals. Unfortunately, when I click the button, I am having trouble getting it to update the related select box value in the Bootstrap modal. I've tried differ ...

Unable to receive acknowledgment from child component within Redux

I am trying to retrieve props from Redux within a child component, but for some reason, {this.props} is showing up as an empty object. While I am successfully using react-redux connect to access the props in the parent component and pass them down to the ...

What is the best way to ensure a function waits for a stable database connection before proceeding?

Perhaps not phrased well, but I grasp the concepts of async/promises/callbacks. My aim is to develop a module that can be imported (database.js), where I can then execute methods like database.insert() and database.read(). This is the code I have so far: ...

What causes the namespace to shift when utilizing npm for installing a library?

I've been attempting to integrate whammy.js into a project. The initial line in the source code is window.Whammy = (function(){ yet, after running npm i and inspecting node_modules, I discovered global.Whammy = (function(){ https://github.com/anti ...

Reactjs - There was an error: $node.attr(...).tooltip is not defined

I am currently troubleshooting the SummerNote mention library and encountering an issue - Uncaught TypeError: $node.attr(...).tooltip is not a function The versions I am using are: - "react-summernote": "^2.0.0", - React version: "react": "^16.8.6", ...

Tips for integrating AudioControl with Phonegap

I couldn't find a suitable plugin, so I decided to create my own. My goal is to activate silent mode using a JavaScript command, however, I am encountering an error with the undefined method getSystemService. It seems like there may be a problem with ...

Running intricate javascript/jquery/json code using an html checkbox

Hey there, I'm hoping this issue is simpler than I'm making it out to be... So, I've got this pretty complex JSON/jQuery function (3000 lines long) that builds a DOM tree in a separate JS file. This JS file also contains a bunch of other on ...

What is the best way to display a header element in front of an article element?

I'm struggling with making my header element sticky and appear in front of my article. Modifying the z-index hasn't given me the desired result so far. Is the z-index ineffective when dealing with floating elements? Or is there a workaround to m ...