www.webdeveloper.com
Results 1 to 3 of 3

Thread: How to animate jpgs using setInterval()

  1. #1
    Join Date
    Feb 2011
    Posts
    2

    How to animate jpgs using setInterval()

    Hello,
    My question is about creating an animation using the setInterval function.

    I want to load 5 jpgs in the head section. I then want to call them - one second apart - using the setInterval function in the body section.

    Below is a simple example of what I'm attempting to do. Can someone please correct my code? Or tell me what I'm doing wrong?

    Thanks so much!

    HTML Code:
      <head>
        <script type="text/javascript">
    
          var c = 1;
    
          /* Preloading images */
          var image1 = new Image();
          image1.src = "a1.jpg";
          var image2 = new Image();
          image2.src = "a2.jpg";
          var image3 = new Image();
          image3.src = "a3.jpg";
          var image4 = new Image();
          image4.src = "a4.jpg";
          var image5 = new Image();
          image5.src = "a5.jpg";
    
          function disp_img(w)
          {
          if (c == 6)
          {
          c = 1;
          }
       
          var img_src = "a" + c + ".jpg";
          document.ani.src = img_src;
          c++;
    
        </script>
      </head>
      
      <body>
        <script type="text/javascript">
    
          setInterval("disp_img(c)", 1000);
      
        </script>
      </body>

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    Code:
     
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title></title>
            <script type="text/javascript">
                var picPaths = ['pic1.jpg','pic2.jpg','pic3.jpg'];
                picO = new Array();
                for(i=0; i < picPaths.length; i++) {
                    picO[i] = new Image();
                    picO[i].src = picPaths[i];
                }
                var curPic = -1;
                function swapImage(){
                    curPic = (++curPic > picPaths.length-1)? 0 : curPic;
                    imgO.src = picO[curPic].src;
                    setTimeout(swapImage,1000);
                }
                window.onload=function(){
                    imgO = document.getElementById("imgRot");
                    swapImage();
                }
            </script>
        </head>
        <body>
            <div>
                <img src="" alt="" id="imgRot" />
            </div>
        </body>
    </html>

  3. #3
    Join Date
    Feb 2011
    Posts
    2
    tirna,
    Thank you so much! That is EXACTLY what i was looking to do.

    Not only is it usable, but the code you provided is very clear. I learned a lot just from going through each line.

    Thanks again!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles