AnimateImage

Index

Introduction

AnimateImage is a JavaScript code which displays various images like animated GIF.

Features

  • Displays various images successively like animated GIF
    • Continuous images whose filename is sequential number
    • Any images whose filename is non-sequential number
  • Supports all image formats supported by Web browser
    • PNG, JPEG, GIF, BMP, etc.
    • Transparent PNG: Transparent background of animated images
    • High-quality images: more than 256 colors photos and illustrations
  • To Specify animation interval (default is 0.5 seconds)
  • To Specify repeat count of animation (default is infinite iteration)
  • To control (play/stop/replay) animation at any time
  • Simple Slide-Show with not continuous images
  • Stand-alone script and only just 2.5 KB file size
    • JS libraries such as jQuery is not required

Usage

Write the following code in "head" or "body" element to load AnimateImage script.

<script type="text/javascript" src="animateImage.js"></script>

If you want to animate Image1.png, Image2.png, … Image10.png, write the following code.

<script type="text/javascript">
    animateImage('Image[1-10].png');
</script>

* animateImage() function accepts the following arguments. See Code Samples and API Specification for more information.

  • Zero-padding filenames (01, 02, …, 001, 002, …)
  • Non-sequential filenames
  • Image title
  • ID string to apply styles (CSS)
  • Animation delay (interval)
  • Repeat count of animation
  • Whether rewinds to first image at the end of animation

* Using ImageAnimator object, you can play/stop/replay animation at any time. And using Animations object, you can change common settings of all animations. See Code Samples and API Specification for more information.

Download

Version
0.8.0.3 (2011-08-29)
Author
attosoft
License
MIT License

Supported Browsers

  • Internet Explorer
  • Mozilla Firefox
  • Google Chrome
  • Apple Safari
  • Opera

* AnimateImage is coded with JavaScript 1.4 / JScript 5.0 (IE5). So AnimateImage may support most browsers.

Demos

Transparent PNG

Transparent background of animated images with transparent PNGs. In this demo, background color changes on mouse hover.

Simple Slide-Show

High-quality photos and illustrations which is more than 256 colors. In this demo, 24-bit colors JPEGs are animated.

Non-sequential filenames

Any files whose filename is non-sequential number. In this demo, "blank.png", "website.png", "blog.png", and "download.png" are animated.

Animation Control (Play/Stop/Replay)

Animation control (play/stop/replay) at any time. In this demo, [Start] button starts (restarts) animation, [Stop] button stops (pauses) animation, and [Replay] button replays animation from the beggining.

Code Samples

Here is code samples when animates the following images.

  • Foo1.png, Foo2.png, … Foo5.png (5 PNGs)
  • Bar0.png, Bar1.png, … Bar9.png (10 PNGs)
  • Baz01.jpg, Baz02.jpg, … Baz15.jpg (15 JPEGs, zero-padding)
Code Sample 1

Animations with image title.

animateImage('Foo[1-5].png', 'Image Foo');
animateImage('Bar[0-9].png', 'Image Bar');
animateImage('Baz[01-15].png', 'Image Baz');

* The sequence number of Image Foo starts from 1, the number of Image Bar starts from 0, and the number of Image Baz starts from 01. Then animation delay is 0.5 seconds (default), repeat count of animation is infinite iteration (default).

Code Sample 2

Animation with 5 images. In only Image Foo, image title and ID string are specified.

animateImage('Foo[1-5].png', 'Image Foo', 'id-foo');
animateImage('Bar[0-4].png', '', null);
animateImage('Baz[01-05].png', null, null);

* In Image Bar, alt attribute with empty string is added to img element (img@alt=""). This represents that alternative text of image is unnecessary. In Image Baz, img@alt and img@title are not added. In both Imabe Bar and Image Baz, ID string is generated automatically.

Code Sample 3

Animation with different animation delay and repeat count. Image Bar is rewinded to first image at the end of animation.

