Skip to content

Instantly share code, notes, and snippets.

@matpratta
Last active August 8, 2023 16:53
Show Gist options
  • Save matpratta/80120e377a9ea2e1e4ee8a2077d5f28c to your computer and use it in GitHub Desktop.
Save matpratta/80120e377a9ea2e1e4ee8a2077d5f28c to your computer and use it in GitHub Desktop.
🖥️ DU Render Script: Seamless Animated Images
-- Demo settings, fill them in with the data from your sprite sheet:
-- - Image: the uploaded link to the image
-- - Cols: number of columns on your sheet
-- - Rows: number of rows on your sheet
-- - FPS: desired frame rate for the animation
-- - Skip: how many frames are missing at end of sheet, used for seamless loops
local image = 'assets.prod.novaquark.com/113304/503a53c7-3fc0-418e-bb59-97e2dfcf1b8d.jpg'
local cols = 19
local rows = 12
local fps = 30
local skip = 3
-- This is the function for rendering an animated image anywhere on your Render Script
-- Feel free to copy this to your own script!
function addAnimatedImage(layer, image, x, y, width, height, cols, rows, fps, skip)
if not image then
return
end
local frameCount = rows * cols - skip
local frame = math.floor(getTime() * fps) % frameCount
local frameX, frameY = frame % cols, math.floor(frame / cols)
local imageWidth, imageHeight = getImageSize(image)
local cropWidth, cropHeight = imageWidth / cols, imageHeight / rows
local cropX, cropY = math.floor(frameX * cropWidth), math.floor(frameY * cropHeight)
addImageSub(layer, image, x, y, width, height, cropX, cropY, cropWidth, cropHeight)
end
-- The code below is only related to the demo, no need to copy it to your own scripts, use as reference
local width, height = getResolution()
local layer = createLayer()
local background = loadImage(image)
addAnimatedImage(layer, background, 0, 0, width, height, cols, rows, fps, skip)
-- This line is required for the animation to work!
requestAnimationFrame(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment