Advertisement
ERROR_CODE

Paint

Oct 22nd, 2024 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.61 KB | None | 0 0
  1. local function createLine(startPos, endPos)
  2.     local line = Instance.new("Frame")
  3.    -- line.Size = UDim2.new(0, (endPos - startPos).Magnitude, 0, 2)
  4. line.Size = UDim2.new(0, 5, 0, 2)
  5.     line.Position = UDim2.new(0, startPos.X, 0, startPos.Y)
  6.     line.AnchorPoint = Vector2.new(0, 0.5)
  7.     line.Rotation = math.deg(math.atan2(endPos.Y - startPos.Y, endPos.X - startPos.X))
  8.     line.BackgroundColor3 = Color3.new(1, 0, 0)
  9.     line.BorderSizePixel = 0
  10.     line.Parent = Canvas
  11. end
  12.  
  13. local function getRelativePosition(input)
  14.     local absolutePosition = Canvas.AbsolutePosition
  15.     local relativePosition = Vector2.new(input.Position.X - absolutePosition.X, input.Position.Y - absolutePosition.Y)
  16.     return relativePosition
  17. end
  18.  
  19. UserInputService.InputBegan:Connect(function(input)
  20.     if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
  21.         drawing = true
  22.         lastPosition = getRelativePosition(input)
  23.     end
  24. end)
  25.  
  26. UserInputService.InputEnded:Connect(function(input)
  27.     if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
  28.         drawing = false
  29.         lastPosition = nil
  30.     end
  31. end)
  32.  
  33. UserInputService.InputChanged:Connect(function(input)
  34.     if drawing and (input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch) then
  35.         local currentPosition = getRelativePosition(input)
  36.         if lastPosition then
  37.             createLine(lastPosition, currentPosition)
  38.         end
  39.         lastPosition = currentPosition
  40.     end
  41. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement