--!movie
global $
----------------------------------------
--
----------------------------------------
on startMovie
-- CONFIG
NUM_COLORS = 128 -- number of colors used by created PNG, in range 3..256
DUR = 50 -- 50 ms = 20 fps
-- libs
$.import("console").show()
-- create an instance of the APNG lib
apng = $.include($.PATH&"apng.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 = lena.duplicate()
master.copyPixels(blue_square, blue_square.rect.offset(20, 20), blue_square.rect, [#maskImage:circle_mask])
palette_colors = apng.findPalette(master, NUM_COLORS)
out("Generating APNG file...")
ms = the milliseconds
-- initialize APNG (i.e. start creation of new APNG file)
apng.init(0, palette_colors)
-- add static background photo, immediately go to next frame (ms=0)
apng.addFrame(lena, 0)
-- add frames in a loop
frame_image = image(100, 100, 24)
repeat with i = 0 to 103
-- add square with circle image on top of background photo
-- resulting image will be mapped by APNG lib to the color list we passed to init()
frame_image.copyPixels(lena, frame_image.rect, frame_image.rect.offset(i*4, i*4))
frame_image.copyPixels(blue_square, blue_square.rect, blue_square.rect, [#maskImage:circle_mask])
apng.addFrame(frame_image, DUR, i*4, i*4, apng.DISPOSE_OP_PREVIOUS, apng.BLEND_OP_OVER)
end repeat
ms = the milliseconds-ms
-- save the APNG as file
ok = apng.writeFile($.PATH&"animation_circle_"&NUM_COLORS&".png")
if ok then
out("Done. APNG was generated in "&ms&" milliseconds.")
else
out("Error: failed to generate APNG file.")
return
end if
end