1. --!movie
  2.  
  3. global $
  4.  
  5. ----------------------------------------
  6. --
  7. ----------------------------------------
  8. on startMovie
  9.  
  10.     -- CONFIG
  11.     NUM_COLORS = 128 -- number of colors used by created GIF, in range 3..256
  12.     DUR = 50 -- 50 ms = 20 fps
  13.  
  14.     -- libs
  15.     $.import("console").show()
  16.  
  17.     -- create an instance of the AGIF lib
  18.     agif = $.include($.PATH&"agif.ls").new()
  19.  
  20.     -- load background image from file
  21.     m = new(#bitmap)
  22.     m.importFileInto($.PATH&"input\lena.bmp")
  23.     lena = m.image
  24.     lena.setAlpha(255)
  25.     lena.useAlpha = TRUE
  26.  
  27.     -- create blue square image
  28.     blue_square = image(100, 100, 32)
  29.     blue_square.fill(blue_square.rect, rgb(0,0,255))
  30.  
  31.     -- create a circle mask
  32.     m = new(#bitmap)
  33.     m.importFileInto($.PATH&"input\circle.bmp", [#trimWhitespace: FALSE])
  34.     alpha = image(100, 100, 8, #grayscale)
  35.     alpha.copyPixels(m.image, alpha.rect, alpha.rect)
  36.     circle_mask = alpha.createMask()
  37.    
  38.     -- create a master image to extract an appropriate palette
  39.     master = lena.duplicate()
  40.     master.copyPixels(blue_square, blue_square.rect.offset(20, 20), blue_square.rect, [#maskImage:circle_mask])
  41.     palette_colors = agif.findPalette(master, NUM_COLORS)
  42.  
  43.     ms = the milliseconds
  44.  
  45.     -- initialize AGIF (i.e. start creation of new animated GIF)
  46.     agif.init(palette_colors, 0)
  47.  
  48.     -- add static background photo, immediately go to next frame (ms=0)
  49.     agif.addFrame(lena, 0)
  50.  
  51.     frame_image = image(100, 100, 24)
  52.  
  53.     repeat with i = 0 to 103
  54.         -- create frame image
  55.         -- the image will be mapped by AGIF lib to the color list we passed to init()
  56.         frame_image.copyPixels(lena, frame_image.rect, frame_image.rect.offset(i*4, i*4))
  57.         frame_image.copyPixels(blue_square, blue_square.rect, blue_square.rect, [#maskImage:circle_mask])
  58.         agif.addFrame(frame_image, DUR, i*4, i*4, agif.DISPOSE_OP_PREVIOUS)
  59.     end repeat
  60.  
  61.     -- save GIF file
  62.     ok = agif.writeFile($.PATH&"animation_circle_"&NUM_COLORS&".gif")
  63.  
  64.     ms = the milliseconds-ms
  65.  
  66.     if ok then
  67.         out("Done. Animated GIF was generated in "&ms&" milliseconds.")
  68.     else
  69.         out("Error: failed to generate animated GIF file.")
  70.     end if
  71. end
  72.  
[raw code]