Converting position offset to scale relative to different things?

Hello,

Currently I have a drawing feature in my game where it places a frame at the mouse position, and it works great if I’m just using offset. Problem is, I’ll need to take that drawing, and be able to render it on many different frames (same aspect ratio, different sizes). So, I was thinking that I would need to convert the positional value to scale.

I’ve tried dividing by the viewport, but the problem is, this is relative to the screen, rather than the frame (see video below).

function Signing:_offsetToScale(x, y)
	local viewport = workspace.Camera.ViewportSize
	
	return x / viewport.X, y / viewport.Y
end


Is it possible to make it relative to the frame, or should I take a different approach in how I use offset so I can render the drawing on different scales?

Did you try AbsoluteSize on the frame?

1 Like

Dividing by it? Yeah, I did but they went out of the canvas instead:

image

Okay, you’re supposed to divide mouse position by viewport size, and then you parent the “ink” into the frame and use scale position.

I think that’s what I am currently doing, but I don’t get what you mean

Use the mouse/viewport scale or something else?

function Signing:_offsetToScale(x, y, canvas)
	local viewport = canvas.AbsoluteSize
	
	return x / viewport.X, y / viewport.Y
end

function Signing:_drawAt(x, y, canvas)

	local point = Instance.new("Frame")
    --x & y come from the mouse pos

	x, y = Signing:_offsetToScale(x, y, canvas)
	
	point.Position = UDim2.fromScale(x, y)
	point.Parent = canvas

    -- code is truncated, but this is the part you are referring to 
end

If your frame is in the exact center of the screen, you could possibly apply an offset of the mouse relative to the center of the frame as the position of the points.

1 Like

How would I approach doing this?

I think you have to get the position of the mouse relative to the window position and divide by the absolute size of the window, like this:

local mouse_pos = UserInputService:GetMouseLocation() - Vector2.new(0, 36)
local rel_pos = (mouse_pos - window.AbsolutePosition) / window.AbsoluteSize

point.Position = UDim2.fromScale(rel_pos.X, rel_pos.Y)

window is the frame you’re painting on.

1 Like

Thanks!

I removed the - Vector2.new(0, 36) as all it did was apply an offset to the final product, but once I did, it worked great.

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