# Fade out frame near edge

I am making a parallax sight for VR, and I am wondering how I would start to fade out an ImageLabel when it’s near the edge of the parenting Frame.

What I want to achieve:

What I have so far:

I just need to detect how close the reticle is to any edge of the frame.

I figured it out with a bit of research, experimenting, and ChatGPT explaining some things

Here is the module I made for it:

``````local module = {}

function getClosestEdge(frame: ScreenGui, point)
local framePos = frame.AbsolutePosition
local frameSize = frame.AbsoluteSize

local leftEdge = framePos
local rightEdge = framePos + Vector2.new(frameSize.X, 0)
local topEdge = framePos
local bottomEdge = framePos + Vector2.new(0, frameSize.Y)

local function distanceToPoint(edge, edgeVertical)
if edgeVertical then
return math.abs(point.X - edge.X)
else
return math.abs(point.Y - edge.Y)
end
end

local function closestDistanceToEdge(edge, edgeVertical)
if edgeVertical then
return math.min(
distanceToPoint(edge, true),
distanceToPoint(edge + Vector2.new(frameSize.X, 0), true)
)
else
return math.min(
distanceToPoint(edge, false),
distanceToPoint(edge + Vector2.new(0, frameSize.Y), false)
)
end
end

local minDistance = math.huge
local closestEdge = nil
local isVertical = true

local leftDistance = closestDistanceToEdge(leftEdge, true)
local rightDistance = closestDistanceToEdge(rightEdge, true)
if leftDistance < minDistance then
minDistance = leftDistance
closestEdge = "Left"
isVertical = true
end
if rightDistance < minDistance then
minDistance = rightDistance
closestEdge = "Right"
isVertical = true
end

local topDistance = closestDistanceToEdge(topEdge, false)
local bottomDistance = closestDistanceToEdge(bottomEdge, false)
if topDistance < minDistance then
minDistance = topDistance
closestEdge = "Top"
isVertical = false
end
if bottomDistance < minDistance then
minDistance = bottomDistance
closestEdge = "Bottom"
isVertical = false
end

return closestEdge, minDistance
end

local self = {}

gui = gui or frame.Parent
local size = gui.AbsoluteSize
local sx,sy = size.X,size.Y

function self:Get()
local pos = frame.AbsolutePosition
local edge,distance = getClosestEdge(gui,pos)

local percent = 0

if edge == "Left" then
end
end
else
end
end
end

local transparency = 0

if percent ~= 0 then
transparency = 1-percent
else
transparency = 0
end

return transparency
end

return self
end

return module
``````
1 Like

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