how to create a simple automatic image rotator using jQuery

Rotate II by FatalBite on deviantART

I’ll explain a simple way to create an automatic image rotator using jQuery. First have a look at the demo page (opens in a new tab) to understand what we’re trying to accomplish. You can also download the tutorial’s source code.

In the example, the initial image will be displayed on the left of a paragraph containing some sample text:

<div id="test">
  <img id="myImage" src="test1.png" alt="image test" />
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
</div>

First, we have to store the filenames of our images in an array. We also have to initialise a counter.

var images = new Array ('test1.png', 'test2.png', 'test3.png');
var index = 1;

The next step is to create the function which will rotate the images. We’ll call it rotateImage(). At the beginning, the currently displayed image fades out. Then, we load the next image from the images array (the counter we introduced before is being used here) and we fade it in. At the end of the function, we deal with the counter (either increment it, or reset it if all the imaged were displayed).

function rotateImage()
{
  $('#myImage').fadeOut('fast', function()
  {
    $(this).attr('src', images[index]);

    $(this).fadeIn('fast', function()
    {
      if (index == images.length-1)
      {
        index = 0;
      }
      else
      {
        index++;
      }
    });
  });
}

The last step is to call the rotateImage function repeatedly inside the $(document).ready function using setInterval. In the example, the function rotateImage() is being executed every 2.5 seconds (2500 milliseconds).

$(document).ready(function()
{
  setInterval (rotateImage, 2500);
});

The complete JavaScript code:

var images = new Array ('test1.png', 'test2.png', 'test3.png');
var index = 1;

function rotateImage()
{
  $('#myImage').fadeOut('fast', function()
  {
    $(this).attr('src', images[index]);

    $(this).fadeIn('fast', function()
    {
      if (index == images.length-1)
      {
        index = 0;
      }
      else
      {
        index++;
      }
    });
  });
} 

$(document).ready(function()
{
  setInterval (rotateImage, 2500);
});

HTML:

<div id="test">
  <img id="myImage" src="test1.png" alt="image test" />
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
</div>

If you haven’t done it already, have a look at the demo page (opens in a new tab). You can also download the tutorial’s source code.

 

my projects’ soundtrack

The Soundtrack - To My Life by Luke-Chueh on deviantART
Painting by ~luke-chueh on deviantART

As far as I can remember in my life, like every music lover does, I’m relating specific songs or sometimes whole albums with events, places, emotions etc. Even working on a project is not an exception.

Recently, it came into my attention that in every project I’m working on, there are two albums that are always part of my playlist, so they can qualify to be called my projects’ soundtrack. On certain times in a project, especially when a lot of mental engergy and even fantasy is required, I find myself listening to those two albums because their music inspires me and provides me with a rhythm that helps me think without distracting me at all.

The fist one is from a Greek songwriter called Konstantinos Vita and it’s the music composed for Dimitris Papaioannou’s (the art director of the Athens 2004 Olympic Ceremony) theatrical production called “2″. Below is the video of his song called “Funky Beep”.

The other one is Moby‘s famous album called “Play”. Below is the video of his song called “Natural Blues”.

What do you prefer to listen while working?

 

the adventure of a robot in love: machinarium

Machinarium

Machinarium is a point and click adventure game which brought me back to the good old days of such games. You control a small and cute (this is my feminine side speaking) robot through its journey to find its lost beloved one in a cruel robot world!

It has beautiful hand-made style graphics (as you can see on the screenshots) and the robot world is designed almost as I would have imagined it. Its music is a great company to the graphics. Note that when you buy the game you also receive its soundtrack in MP3 format! There is a lack of speech, but the communication is being done well using speech bubbles which explains everything clearly. After all, in a robot world English (or any other human language) can’t be an option!

It incorporates a large number of puzzles, in a great variety, from simple games (e.g. a large tic-tac-toe variation and an arcade asteroids game) to more complicated puzzles. Some of them are hard enough even for an experienced adventure games player (maybe due to the lack of explanation) but everything is solvable with some attention and patience.

After successfully using an item, it is disposed so the inventory isn’t full with unusable items (actually when I finished the game, I had 2 items left in my inventory but it wasn’t annoying at all).

But enough with the praise, let’s talk for the bad stuff: It’s a little bit annoying that you cannot just deselect an item in use and instead you have to return it to the inventory. Also, it might become boring at some point to have to watch the full animation of the robot walking up the stairs to go to another floor etc.

The only actual downfall of the game is the use of Flash as the development platform. Right-click does nothing (actually it displays the frustrating Flash player menu) when it could be used for example to deselect an item of the inventory (if another development platform was used since Flash doesn’t support right-click functionality as far as I’m aware). Also, the point-and-click system isn’t as precise as someone would expect it to be, so you might find yourself at some point clicking in the wrong place and making a wrong move.

But these facts couldn’t prevent even a huge Flash hater like myself to play and enjoy this amazing game! Before I forget, you cannot die in the game, so feel free to explore and try anything that you can think!

You can download the demo and/or buy the full game from its official website. Also, since you’re clearly interested in games I would suggest that you visit zuckr.com, where you can find news and reviews for the best free online games!

Machinarium Screenshot 1

Machinarium Screenshot 2