Fun with code (since 2006)

AS3 GIF Animation Encoding Class 0.1

05.23.07 Posted in Actionscript 3, ByteArray by

I have to admit that I just love animated GIF's, it reminds me the old school days playing the SNES ;)

Recently I found a java GIF encoder and decided to implement it in AS3, ok maybe some people will say it's just unuseful but here it is, I love it !

Everything is encoded through the player. Then you know the story, the GIF stream is sent through AMFPHP to a server to download it, and then save it to your hard disk. Any kind of images can be included in the final GIF, each frame is a flash.display.BitmapData object.

I'll post the sources in a few days, I want to add some more features, stay tuned !

Piterwilson did a funny experiment with this and the GIFPlayer class.

Here is how to generate an animated GIF stream in ActionScript 3 :

// we create the GIF encoder
var myGIFEncoder:GIFEncoder = new GIFEncoder();
// we call the start method to start a new GIF file stream
// we call the setRepeat method with 0 as parameter so that it loops
//we specify each frame delay in ms before each addFrame call
myGIFEncoder.setDelay ( 100 );
//we add each BitmapData as frames with the addFrame method
myGIFEncoder.addFrame( new BitmapData ( 200, 200 ) );
// we finalize the GIF stream by calling the finish method
// then you retrieve the GIF ByteArray with the stream getter
var myGIFStream:ByteArray =;

Online Demo :

A Flash animation should have appeared here, but it seems that your browser has an older version of the Flash Player or it is not installed at all. Please, install the last release of the Flash Player now, then reload this page.

The GIF Encoder looks nice on the Web Flash Festival 2007 screen :)

GIF Encoder on Screen

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.

Sources and documentation available at

71 Responses to “AS3 GIF Animation Encoding Class 0.1”

  1. Thibault Imbert says:

    Hi Kaiser,

    Sorry for the late response. Cool you found the problem, I will patch the latest version so that it automatically resets the pointer to 0.

    Thanks ;)


  2. Marek says:

    First i would like to thank you for the gif classes, CUrrently i’m working on an project where i need to save gifs wirh reduced colors – 128,64,32 or 16. Is there an option in the gif encoder class to reduce colors? I have tried a few things – reduced the colors in the NeuQuant Class but that didnt work. What would you do to reduce colors an export an animated gif? I suppose you havve to create a palette of (for example 64 colors) for all frames!?

  3. Benjamin says:

    What about my problem? Will there be a resizing method?
    My problem is the color palette for every frame. It takes to much time to calculate it for every frame. Can i take the color palette from the given animation and use it for the resized animation?

    Thanks and Greetings

  4. Hi I am Japanese says:


    I am Japanese.

    this message was created by Google Translate.

    Can I use the commercial?

    I’m trying to sell the software with this encoder.

    if I can use the commercial I link your page.

    I am sorry.

    I can not speak English : (

    please mail my e-mail address.

  5. Thibault Imbert says:

    Hi my Japanese friend :)

    Of course you can use it commercially. No problem ;)



  6. Hi I am Japanese says:

    Thank you very much!!

  7. Hi I am Japanese says:

    My nickname is Somila.
    Nice to meet you!

    Can this encoder create a transparent gif?

    I can speak a little English:)

  8. [...] ก่อนหน้านี้ ที่เว็ป ก็มีคลาสที่ใช้ ByteArray มาเพิ่มขีดความสามารถให้แฟลช ด้วยการสร้างคลาสที่จะ แสดงผล GIF Animation ได้ หรือคลาสที่จะสามารถให้แฟลช สร้างไฟล์ Gif Animation ได้ [...]

  9. Stone says:

    When I save the GIF decoder’s stream to a file, it cannot be displayed correctly. Why….

  10. Abarrow says:

    I’ve been using this class in an image editing program I’ve been writing and I’ve noticed a problem. When I load an image with thousands of colors and try to save it as a gif it fails. Is there anyway that I/you could modify this class to allow it to generate pallets when given an obscene number of different colors?

  11. [...] Actionscript 3 Experiments and Flash Player love ;) < AS3 GIF Animation Encoding Class 0.1 [ by Thibault Imbert ] Introducing AlivePDF! [...]

  12. Eldar says:

    Thank you for your job!

    I have some problem. I add 8 frames by this manner:
    // creating Array of BitmapData in loop by snap some sprite;
    var gifSource:BitmapData = new BitmapData (106, 106, false, 0xf29a9a);
    gifSource.draw(spriteCont, null, null, null);

    // then add them to GIF
    myGIFEncoder = new GIFEncoder();
    myGIFEncoder.setDelay ( 1000 );



    And I have only 3rd frame with transparent background:

    Can you say me what is wrong.

  13. [...] numerous amazing AS3 libraries! Last year i used his AS3 PDF generation library, and now i used his AS3 GIF Animation Encoding Library for an online gif89a [...]

  14. Forrest says:

    How to use the transparency of a bitmapdata that already has an alpha channel?
    AND If I setTransparent(0×00000000) only a few frames are converted correctly..what’s wrong?
    I really need this wonderful class :(

  15. Forrest says:

    still no luck, this class doesn’t handle transparency correctly…Thibault can you help? :(

  16. Roman says:

    Thank you for all.
    Thibault, Can you help me please?
    When I created some a gif, class encoder created stream for a gif, so when I want to play created gif, i use .loadBytes(bytes) method, but it don’t play loaded by stream (((
    But .load(url) method perfectly works.
    What my wrong?

  17. Keyneko says:

    Hello, thanks for your classes,
    but this gifplayer doesn’t handle transparency correctly, what method can be solved?

  18. [...] またもやライブラリの作例を改変。 作例がFlashIDE+FlashPlayer9向けなので、FlashDevelop+FlashPlayer10向けに書き換えてみた。要はflaファイルを不要にしたのと、ファイルの保存をFileReferenceのsave()を使うようにした。 そんで、かなりシンプルにして、ライブラリの最新版(0.6)を使うようにした(作例では、0.1)。 [...]

  19. [...] AS3 GIF Animation Encoding Class 0.1 by Thibault Imbert [...]


Leave a Reply

Open Sort Options

Sort comments by:
  • * Applied after refresh
Fun with code since 2006.