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 PNG, 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 APNG lib
  18.     apng = $.include($.PATH&"apng.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.  
  25.     -- create blue square image
  26.     blue_square = image(100, 100, 32)
  27.     blue_square.fill(blue_square.rect, rgb(0,0,255))
  28.  
  29.     -- create a circle mask
  30.     m = new(#bitmap)
  31.     m.importFileInto($.PATH&"input\circle.bmp", [#trimWhitespace: FALSE])
  32.     alpha = image(100, 100, 8, #grayscale)
  33.     alpha.copyPixels(m.image, alpha.rect, alpha.rect)
  34.     circle_mask = alpha.createMask()
  35.    
  36.     ----------------------------------------
  37.     -- Create a "master image" for finding an optimzed color palette.
  38.     -- In this case we simply copy the blue circle on top of the photo.
  39.     -- in other cases - if different frames of your animation use completely different
  40.     -- colors - such a master image could be created by assembling various or all frames
  41.     -- to a large image.
  42.     ----------------------------------------
  43.     master = lena.duplicate()
  44.     master.copyPixels(blue_square, blue_square.rect.offset(20, 20), blue_square.rect, [#maskImage:circle_mask])
  45.     palette_colors = apng.findPalette(master, NUM_COLORS)
  46.  
  47.     out("Generating APNG file...")
  48.  
  49.     ms = the milliseconds
  50.  
  51.     -- initialize APNG (i.e. start creation of new APNG file)
  52.     apng.init(0, palette_colors)
  53.  
  54.     -- add static background photo, immediately go to next frame (ms=0)
  55.     apng.addFrame(lena, 0)
  56.  
  57.     -- add frames in a loop
  58.     frame_image = image(100, 100, 24)
  59.     repeat with i = 0 to 103
  60.         -- add square with circle image on top of background photo
  61.         -- resulting image will be mapped by APNG lib to the color list we passed to init()
  62.         frame_image.copyPixels(lena, frame_image.rect, frame_image.rect.offset(i*4, i*4))
  63.         frame_image.copyPixels(blue_square, blue_square.rect, blue_square.rect, [#maskImage:circle_mask])  
  64.         apng.addFrame(frame_image, DUR, i*4, i*4, apng.DISPOSE_OP_PREVIOUS, apng.BLEND_OP_OVER)
  65.     end repeat
  66.  
  67.     ms = the milliseconds-ms
  68.  
  69.     -- save the APNG as file
  70.     ok = apng.writeFile($.PATH&"animation_circle_"&NUM_COLORS&".png")
  71.     if ok then
  72.         out("Done. APNG was generated in "&ms&" milliseconds.")
  73.     else
  74.         out("Error: failed to generate APNG file.")
  75.         return
  76.     end if
  77.  
  78. end
  79.  
[raw code]