animateImage('Foo[1-5].png', 'Image Foo', null, 1000, 5);
animateImage('Bar[0-9].png', 'Image Bar', null, 500, 10, true);

* At the end of animation, Image Foo remains last image (Foo5.png), while Image Bar is rewinded to first image (Bar0.png).

Code Sample 4

Animation once at 1 second interval. But in only Image Baz, animation iterates infinitely at 0.5 seconds interval.

Animations.delay = 1000;
Animations.repeat = 1;
animateImage('Foo[1-5].png', 'Image Foo');
animateImage('Bar[0-9].png', 'Image Bar');
animateImage('Baz[01-15].jpg', 'Image Baz', null, 500, -1);
Code Sample 5

Animation with any images whose filename is non-sequential number.

var files = ['Foo1.png', 'Bar0.png', 'Baz01.jpg'];
animateImage(files);

animateImage(['Foo5.png', 'Bar9.png', 'Baz15.jpg']);
Code Sample 6

Animation control (start/stop/replay). Repeat count of animation is once.

var animator = new ImageAnimator('Foo[1-5].png', null, null, null, 1);

* If you control animation with buttons, write the following code.

<input type="button" value="Start" onclick="animator.animate()" />
<input type="button" value="Stop" onclick="animator.stopAnimate()" />
<input type="button" value="Replay" onclick="animator.animate(true)" />
Code Sample 7

To apply styles (CSS) to animation images. In only Image Bar, ID string to apply individual style is specified.

animateImage('Foo[1-5].png', 'Image Foo');
animateImage('Bar[0-9].png', 'Image Bar', 'id-bar');

Write class selector (img.animation) for common styles, and write id selector (img#id-bar) for individual style.

img.animation {
    border: solid medium silver;
}

img#id-bar {
    border: dashed medium blue;
}

API Specification

animateImage() Function

Animates images.

Syntax
animator = animateImage(files, title, id, delay, repeat, rewind)
Arguments
files (required)
Image files. Sequenctial part of filename is "[x-y]" format (x: first sequence number, y: last sequence number). If sequence number is zero-padding, use "[0x-yy]" format. And if files are non-sequential, use Array object.
title
Image title. This is value of img@alt, img@title attribute. (attributes are not added in null or default)
id
ID string of img element. This is unique value for each animation. (automatically generated in null or default)
delay
Animation delay (Animations.delay in null or default)
repeat
Repeat count of animation (Animations.repeat in null or default)
rewind
Whether rewinds to first image at the end of animation. This is boolean (true/false). (Animations.rewind in default)
Return Value
animator
ImageAnimator object. you can stop/replay animation using this object.

Animations Object

Common settings of all animations.

* The value is reflected at calling animateImage() function. If you change the value, you should set before the start of animation.

Properties
Animations.delay
Animation delay. This is on the millisecond (ms). Default is 500 ms.
Animations.repeat
Repeat count of animation. -1 means infinite iteration, and 0 means no animation (only displays first image). Default is -1.
Animations.rewind
Whether rewinds to first image at end of animation. Default is false.
Animations.className
class name of img element (img@class attribute value). Default is "animation". class attribute is not added if the value is undefined.

ImageAnimator Object

Controls animation of images.

Constructor Function
ImageAnimator(files, title, id, delay, repeat, rewind)

* See animateImage() function about arguments specification.

animate(replay) Method

Plays (Replays) animation.

If argument "replay" is true or you call this function after the end of animation, animation is replayed from the beggining.

stopAnimate() Method

Stops (Pauses) running animation.

FAQ

Differences between ImageAnimator object and animateImage() function

ImageAnimator Object
Use when you want to control the start timing of animation.
  • In creating object, first image is displayed.
  • Animation does not start until calling animate() method.
animateImage() Function
Use when you do not care about starting animation automatically.
  • In calling the function, first image is displayed and animation starts.

Alternative Scripts

Animation Specification

Support Me?

  • If you have any feedbacks or questions, visit Contact Me
  • If you like this script, please consider making a donation to support the development

