Tweener - is a single file Lua module for the Defold game engine. It provides a way to handle manual tweening in your game.
- Tweening: Create tweens for any action your want.
- Easing Functions: Provides a set of easing functions for different types of easings.
- Callbacks: Callbacks for each tween update.
- Custom Easings: Support for custom easing functions.
- Custom Update Frequency: Option to define update frequency for the tween.
Open your game.project file and add the following line to the dependencies field under the project section:
https://github.com/Insality/defold-tweener/archive/refs/tags/6.zip
Note: The library size is calculated based on the build report per platform
| Platform | Library Size |
|---|---|
| HTML5 | 3.28 KB |
| Desktop / Mobile | 6.21 KB |
Optionally, you can setup global default update frequency in your game.project. It's 60 by default.
This is a global settings for the tweener. You can specify the update frequence parameter in the tweener.tween function for special tween operation.
Add next tweener section to your game.project in text mode:
[tweener]
update_frequency = 60-- Tween function can be a string, a predefined easing function, or a custom easing function
local tween_function = "linear" or tweener.linear or go.EASING_LINEAR or gui.EASING_LINEAR or {0, 0.2, 0.4, 0.8, 0.9, 1}
local tween = tweener.tween(tween_function, from, to, time, callback, [dt])
tweener.cancel(tween)
tweener.set_pause(tween, true)
tweener.is_paused(tween)
tweener.is_active(tween)
local value = tweener.ease(tween_function, from, to, time, time_elapsed)To start using the Tweener module in your project, you first need to import it. This can be done with the following line of code:
local tweener = require("tweener.tweener")The Tweener module provides two primary functions to work with tweens:
tweener.tween(tween_function, from, to, time, callback, [dt])This function initiates a tween operation immediately. Here's how to use it:
-
Parameters:
tween_function: The tween function. You can use one of the predefined easing functions or provide a custom one.from: The starting value of the tween.to: The ending value of the tween.time: The duration of the tween, in seconds.callback: Afunction(value, is_final_call, time_elapsed, time_total)that gets called upon each update of the tween.dt(optional): The time interval for updating the tween, in seconds.
-
Return Value:
tween: A tween object. You can use it to cancel the tween, check if it still running, or pause it.
-
Usage Example:
tweener.tween(tweener.linear, 0, 100, 1.5, function(value, is_final_call)
print("Tween value: " .. value)
end)
tweener.tween(go.EASING_OUTSINE, 0, 100, 1.5, function(value, is_final_call)
print("Tween value: " .. value)
end)
local tween = tweener.tween({0, 0.2, 0.4, 0.8, 0.9, 1}, 0, 100, 1.5, function(value, is_final_call, time_elapsed, time_total)
print("Tween value: " .. value)
print("Time elapsed: " .. time_elapsed .. " from " .. time_total .. " seconds")
end)
-- You can cancel the tween by calling tweener.cancel
tweener.cancel(tween)tweener.ease(tween_function, from, to, time, time_elapsed)This function calculates the value of the tween at a specific point in time, based on the easing function provided.
-
Parameters:
tween_function: The tween function.from: The starting value of the tween.to: The ending value of the tween.time: The total duration of the tween, in seconds.time_elapsed: The elapsed time since the start of the tween, in seconds.
-
Usage Example:
local value = tweener.ease(tweener.inquad, 0, 100, 1.5, 0.75)
print("The tween value at halfway point is: ", value)
local value = tweener.ease(gui.EASING_OUTSINE, 0, 100, 1.5, 0.75)
print("The tween value at halfway point is: ", value)
local value = tweener.ease({0, 0.2, 0.4, 0.8, 0.9, 1}, 0, 100, 1.5, 0.75)
print("The tween value at halfway point is: ", value)tweener.cancel([tween])This function cancels the tween with the given tween object.
-
Parameters:
tween: The tween object to cancel.
-
Return Value:
trueif the tween was successfully canceled,falseotherwise.
-
Usage Example:
local tween = tweener.tween(tweener.linear, 0, 100, 1.5, function(value, is_final_call)
print("Tween value: " .. value)
end)
tweener.cancel(tween)tweener.is_paused(tween)This function returns true if the tween is paused, false otherwise.
-
Parameters:
tween: The tween object to check. It returned fromtweener.tweenfunction.
-
Return Value:
trueif the tween is paused,falseotherwise.
tweener.set_pause(tween, is_paused)This function sets the pause state of the tween.
-
Parameters:
tween: The tween object to set the pause state. It returned fromtweener.tweenfunction.is_paused: The new pause state.
-
Return Value:
trueif the tween was successfully paused,falseotherwise.
-
Usage Example:
local tween = tweener.tween(tweener.linear, 0, 100, 1.5, function(value, is_final_call)
print("Tween value: " .. value)
end)
tweener.set_pause(tween, true)
tweener.is_paused(tween) -- Returns truetweener.is_active(tween)This function returns true if the tween is running, false is the tween is finished.
-
Parameters:
tween: The tween object to check. It returned fromtweener.tweenfunction.
-
Return Value:
trueif the tween is running,falseis the tween is finished.
-
Usage Example:
local tween = tweener.tween(tweener.linear, 0, 100, 1.5, function(value, is_final_call)
print("Tween value: " .. value)
end)
tweener.is_active(tween) -- Returns true
-- Wait for 1.5 seconds
tweener.is_active(tween) -- Returns falseYou can use Tweener to animate scoring text, for example:
This animation can be created using the following code:
tweener.tween(gui.EASING_OUTCIRC, 0, 9999, 2.4, function(value, is_final_call)
gui.set_text(text_score, "Score: " .. math.floor(value))
if is_final_call then
gui.set_scale(text_score, vmath.vector3(1.25, 1.25, 1))
gui.animate(text_score, "scale", vmath.vector3(1, 1, 1), gui.EASING_OUTBOUNCE, 0.5)
end
end)You can obtain the value of the tween at any point in time with the tweener.ease function:
tweener.ease(gui.EASING_OUTSINE, 0, 100, 1, 0) -- Returns 0
tweener.ease(gui.EASING_OUTSINE, 0, 100, 1, 0.25) -- Returns 38.268343236509
tweener.ease(gui.EASING_OUTSINE, 0, 100, 1, 0.5) -- Returns 70.710678118655
tweener.ease(gui.EASING_OUTSINE, 0, 100, 1, 0.75) -- Returns 92.387953251129
tweener.ease(gui.EASING_OUTSINE, 0, 100, 1, 1) -- Returns 100This project is licensed under the MIT License - see the LICENSE file for details.
If you have any issues, questions or suggestions please create an issue.
- Initial release
- Changed timer
delta timetosocket.gettimefor more precise tweening
- Added custom easings support
- Fix
update_frequencygame.project settings typo - Now able to use string name of easing functions (ex. "linear", "insine", "outquad", etc.)
- Add
tweener.cancelfunction to cancel tweening instead oftimer.cancel(For better API and README) - Code cleanup and better performance
- Fix if total time is 0, then callback will be called immediately
- [Breaking]:
tweener.tweennow returns a tween object instead of a timer id. So if you usedtimer.cancelto cancel the tween, you need to usetweener.cancelinstead. - Added
tweener.set_pausefunction to pause and resume a tween - Added
tweener.is_pausedfunction to check if a tween is paused - Added
tweener.is_activefunction to check if a tween is active
- Add
time_elapsedandtime_totalparameters to the tween callback function - The
tweener.cancelnow can be called with anilobject to escape the additionalifcheck
Your donation helps me stay engaged in creating valuable projects for Defold. If you appreciate what I'm doing, please consider supporting me!

