Created
September 19, 2021 19:56
-
-
Save Jellonator/93b67a24e78fb3fbedfb32e123398957 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function dowobble(cel, data, dataoffset) | |
local img = cel.image:clone() | |
-- local selection = cel.sprite.selection | |
for it in img:pixels() do | |
local nx = it.x | |
local ny = it.y | |
local seed = data.dir == "y" and it.x or it.y | |
-- Frequency is number of pixels it takes to perform full phase | |
local pos = (seed + dataoffset) / data.frequency | |
local offset = math.floor(math.sin(pos * math.pi * 2) * data.amplitude) | |
if data.dir == "x" then | |
nx = nx + offset | |
if data.wrap then | |
nx = negmod(nx, img.width) | |
else | |
nx = math.min(math.max(nx, 0), img.width-1) | |
end | |
else | |
ny = ny + offset | |
if data.wrap then | |
ny = negmod(ny, img.height) | |
else | |
ny = math.min(math.max(ny, 0), img.height-1) | |
end | |
end | |
it(cel.image:getPixel(nx, ny)) | |
end | |
cel.image = img | |
app.refresh() | |
end | |
function negmod(a, b) | |
return math.fmod(math.fmod(a, b) + b, b) | |
end | |
local cel = app.activeCel | |
local spr = app.activeSprite | |
if not cel then return app.alert "No active cel" end | |
local dlg = Dialog("Wobble") | |
dlg:number { id="amplitude", label="Amplitude", text="16", focus=true } | |
:number { id="frequency", label="Frequency", text="16" } | |
:number { id="offset", label="Offset", text="0" } | |
:check { id="wrap", label="Wrap", selected=false } | |
:check { id="animate", label="Animate", selected=false } | |
:combobox { id="dir", label="Direction", option="x", options={ "x", "y" } } | |
:button { id="ok", text="OK" } | |
:button { text="Cancel" } | |
dlg:show() | |
local data = dlg.data | |
if data.ok then | |
app.transaction( | |
function() | |
if data.animate then | |
local cels = app.range.cels | |
if not cels then | |
app.alert("No selected cels") | |
return | |
elseif #cels <= 1 then | |
app.alert("Need at least two selected cels to animate") | |
return | |
end | |
for i = 1, #cels do | |
local o = (i - 1) / #cels | |
dowobble(cels[i], data, data.offset + o * data.frequency) | |
end | |
else | |
dowobble(cel, data, data.offset) | |
end | |
end | |
) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment