Advertisement
HowToRoblox

CanvasHandler

Mar 14th, 2020
1,108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.65 KB | None | 0 0
  1. local gui = script.Parent
  2.  
  3.  
  4. local canvas = gui.Canvas
  5.  
  6.  
  7. local brushButton = gui.Brush
  8.  
  9. local eraserButton = gui.Eraser
  10.  
  11.  
  12. local clearButtton = gui.ClearCanvas
  13.  
  14.  
  15. local brushSize = gui.BrushSizeInput
  16.  
  17. local sizeConfirm = gui.ConfirmBrushSize
  18.  
  19. local size = 10
  20.  
  21.  
  22. local brushColour = gui.BrushColourInput
  23.  
  24. local colourConfirm = gui.ConfirmBrushColour
  25.  
  26. local colour = Color3.fromRGB(0, 0, 0)
  27.  
  28.  
  29. local isEraser = false
  30.  
  31.  
  32. local paint = game.ReplicatedStorage.Paint
  33.  
  34.  
  35. local plr = game.Players.LocalPlayer
  36.  
  37. local mouse = plr:GetMouse()
  38.  
  39.  
  40. local isMouseDown = false
  41.  
  42.  
  43.  
  44. canvas.MouseMoved:Connect(function(xPos, yPos)
  45.    
  46.    
  47.     if isMouseDown then
  48.        
  49.        
  50.         if isEraser == false then
  51.        
  52.             local paintCloned = paint:Clone()
  53.            
  54.            
  55.             local offset = Vector2.new(math.abs(xPos - canvas.AbsolutePosition.X), math.abs(yPos - canvas.AbsolutePosition.Y - 36))
  56.            
  57.            
  58.             paintCloned.Size = UDim2.new(0, size, 0, size)
  59.             paintCloned.Position = UDim2.new(0, offset.X, 0, offset.Y)
  60.            
  61.             paintCloned.ImageColor3 = colour
  62.            
  63.             paintCloned.Parent = canvas
  64.            
  65.            
  66.         else           
  67.            
  68.             local guisAtPos = plr.PlayerGui:GetGuiObjectsAtPosition(xPos, yPos - 36)
  69.            
  70.            
  71.             for i, paintToErase in pairs(guisAtPos) do
  72.                
  73.                
  74.                 if paintToErase.Name == "Paint" then paintToErase:Destroy() end
  75.                
  76.             end    
  77.         end    
  78.     end
  79. end)
  80.  
  81.  
  82. game:GetService("UserInputService").InputBegan:Connect(function(input)
  83.    
  84.     if input.UserInputType == Enum.UserInputType.MouseButton1 then
  85.        
  86.        
  87.         isMouseDown = true
  88.     end
  89. end)
  90.  
  91.  
  92. game:GetService("UserInputService").InputEnded:Connect(function(input)
  93.    
  94.     if input.UserInputType == Enum.UserInputType.MouseButton1 then
  95.        
  96.        
  97.         isMouseDown = false
  98.     end
  99. end)
  100.  
  101.  
  102. eraserButton.MouseButton1Down:Connect(function()
  103.    
  104.    
  105.     isEraser = true
  106. end)
  107.  
  108.  
  109. brushButton.MouseButton1Down:Connect(function()
  110.    
  111.    
  112.     isEraser = false   
  113. end)
  114.  
  115.  
  116. clearButtton.MouseButton1Down:Connect(function()
  117.    
  118.    
  119.    
  120.     for i, paintToClear in pairs(canvas:GetChildren()) do
  121.        
  122.        
  123.         paintToClear:Destroy()     
  124.     end
  125. end)
  126.  
  127.  
  128.  
  129. sizeConfirm.MouseButton1Down:Connect(function()
  130.    
  131.    
  132.     if not tonumber(brushSize.Text) then return end
  133.    
  134.    
  135.     size = math.clamp(tonumber(brushSize.Text), 1, 100)
  136.    
  137. end)
  138.  
  139.  
  140. colourConfirm.MouseButton1Down:Connect(function()
  141.    
  142.    
  143.     local hexColour = brushColour.Text
  144.    
  145.    
  146.     if string.len(hexColour) < 6 then return end
  147.    
  148.    
  149.     hexColour = string.gsub(hexColour, "#", "")
  150.     hexColour = string.gsub(hexColour, "0x", "", 1)
  151.    
  152.     local r = tonumber("0x" .. hexColour:sub(1, 2))
  153.     local g = tonumber("0x" .. hexColour:sub(3, 4))
  154.     local b = tonumber("0x" .. hexColour:sub(5, 6))
  155.    
  156.    
  157.     colour = Color3.fromRGB(r, g, b)
  158. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement