Lerpman - a small resource for making easy lerping

lerpman is a short yet useful resource for lerping. what makes it special? it adds lots of easing styles for lerping. here is an example usage:

local lerpMan = require(path.to.lerpman)
local part = workspace.Part
local starting = 0
local ending = 1
for alpha = 0, 1, 0.01 do
   part.Transparency = lerpMan.lerp(starting, ending, alpha, lerpMan.lerpingStyles.In.quad)
end

It makes the parts transparency go away with a smooth easing style!
here is the module’s source:

local lerpMan = {}
lerpMan.lerpStyles = {
   In = {},
   Out = {}
}
function lerpMan.lerpStyles.In.quad(progress)
   return progress ^ 2
end
function lerpMan.lerpStyles.In.quart(progress)
   return progress ^ 4
end
function lerpMan.lerpStyles.In.expo(progress)
   return 2 ^ (10 * progress - 10)
end
function lerpMan.lerpStyles.In.circ(progress)
   return 1 - math.sqrt(1 - (progress ^ 2))
end
function lerpMan.lerpStyles.In.cubic(progress)
   return progress ^ 3
end
function lerpMan.lerpStyles.Out.expo(progress)
   return progress == 1 and 1 or (1 - (2 ^ (-10 * progress)))
end
function lerpMan.lerpStyles.Out.quad(progress)
   return 1 - ((1 - progress) ^ 2)
end
function lerpMan.lerpStyles.Out.cubic(progress)
   return 1 - ((1 - progress) ^ 3)
end
function lerpMan.lerpStyles.Out.quart(progress)
   return 1 - ((1 - progress) ^ 4)
end
function lerpMan.lerpStyles.Out.circ(progress)
   return math.sqrt(1 - ((progress - 1) ^ 2))
end
function lerpMan.lerp(startValue, endValue, alpha, easingFunction)
   if easingFunction then
      alpha = easingFunction(alpha)
   end
   return startValue + (endValue - startValue) * alpha
end
return lerpMan

Put it in a module script.
hope it helps.

2 Likes

Is there a reason this would be better than using TweenService? And yes, it also does support retrieving a value from a position.

This will only support data types you can operate on, which doesn’t include data types such as UDim.

Your code with Roblox’s TweenService using GetValue:

-- Services
local Tween = game:GetService("TweenService")

-- Your Code (Adjusted):
local part = workspace.Part
local starting = 0
local ending = 1

for alpha = 0, 1, 0.01 do
   local tweenedAlpha = Tween:GetValue(
        alpha,
        Enum.EasingStyle.Quad, Enum.EasingDirection.In
   )

   part.Transparency = (starting + ((ending - starting) * tweenedAlpha))
end

My suggestion for this module (as to not entirely discredit it) is that you make it simply return a function that takes a style, direction, and alpha (just like TweenService:GetValue) and have it do the interpolation logic as a shortcut. You could also have it index a custom-interpolation table to get custom styles/directions as well (style would be (string | Enum.EasingStyle) and direction would remain Enum.EasingDirection). To me, this would serve a much greater use than basically replicating TweenService:GetValue with equations found from the internet.

As a side-note: You also provide less built-in easing styles than what TweenService already provides - most notably - sine.

2 Likes

You can just use some numbers thatll take a few more lines but create a UDim with the numbers you generated instead of directly creating the primitive type that does not want arthimetics.

Yes

  1. Lua supported (if that counts)
  2. You can add more easing styles (planned to make more builtin easing styles) instead of only having a specific list of easing styles.

Read the essay i made above ^

Thats technically what im doing. (if i understood correctly) read the example code.

Yeah. Im planning to add WAYY more easing styles and make the module well, better. For now, i took 3 days writing this. and i posted it here for feedback obv. Thanks

Well the point to take away was that your built-in styles/directions are already accounted for with the built-in TweenService:GetValue functionalities - so you shouldn’t try to add anything that it already covers and instead add styles that aren’t covered by it - otherwise default to TweenService:GetValue. That’s my primary feedback to save you time with implementation.

Ohh i get it now. The problem is i want this module to be usable for lua too, so i either have to switch it to luau roblox completely, to split the code in half, or keep yhe modulr in its way to make it for lua too.

Well for Community Resources everything is expected to be used on Roblox for Roblox; sometime some of those Resources just so happen to be compatible with vanilla lua - but thats just because some of those resources don’t have to interface with Roblox content at all.

I would focus on making a great module that can be used on Roblox - by other Roblox Developers (which are here on the DevForum)

Alr. I will kinda default it to tween:getvalue. but ill add much more easing styles tweenservice dosent have.

1 Like

Thank you, this helped a lot. Nice work, you should put this in GitHub.

In the usage example below, it should be lerpStyles instead of lerpingStyles.

part.Transparency = lerpMan.lerp(starting, ending, alpha, lerpMan.lerpStyles.In.quad)

Regarding building UDims with this

Size = size:map(function(value)
    local x = lerpMan.lerp(startSizeX, endSizeX, value, lerpMan.lerpStyles.Out.quart)
    local y = lerpMan.lerp(startSizeY, endSizeY, value, lerpMan.lerpStyles.Out.quart)
    return UDim2.new(x, 0, y, 0)
end)