1. --!movie
  2.  
  3. global $
  4.  
  5. ----------------------------------------
  6. --
  7. ----------------------------------------
  8. on startMovie
  9.  
  10.     -- CONFIG
  11.     NUM_COLORS = 64
  12.     DUR = 50 -- 50 ms = 20 fps
  13.  
  14.     -- libs
  15.     $.import("console").show()
  16.  
  17.     -- create an instance of the AWEBP lib
  18.     awebp = $.include($.PATH&"awebp.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_image = lena.duplicate()
  44.     master_image.copyPixels(blue_square, blue_square.rect, blue_square.rect, [#maskImage:circle_mask])
  45.     palette_colors = awebp.findPalette(master_image, NUM_COLORS)
  46.  
  47.     out("Generating animated WebP file...")
  48.  
  49.     ms = the milliseconds
  50.  
  51.     -- initialize AWEBP (i.e. start creation of new WebP file)
  52.     awebp.init(0, palette_colors)
  53.  
  54.     -- add frames in a loop
  55.     repeat with i = 0 to 103
  56.         -- create frame image
  57.         -- the image will be mapped by AWEBP lib to the color list we passed to init()
  58.         frame_image = lena.duplicate()
  59.         frame_image.copyPixels(blue_square, blue_square.rect.offset(i*4, i*4), blue_square.rect, [#maskImage:circle_mask])
  60.         awebp.addFrame(frame_image, DUR)
  61.     end repeat
  62.  
  63.     ms = the milliseconds-ms
  64.  
  65.     -- save animation as WebP file
  66.     ok = awebp.writeFile($.PATH&"animation_circle_"&NUM_COLORS&".webp")
  67.     if ok then
  68.         out("Done. Animated WebP file was generated in "&ms&" milliseconds.")
  69.     else
  70.         out("Error: failed to generate WebP file.")
  71.         return
  72.     end if
  73. end
  74.  
[raw code]