local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local GUI = script.Parent
local Frame = GUI.Frame
local CanvasDraw = require(GUI.CanvasDraw) -- Require the module
-- Canvas creation with resolution
local resolution = Vector2.new(300, 300) -- Make sure resolution is set
local Canvas = CanvasDraw.new(Frame, resolution, Color3.new(1, 1, 1))
Canvas.AutoRender = false
-- Ensure Canvas.Size is initialized
local canvasSize = Canvas.Size or resolution -- Use a fallback size if Canvas.Size is nil
local points = 16
local cellCount = math.sqrt(points)
local xTile = {0, canvasSize.X, canvasSize.X, canvasSize.X, 0, -canvasSize.X, -canvasSize.X, -canvasSize.X}
local yTile = {canvasSize.Y, canvasSize.Y, 0, -canvasSize.Y, -canvasSize.Y, -canvasSize.Y, 0, canvasSize.Y}
local n = 1
-- Mapping function to scale values
local function map(value, fromMin, fromMax, toMin, toMax)
return (value - fromMin) / (fromMax - fromMin) * (toMax - toMin) + toMin
end
-- Function to find the nth smallest distance
local function findNthSmallest(distances, n)
table.sort(distances)
return distances[n] or 0
end
-- Function to generate Voronoi noise
local function createNoise(Px, Py)
for y = 0, canvasSize.Y - 1 do
for x = 0, canvasSize.X - 1 do
local distances = {}
-- Calculate distance to each point
for j = 1, points do
local dx = Px[j] - x
local dy = Py[j] - y
table.insert(distances, math.sqrt(dx * dx + dy * dy))
end
-- Get the nth smallest distance and map it to noise
local distance = findNthSmallest(distances, n)
local noise = map(distance, 0, canvasSize.X / 2, 255, -200)
-- Set pixel color based on noise
Canvas:SetRGB(x, y, noise / 255, noise / 255, noise / 255)
end
end
Canvas:Render()
end
-- Function to generate random spots
local function createSpots()
local x, y = {}, {}
local step = canvasSize.X / cellCount
-- Generate random spots within each cell
for yi = 0, cellCount - 1 do
for xi = 0, cellCount - 1 do
local xStart, xEnd = xi * step, (xi + 1) * step
local yStart, yEnd = yi * step, (yi + 1) * step
table.insert(x, math.random(xStart, xEnd))
table.insert(y, math.random(yStart, yEnd))
end
end
return x, y
end
local function drawSpots(x, y)
for i = 1, points do
Canvas:DrawCircle(Vector2.new(x[i], y[i]), 8, Color3.new(0, 255, 0), 0, Enum.ImageCombineType.BlendSourceOver)
end
Canvas:Render()
end
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if not gameProcessed and input.KeyCode == Enum.KeyCode.F then
local x, y = createSpots()
createNoise(x, y)
drawSpots(x, y)
end
end)
no idea why this is happening. I just switched my code from using EditableImage API to CanvasDraw