0

The thing is that I made a reflection cube where each side image change randomly each time that I reload my site.

This is the code that I made:

            var imgAr = [
                      'sources/instagram2/image1.jpg',
                      'sources/instagram2/image2.jpg',
                      'sources/instagram2/image3.jpg',
                      'sources/instagram2/image4.jpg',
                      'sources/instagram2/image5.jpg',
                      'sources/instagram2/image6.jpg',
                      'sources/instagram2/image7.jpg',
                      'sources/instagram2/image8.jpg',
                      'sources/instagram2/image9.jpg',
                      'sources/instagram2/image10.jpg'
                    ];

            var urls = imgAr.sort(function(){return .6 - Math.random()}).slice(0,6);
            var reflectionCube = THREE.ImageUtils.loadTextureCube( urls, THREE.CubeReflectionMapping );

I have a lot of images with the same name style in the same folder (image1, image2, etc) so I tryied to find a way to write a code where I could specify that I want to use all the files which name ends with a number.

I made this code but it doesnt work :

            var nrString = "000";

            images = [
                    'sources/instagram2/image' + nrString + ".jpg",
                    'sources/instagram2/image' + nrString + ".jpg",
                    'sources/instagram2/image' + nrString + ".jpg",
                    'sources/instagram2/image' + nrString + ".jpg",
                    'sources/instagram2/image' + nrString + ".jpg",
                    'sources/instagram2/image' + nrString + ".jpg"
                    ];

            var urls = images.sort(function(){return .6 - Math.random()}).slice(0,6);
            var reflectionCube = THREE.ImageUtils.loadTextureCube( urls, THREE.CubeReflectionMapping );

I hope that someone can help me. I have days trying to make it.

WestLangley
  • 102,557
  • 10
  • 276
  • 276
gnazoa
  • 49
  • 11

1 Answers1

0
        var numberOfImages = 50, images = [];
        for (var i = 1; i <= numberOfImages; i++) {
            images.push(
                'sources/instagram2/image' + i + ".jpg"
            );
        }
kaigorodov
  • 877
  • 9
  • 15
  • Thank you @kaigorodov you are a genius!!! But my problem is that my folder will be constantly updating so I can't limit the number of images to 50 because it will be constantly growing... – gnazoa Nov 19 '15 at 12:48
  • JavaScript does not now the number of images on your server side. You need to count and pass a number of the last image from server with PHP in example. – kaigorodov Nov 19 '15 at 12:50
  • Ok. Thank you very much for your help – gnazoa Nov 19 '15 at 12:53