Having trouble figure out the math to make a frame position relative to the mouse as well as the CanvasPosition of a frame

I am making a system where you can move a frame inside of a scrollingFrame based on the mouse position.

Although I got the frame to follow the mouse position, it does not work how I want it to when I scroll.

--Code to make it follow the Mouse
local xS = ((- layersScroll.AbsolutePosition.X + mouse.X) / layersScroll.AbsoluteCanvasSize.X)
					
clone.Position = UDim2.new(xS,0,.5,0)

Along with minor changes in the math for xS, this is my current xS variable, which works a lot better, but still not what I want.

local xS = ((- layersScroll.AbsolutePosition.X + mouse.X) / layersScroll.AbsoluteCanvasSize.X) * (1 + (layersScroll.CanvasPosition.X/(layersScroll.AbsoluteCanvasSize.X - layersScroll.AbsoluteSize.X)))

I know it’ll have something to do with the CanvasPosition.X but I just can’t figure out what I need to do. I keep trying to divide and multiply things, but nothing has worked yet, hence why I came here.

1 Like

For extra info, this gets the canvas position as a scale

(layersScroll.CanvasPosition.X/(layersScroll.AbsoluteCanvasSize.X - layersScroll.AbsoluteSize.X))

You will need to take the scrolling position of the scrolling frame into account when calculating the position of the frame. You can do this by using the CanvasPosition property of the scrolling frame, which gives the position of the scrolling frame’s content within the frame.
Example:

local xS = (-layersScroll.AbsolutePosition.X + mouse.X) / layersScroll.AbsoluteCanvasSize.X

xS = xS * (1 + (layersScroll.CanvasPosition.X / (layersScroll.AbsoluteCanvasSize.X - layersScroll.AbsoluteSize.X)))

clone.Position = UDim2.new(xS, 0, 0.5, 0)

In this code, we use the CanvasPosition.X value to calculate a factor that is used to adjust the position of the frame. This factor is equal to 1 when the scrolling frame is scrolled all the way to the left, and decreases as the scrolling frame is scrolled to the right. This means that the frame will move more quickly when the scrolling frame is scrolled to the left, and more slowly when the scrolling frame is scrolled to the right.

I already had this, and like I said in my post, it didn’t work

After explaining what my code did, and what I needed to be done, ChatGPT AI figured this out for me. This absolutely blows my mind.

local xS = ((- layersScroll.AbsolutePosition.X + layersScroll.CanvasPosition.X + mouse.X) / layersScroll.AbsoluteCanvasSize.X)
clone.Position = UDim2.new(xS,0,.5,0)

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