Image Slider in JavaScript

Introduction

 
In this article we will create a simple Image Slider in which the image can be scrolled in both directions (X and Y) with the slider like this:
 
javascript Image Slider
In this example, we will use a Canvas, that will be used as a container of our images in the Image Slider. Here we will use the drawImage() method that is used to draw our Image in the Canvas.
 
Step 1: So first we will create a Canvas using the <canvas> tag like this:
  1. <canvas id="myCanvas" width="1200" height="800"></canvas> 
Step 2: Now we will use the Images for our Image Slider:
  1. <img id="img1" src="Desert.jpg" style="display:none;"  />  
  2. <img id="img2" src="Hydrangeas.jpg"   style="display:none;" />  
  3. <img id="img3" src="Jellyfish.jpg" style="display:none;" />  
  4. <img id="img4" src="Koala.jpg" style="display:none;" />  
  5. <img id="img5" src="Lighthouse.jpg" style="display:none;" />  
  6. <img id="img6" src="Penguins.jpg"  style="display:none;"/>  
  7. <img id="img7" src="Chrysanthemum.jpg" style="display:none;"/>  
  8. <img id="img8" src="Tulips.jpg"  style="display:none;" /> 
Here we will set the display property of the images so it can't be visible.
 
Step 3: Now we will write the JavaScript function Show():
  1. <script language="javascript">  
  2. var i = 1, x = 100, y = 100, j = 0;  
  3. var img = new Image();  
  4. var imgsrc;  
  5.    
  6. function Show() {  
  7.   var canvas = document.getElementById('myCanvas');  
  8.   var context = canvas.getContext('2d');  
  9. }  
  10. </script> 
Here we will declare some variables like x is used to increase the values in the x-direction and y is used to increase the values in the y-direction. canvas is used for the canvas. And context is used to draw on the canvas.
 
Now we start the loop where we decrease the value of the variable that scrolls the images for a specific amount of time, like this:
  1. if (j == 0) {  
  2.     j = 400;  
  3. }  
  4. else {  
  5.     j = j - 5;  
  6. }  
  7.   
  8. if (j == 10) {  
  9.     var a = "img" + i;  
  10.     imgsrc = document.getElementById(a).src;  
  11.     x = 0;  
  12.     y = 0;  
  13.   
  14.     if (i < 8) {  
  15.         i = i + 1;  
  16.     }  
  17.     else {  
  18.         i = 1;  
  19.     }  
Here we start the loop and decrease it at the time of each call of the Show() function. Here we assign the value if Image Id in the variable a like this:
  1. var a="img"+i;  
and we will increase the value of i in each call of the Show() function. Here we assign the source value of the Image in a variable (imgsrc) that will be used later.
 
Step 4: Now we will write the function that is used to draw our image on the canvas:
  1. img.onload = function () {  
  2.   var sX = x;  
  3.   var sY = y;  
  4.   var sWidth = 500;  
  5.   
  6.   var sHeight = 400;  
  7.   var dWidth = sWidth;  
  8.   var dHeight = sHeight;  
  9.   
  10.   var dX = canvas.width - dWidth - 400;  
  11.   var dY = canvas.height / 2 - dHeight / 2 - 100;  
  12.   context.drawImage(img, sX, sY, sWidth, sHeight, dX, dY, dWidth, dHeight);  
  13. };  
  14. img.src = imgsrc;  
  15.   x = x + 6;  
  16.   y = y + 3; 
Here we will assign the values in the variables, that will be used in the drawImage() method. Here we will increase the x and y, it will be helpful to increase the values of the x and y coordinates in our program.
 
Step 5: Now we use the setTimeout() method to call our Show() function after a specified time.
  1. setTimeout("Show()", 5);