I have a line tool but when going very fast it seems to make multiple lines and acts strange.
Example: https://streamable.com/lfv6df
My code:
-- Functions
function PosIsInDrawFrame(pos)
pos = pos - Canvas.AbsolutePosition
return pos == ClampPosInDrawFrame(pos) --If the position is not on the frame, the position will be not equal to the clamped position
end
function ClampPosInDrawFrame(pos)
local frameSize = Canvas.AbsoluteSize
local minX, maxX = 0, frameSize.X
local minY, maxY = 0, frameSize.Y
local clampedPos = Vector2.new(math.clamp(pos.X, minX, maxX), math.clamp(pos.Y, minY, maxY)) --For lock position to the drawFrame
return clampedPos
end
function GetLinePosition(p1, p2)
local pos = p1:Lerp(p2, .5) --Get the midle between Pos1 and Pos2
pos = pos / Canvas.AbsoluteSize --Convert this position(currently in offset) to scale
return pos.X, pos.Y
end
function GetLineLen(p1, p2)
local len = (p1 - p2).Magnitude --Calculate Euclidean distance for len, similar to math.sqrt((p1.X - p2.X)^2 + (p1.Y - p2.Y)^2)
return len
end
function GetLineOrientation(p1, p2)
local delta = p1 - p2
local abjacent = delta.X
local opposite = delta.Y
local hypotenuse = math.sqrt(abjacent ^ 2 + opposite ^ 2)
local cosinusTheta = abjacent / hypotenuse
local sinusTheta = opposite / hypotenuse
local sign = sinusTheta > 0 and 1 or -1
local orientation = math.acos(cosinusTheta) * sign
return math.deg(orientation)
end
function RenderLine(frame) --Just update the frame posion, size and orientation
frame.Position = UDim2.fromScale(GetLinePosition(startPos, drawPos))
frame.Size = UDim2.fromOffset(GetLineLen(startPos, drawPos), selectedSize)
frame.Rotation = GetLineOrientation(startPos, drawPos)
end
function CreateLine()
local frame = Instance.new("Frame", Canvas)
frame.BackgroundColor3 = selectedColor
frame.BorderSizePixel = 0
frame.Transparency = 0
frame.AnchorPoint = Vector2.new(.5, .5) --Necessary for after, for have the frame position from the midle of the frame
return frame
end
function StartDrawingLine()
isClicking = true --Set mouseDown to true
startPos = drawPos --Set the start position to the current position
local frame = CreateLine()
while isClicking == true do
RenderLine(frame) --Just update the frame on drawing
wait()
end
end
local function corner2center(p: Vector2)
return p * 2 - Vector2.new(1, 1)
end
local function center2corner(p: Vector2)
return p / 2 + Vector2.new(0.5, 0.5)
end
local function redraw()
color = Color3.fromHSV(h, s, v)
fg.ImageColor3 = Color3.fromHSV(h, 1, 1)
local ang = (h * math.pi) * 2 - math.pi / 2
local hpos = center2corner(Vector2.new(math.cos(ang), math.sin(ang)) * 0.9)
hp.Position = UDim2.new(hpos.X, 0, hpos.Y, 0)
hp.Rotation = h * 360
local svpos = center2corner(Vector2.new(s - 0.5, -v + 0.5))
svp.Position = UDim2.new(svpos.X, 0, svpos.Y, 0)
bg:SetAttribute("Color", color)
end
-- Clamp position
InputService.InputChanged:Connect(function(input: InputObject)
if input.UserInputType == Enum.UserInputType.MouseMovement then
local Mouse = game.Players.LocalPlayer:GetMouse()
if colorWheelPressed then
update(Vector2.new(input.Position.X, input.Position.Y))
end
if isMakingLine == true then
local pos = input.Position
pos = Vector2.new(pos.X, pos.Y) - Canvas.AbsolutePosition --Convert to vector2 and remove the DrawFrame absolute positions for has the good position
drawPos = ClampPosInDrawFrame(pos) --Clamp the position for don't have a position out of the frame
end
Starts drawing (Inside of a inputBegan
-- Checks if it is mousebutton1 then--
if isMakingLine == true then
local pos = input.Position
pos = Vector2.new(pos.X, pos.Y) -- Just convert the input position to vector2
if PosIsInDrawFrame(pos) and isMakingLine then --Check if the position is on the draw frame
StartDrawingLine()
end