Uistroke that can tween between elements?

Hi,
I want to make a UIstroke that can visibly move across the screen between frames/buttons as a selection indicator, possibly with a tween or a lerp, but I’m not sure how to go about doing this because UIstrokes don’t have a ‘position.’

I’ve considered the idea of making an invisible frame that has a UIstroke, and moving that to the positions of all the buttons inside my parent, but I have a problem that, whilst inside the parent, the UIstroke will actually mess up the layout because I’m using a UIlistlayout.

If I tried to put the invisible stroke frame outside into a frame the same size as the parent frame, I would run into issues because it’s actually a scrollingFrame, so if the player scrolled, the UIstroke wouldn’t update to match the location of the button inside the scrollingFrame, unless I tied it to runservice.heartbeat, which I feel like can’t be the best way to do it. Any thoughts?

why not just have a ui stroke directly on the buttons and then toggle it when the players mouse hovers in/out the button?

i specifically want to replicate the smooth motion of tweening the UIstroke

Could you show an example of what you want to achieve?

can you show your UI demo screenshot?

Have the frame OUTSIDE the parent frame, and Lerp a Udim2 based on what frame you want the UI stoke to be over.

local frame = PathToFrame
local targetFrame

local frameGoal, framePosition = Udim2.new(), framePosition = Udim2.new()

local factor = 30 --// speed of lerp

function LerpPosition(deltaTime)
    if not targetFrame then return end
    frameGoal = targetFrame.AbsolutePosition

    framePosition = framePosition:Lerp(frameGoal, deltaTime * factor)
    frame.Position = framePosition
end

game:GetService("RunService"):BindToRenderStep("LerpStroke", Enum.RenderPriority.Camera.Value, LerpPosition)
1 Like

You could probably have an invisible frame with the UIStroke and move it around depending on the absolute position & size of the selected element on the screen.

For example, I select a button, and it tweens the frame to the absolute position & size of that button.

That’s almost a perfect solution apart from the fact that it causes a weird wobble effect when the player scrolls. Is there any way to mitigate that?

code:

local player = game:GetService("Players").LocalPlayer
local playerGUI = player:WaitForChild("PlayerGui")

local mainUI = playerGUI:WaitForChild("MainUI")
local clippingFrame = mainUI:WaitForChild("ClippingFrame")
clippingFrame.ClipsDescendants = true --important


local mainFrame = mainUI:WaitForChild("MainFrame")

local targetFrame, mouseInMainFrame, frameGoal 
local framePosition = Vector2.new()

local buttons = {}
for _, v in mainFrame:GetDescendants() do
	if v:IsA("TextButton") then
		table.insert(buttons, v)
	end
end


local selector = clippingFrame:WaitForChild("SelectorFrame")
local selectorStroke = selector:WaitForChild("UIStroke")
selectorStroke.Transparency = 1


mainFrame.MouseEnter:Connect(function()
	mouseInMainFrame = true
end)

mainFrame.MouseLeave:Connect(function()
	mouseInMainFrame = false
	selectorStroke.Transparency = 1
end)

for _, button in buttons do
	button.MouseEnter:Connect(function()
		targetFrame = button
		selectorStroke.Transparency = 0
		selector.Size = targetFrame.Size
	end)
end

local factor = 15


function LerpPosition(deltaTime)
	if not targetFrame then return end

	frameGoal = targetFrame.AbsolutePosition -- Vector2
	framePosition = framePosition + (frameGoal - framePosition) * (deltaTime * factor)

	selector.Position = UDim2.new(0, framePosition.X, 0, framePosition.Y)
	local relativePos = framePosition - clippingFrame.AbsolutePosition
	selector.Position = UDim2.fromOffset(relativePos.X, relativePos.Y)
end



game:GetService("RunService"):BindToRenderStep("LerpStroke", Enum.RenderPriority.Camera.Value, LerpPosition)


Place:
showcase.rbxl (68.5 KB)

There’s a really close result in the video I just posted. Sorry, I didn’t know how to recreate it outside of roblox and I couldn’t find anything similar to what I wanted to achieve

Yeah, that’s kind of a downside to having to lerp it.

There’s 2 things you can do:

Complicated:

  1. Make the frame only lerp when the selected frame changes, and once in a certain range a variable triggers and the frame then starts tracking the AbsolutePosition of the selected frame.

something like this:

local trackPosition = false
local snapTarget = --// ??? idk you figure out good numbers

function onFrameChange(newFrame)
    targetFrame = newFrame
    trackPosition = false
end

function lerpPosition(deltaTime)
    if not targetFrame then return end

    frameGoal = targetFrame.AbsolutePosition

    framePosition = trackPosition and frameGoal or framePosition:Lerp(frameGoal, deltaTime * factor)

    selector.Position = Udim2.fromOffset(framePosition.X, framePosition.Y)

    if (selector.AbsolutePosition - frameGoal).Magnitude < snapTarget then
        trackPosition = true
    end
end

Simple:
3. Make the lerping more snappy to reduce the effect.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.