Any comments will be very helpful and appreciated. Thank you for your support!


"keyboard keys" by chromatix is licensed under Creative Commons License.

Related Posts

  1. JavaScript: 複数の画像をアニメーション GIF のように表示する AnimateImage (旧バージョン)
  2. AnimateImage Script 0.8 released
  3. AnimateImage version 0.6.5 公開
  4. AnimateImage version 0.7 公開
  5. AnimateImage version 0.6.2 公開

Comments

  1. 見習い says:

    はじめまして。
    PNGアニメのできるjQueryを探していたところこちらにたどり着きました。
    大変助かりました。ありがとうございます!
    こんなに簡単にできてしまうなんて感動です。

    ひとつ質問させていただきたいのですが、「アニメーションが一度終了したのちに、一定時間後に再生を再開する。(これをループ)」という動作をさせたいと思い、以下のように書いてみたのですが、うまくいきませんでした。もしかしてかなりトンチンカンな事をしているのかもしれません。このような動作をさせるにはどのようにしたら良いのでしょうか?

    function test(){
    animateImage('sample[01-10].png', 'sampleanime', null, 100, 1); setTimeout("test()",3000);
    }

    このページ内のMEMOにお書きになっている「最初/最後の画像で一時停止してからアニメーションする (pauseAtFirst/Last)」との表記がもしかして関係あるのでしょうか?いきなりの質問で大変恐縮ですが、お分かりになりましたらお答え願えませんでしょうか?よろしく御願いいたします。

    • attosoft says:

      PNGアニメのできるjQueryを探していたところこちらにたどり着きました。
      大変助かりました。ありがとうございます!
      こんなに簡単にできてしまうなんて感動です。

      コメントありがとうございます。attosoft です。
      こちらこそご利用いただけて大変うれしいです。

      ひとつ質問させていただきたいのですが、「アニメーションが一度終了したのちに、一定時間後に再生を再開する。(これをループ)」という動作をさせたいと思い、以下のように書いてみたのですが、うまくいきませんでした。

      function test(){
          animateImage('sample[01-10].png', 'sampleanime', null, 100, 1);
          setTimeout("test()",3000);
      }
      

      希望のアニメーション動作を具体的にリストで表すと、次のような動作でしょうか。

      1. アニメーション間隔 0.1 秒で 1 回だけアニメーションする
      2. アニメーションが終了して止まる
      3. 一定時間 (3 秒) 後に「動作1」に戻る (以降繰り返す)

      アニメーション動作を制御したい場合は ImageAnimator オブジェクトを利用します。たとえば今回のような動作であれば、次のようなコードになると思います。

      var animator = new ImageAnimator('sample[01-10].png', 'sampleanime', null, 100, 1);
      function test(){
          animator.animate();
          setTimeout("test()", 3000);
      }
      test();
      

      ちなみにうまくいきませんでしたというのは、もしかしてアニメーション画像が次々と挿入されていく現象のことでしょうか。これは animateImage() 関数を同じ ID で複数回呼ぶと、重複しない ID 文字列の生成処理が行われるのが要因です。(バグではありません)

      このページ内のMEMOにお書きになっている「最初/最後の画像で一時停止してからアニメーションする (pauseAtFirst/Last)」との表記がもしかして関係あるのでしょうか?

      MEMO にあるのは思いついた機能の候補ですので直接的には関係しません。またどちらかと言えば関連性があるのは「アニメーションサイクル間の遅延時間 (cycleDelay)」になります。AnimateImage に cycleDelay が実装されれば、アニメーションとアニメーションの間隔に任意の時間を指定できるため、今回のような動作を簡単に実現することができる、ということになります。

  2. 見習い says:

    attosoftさま

    回答ありがとうございます!
    こんなに丁寧に書いていただけるなんて感激です。
    提示していただいたコードで理想の動作がしっかり実現できました!
    勉強にもなりました。
    まだまだ初心者。勉強しなければダメですね。

    ※「うまくいきませんでした」というのは、PNGアニメ自体が表示されなくなってしまった状態でした。

    いきなりの質問に貴重な時間を割いてお答えいただき、本当にありがとうございました。AnimateImage、これからも愛用させていただきます。
    それでは。

    • attosoft says:

      無事に解決したようで何よりです。
      作成したスクリプトが多少なりとも役に立っていると思うと、非常に嬉しく思います。

      ※「うまくいきませんでした」というのは、PNGアニメ自体が表示されなくなってしまった状態でした。

      原因はなんでしょう…。test() 関数を呼び出していないから、なんてことはないですよね。

  3. 見習い says:

    >原因はなんでしょう…。test() 関数を呼び出していないから、なんてことはないですよね。
    問題は解決したものの、勉強のためにいろいろ試してみようと思い、最初に試したものを引っ張ってきたのですが、以下になります。この時点では「test() 関数を呼び出していない」という事になってしまっているようですね。(汗)

    function test(){
    animateImage('sample[01-10].png', 'sampleanime', null, 100, 1); setTimeout("test()",3000);
    }

    しかし以下で試したところ、ページ自体が白紙の状態になってしまいました。
    function test(){
    animateImage('sample[01-10].png', 'sampleanime', null, 100, 1); setTimeout("test()",3000);
    }
    test();

    • attosoft says:

      しかし以下で試したところ、ページ自体が白紙の状態になってしまいました。

      function test(){
      animateImage('sample[01-10].png', 'sampleanime', null, 100, 1);
      setTimeout("test()",3000);
      }
      test();
      

      うーん、原因不明ですね。私の環境 (IE, Chrome, Firefox, Opera, Safari) では、上記コードで一応 (初回の) 画像アニメーションが行われますけど。(前述のコメントのように animateImage 関数は同じ ID による複数回呼び出しを想定していないため、初回以降のアニメーションには問題あり)

      ちなみに細かいことになりますが setTimeout 関数では文字列 "test()" を渡すのではなく関数オブジェクト test を渡した方が適切です。

  4. babi says:

    はじめまして!とてもわかりやすく、素敵なページ、勉強させていただいております。

    AnimateImageを使わせていただき、動作確認をしていたところ、
    連続する画像が8枚以上だとエラーがおきてしまいました。。。
    7枚までですと正常に動きます。

    改変した部分は、秒数と、img 要素の書き込み(下記部分)のみです。

    ———————————-

    // img 要素の書き込み
    var imgElem = '<img id="' + id + '" src="' + anim.images[0].src + '"' + 'width="200"' ;

    ———————————-

    7枚と8枚の差は何なのでしょうか・・・
    お手数ですがご教授いただけますと幸いです。

    • attosoft says:

      コメントありがとうございます。attosoft です。

      連続する画像が8枚以上だとエラーがおきてしまいました。。。
      7枚までですと正常に動きます。

      具体的にはどのようなエラーでしょうか。

      現象およびエラーメッセージが分からないので推測になりますが、次のような原因が考えられます。

      • animateImage.js の改変に問題がある
      • 画像に問題がある
      • AnimateImage スクリプトのバグ

      animateImage.js を改変しているとのことですが、改変していない状態で動作確認してみてください。正常に動作するのであれば改変部分に、エラーとなる場合は画像に問題がある可能性があります。

      そもそも animateImage.js を改変する必要はあるのでしょうか。

      改変した部分は、秒数と、img 要素の書き込み(下記部分)のみです。

      // img 要素の書き込み
      var imgElem = '<img id="' + id + '" src="' + anim.images[0].src
        + '"' + 'width="200"' ;
      

      秒数の変更と width 指定のみであるならば、次のようなコードを書いて、

      Animations.delay = xxx;
      

      CSS で次のようなスタイルを指定すれば改変不要になりますよね。

      img.animation {
          width: 200px;
      }
      

      ※ ちなみに改変部分ですが、width 属性の前にスペースがないので不正な HTML となってしまいます。

    • babi says:

      attosoft 様

      こんにちは、先日質問させていただきましたbabiと申します。

      お返事ありがとうございます!!とても丁寧な解説、勉強になります。
      確かにjs側で画像サイズを指定する意味はないですね、、失礼いたしました、cssで調整することにして、jsは改変せずに使用させていただきました。

      ですが、やはり8枚以降になると動作しませんでした。。

      他のjsが競合になっているのかな?と思い、animateImage.jsのみと、画像、htmlのみで実験してみましたが、8枚以上になるとページが生成されません。
      エラー文言も出てこず、画像を他のものに差し替えてもだめでした。。。

      ちなみに、実験時のファイル内容は以下の通りです

      ※jsは秒数含め改変一切なし

      html
      ———————————-

      テスト

      animateImage('画像パス[01-08].gif');

      ———————————-

      上記の記述で、画像パス[01-07].gif までだと正常に動きます。。

      jsエラーが起きないので、何が問題なのかわかりません。。何かhtml記述に間違いがあれば、教えていただけますと幸いです。

    • babi says:

      あ、すみません、、上記にhtmlコードを書いたのですがコメント欄にはソース反映されませんね。。

      ———————————-

      <!DOCTYPE html>
      <html lang="ja">
      <head>
      <meta charset="UTF-8" />
      <meta http-equiv="Content-Style-Type" content="text/css" />
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

      <title>テスト</title>
      <script type="text/javascript" src="animateImage.js"></script>

      </head>

      <body>

      <script type="text/javascript">animateImage('画像パス[01-08].gif');</script>

      </body>
      </html>
      ———————————-

      こちらで表示されますでしょうか。。

    • attosoft says:

      あ、原因分かりました。AnimateImage スクリプトのバグでした。色々と時間を無駄にさせてしまってすみません。

      連番が "0" で始まっていると 8 進数と解釈されるため "08", "09" は "0" と評価されていました。問題を修正した version 0.8.0.3 に差し替えましたので、正常に動作するかご確認ください。

      ※ version 0.8.0.3: parseInt 関数に明示的に基数 10 を指定

    • babi says:

      attosoft 様

      8進数!そうだったのですね!すぐにわかるなんてすごい!!
      迅速なご対応、ありがとうございます、更新されたjsに差し替えましたところ、正常に動作しました!
      素敵なコードを提供くださり、ほんとうに助かっております。勉強させていただきます。

      本当にありがとうございました!!

    • attosoft says:

      更新されたjsに差し替えましたところ、正常に動作しました!

      問題が解決されたようで良かったです。AnimateImage スクリプトのバグだったのに、余計なお手間を取らせてしまいすいませんでした。

  5. SAsa says:

    attosoft 様

    はじまして。今回こちらのjsを使用させていただかこうかと考えているのですが、アニメーションを繰り返さずに1回にして、アニメーション終了時のイベントを取得することは可能でしょうか?

    こちらがやりたいこととしては、アニメーション終了後に別の全くアニメーションを動かすことになります。もし簡単に終了イベント等取る方法がありましたらご教授いただけたらと思います。

    • attosoft says:

      コメントありがとうございます。attosoft です。

      アニメーションを繰り返さずに1回にして、アニメーション終了時のイベントを取得することは可能でしょうか?

      こちらがやりたいこととしては、アニメーション終了後に別の全くアニメーションを動かすことになります。もし簡単に終了イベント等取る方法がありましたらご教授いただけたらと思います。

      AnimateImage の API 仕様はこのページに記載してある通りですので、残念ながらイベントには対応していません。イベントの仕組みを利用したい場合は、そのように改変する必要があります。(改変と言ってもイベントハンドラを呼び出すだけですが)

      あるいは希望のアニメーションが「アニメーションA → アニメーションB → 終了」であるならば、アニメーションA とアニメーションB の画像ファイルからなる配列を生成して引数 files に指定する方法が一番簡単だと思います。

  6. Jeeseong Chung says:

    Great script… I'm using this script for my presentation…