Scale to Offset Math

I’m trying to convert an objects size in scale to offset via math

See how it says the size in offset on the bottom right of the check image even though it’s in scale, I’m trying to replicate this, but in a script

I’ve tried looking through the forum and the web and the only things I could find are plugins but I need the math

If anyone knows the math, or other solutions post below :slight_smile:

2 Likes

offset_x = screen_size.X * scale.X

You get the screen size by looking at the AbsoluteSize property of the ScreenGui

10 Likes

The object’s scale property is based on the parent’s AbsoluteSize/AbsolutePosition. All you need to do is multiply the scale value by the AbsolutePosition of the parent.

Edit:
I misread the post…
offset = UDim2.new(0, image.Parent.AbsolutePosition.X*image.Position.X.Scale+image.Position.X.Offset, 0, image.Parent.AbsolutePosition.Y*image.Position.Y.Scale+image.Position.Y.Offset)

11 Likes

Thanks, both of you :smiley:

I wish I could give two solutions but I went with @Hexcede’s because he provided more a more in-depth solution

I found a better method by accident going to post it here because it’s more efficient as you don’t need to do the math to get the offset from scale, and this works with constraints such as
UIAspectRatioConstraint

you take the absolute size directly from the GuiObject you want to convert into offset from scale as shown below

image
there is no need to do the math if you do it this way

Example:

function Resize_Offset()
local AB_Size = LogoBase.Parent.AbsoluteSize
local LB_Size = LogoBase.AbsoluteSize
--Logo.Size = UDim2.new(0, AB_Size.X * LB_Size.X, 0, AB_Size.Y * LB_Size.Y) -- Previous Method which uses math then sets the UDim2
  Logo.Size = UDim2.new(0, LB_Size.X, 0, LB_Size.Y) -- newer method which just reads a value and then sets the UDim2
  print(LB_Size)
end
15 Likes

Adding onto this, I use this method when I display images in an inventory/shop. I would divide the AbsoluteSize.X by a certain number, and assign the offset on X and Y to that offset.

2 Likes

Keep in mind that this method will break if you’re using a folder, and it may be a better idea to use image:FindFirstAncestorWhichIsA("GuiBase2d") than image.Parent.

Edit: Replied to the wrong post, meant to reply to @Hexcede.

2 Likes

My bad, I meant to reply to someone else. Your method is definitely fine.

3 Likes