--!movie
global $
----------------------------------------
--
----------------------------------------
on startMovie
-- CONFIG
NUM_COLORS = 64
DUR = 50 -- 50 ms = 20 fps
-- libs
$.import("console").show()
-- create an instance of the AWEBP lib
awebp = $.include($.PATH&"awebp.ls").new()
-- load background image from file
m = new(#bitmap)
m.importFileInto($.PATH&"input\lena.bmp")
lena = m.image
-- create blue square image
blue_square = image(100, 100, 32)
blue_square.fill(blue_square.rect, rgb(0,0,255))
-- create a circle mask
m = new(#bitmap)
m.importFileInto($.PATH&"input\circle.bmp", [#trimWhitespace: FALSE])
alpha = image(100, 100, 8, #grayscale)
alpha.copyPixels(m.image, alpha.rect, alpha.rect)
circle_mask = alpha.createMask()
----------------------------------------
-- Create a "master image" for finding an optimzed color palette.
-- In this case we simply copy the blue circle on top of the photo.
-- in other cases - if different frames of your animation use completely different
-- colors - such a master image could be created by assembling various or all frames
-- to a large image.
----------------------------------------
master_image = lena.duplicate()
master_image.copyPixels(blue_square, blue_square.rect, blue_square.rect, [#maskImage:circle_mask])
palette_colors = awebp.findPalette(master_image, NUM_COLORS)
out("Generating animated WebP file...")
ms = the milliseconds
-- initialize AWEBP (i.e. start creation of new WebP file)
awebp.init(0, palette_colors)
-- add frames in a loop
repeat with i = 0 to 103
-- create frame image
-- the image will be mapped by AWEBP lib to the color list we passed to init()
frame_image = lena.duplicate()
frame_image.copyPixels(blue_square, blue_square.rect.offset(i*4, i*4), blue_square.rect, [#maskImage:circle_mask])
awebp.addFrame(frame_image, DUR)
end repeat
ms = the milliseconds-ms
-- save animation as WebP file
ok = awebp.writeFile($.PATH&"animation_circle_"&NUM_COLORS&".webp")
if ok then
out("Done. Animated WebP file was generated in "&ms&" milliseconds.")
else
out("Error: failed to generate WebP file.")
return
end if
end