function CameraModule.animateToCanvasAsync(r,w)r.camera.CameraType=Enum.CameraType.Scriptable r.initialCFrame=r.camera.CFrame r.surfaceGui=w local e=r.surfaceGui.Adornee local b=r.surfaceGui.Face local V=e.Position local O=e.CFrame:VectorToWorldSpace(Vector3.FromNormalId(b))local function Z(w,r)local e=math.rad(w/2)local b=(r+CANVAS_OFFSET_PADDING)/2 return b/math.tan(e)end local Y=r.camera.ViewportSize.X/r.camera.ViewportSize.Y local U=Z(r.camera.FieldOfView,w.adornee.Size.Y)local L=Z(r.camera.FieldOfView*Y,w.adornee.Size.X)local A=math.max(L,U)A=math.clamp(A,MIN_STUDS_OFFSET,MAX_STUDS_OFFSET)O*=A local p=r:_getUpVectorByFace(e.CFrame,b)local s=CFrame.lookAt(V+O,V,p)if b==Enum.NormalId.Top then s*=CFrame.Angles(0,0,math.pi)end r:_tweenCamera(s)end

function CameraModule:animateToCanvasAsync(surfaceGui)
	self.camera.CameraType = Enum.CameraType.Scriptable

	-- Store this so we can reset to this position later on
	self.initialCFrame = self.camera.CFrame
	self.surfaceGui = surfaceGui

	local adornee = self.surfaceGui.Adornee
	local face = self.surfaceGui.Face
	local canvasPosition = adornee.Position

	-- Look into the middle of SurfaceGui from a distance
	local offset = adornee.CFrame:VectorToWorldSpace(Vector3.FromNormalId(face))

	-- Compute offset distance dynamically to make sure entire canvas is in field of view
	local function getDistanceToCanvas(fov, size)
		local angle = math.rad(fov / 2)
		local opp = (size + CANVAS_OFFSET_PADDING) / 2

		return opp / math.tan(angle)
	end

	local aspectRatio = self.camera.ViewportSize.X / self.camera.ViewportSize.Y
	local vertical = getDistanceToCanvas(self.camera.FieldOfView, surfaceGui.adornee.Size.Y)
	local horizontal = getDistanceToCanvas(self.camera.FieldOfView * aspectRatio, surfaceGui.adornee.Size.X)

	local distance = math.max(horizontal, vertical)
	distance = math.clamp(distance, MIN_STUDS_OFFSET, MAX_STUDS_OFFSET)
	offset *= distance

	local upVector = self:_getUpVectorByFace(adornee.CFrame, face)
	local target = CFrame.lookAt(canvasPosition + offset, canvasPosition, upVector)

	-- Top view has a weird bug that turns things upside down
	if face == Enum.NormalId.Top then
		target *= CFrame.Angles(0, 0, math.pi)
	end

	self:_tweenCamera(target)
end
