Skip to content

Commit a00f547

Browse files
committed
perf: use timeout instead of interval for animation
ref #156
1 parent 88cd00c commit a00f547

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

lua/smear_cursor/animation.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ local M = {}
77
local BASE_TIME_INTERVAL = 17
88

99
local animating = false
10-
local timer = nil
1110
local previous_time = 0
1211
local target_position = { 0, 0 }
1312
local current_corners = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } }
@@ -246,7 +245,7 @@ local function update_particles(time_interval)
246245
local i = 1
247246
while i <= #particles do
248247
local particle = particles[i]
249-
particle.lifetime = particle.lifetime - config.time_interval
248+
particle.lifetime = particle.lifetime - time_interval
250249

251250
if particle.lifetime <= 0 then
252251
table.remove(particles, i)
@@ -311,10 +310,6 @@ local function shrink_volume(corners)
311310
end
312311

313312
local function stop_animation()
314-
if timer == nil then return end
315-
timer:stop()
316-
timer:close()
317-
timer = nil
318313
animating = false
319314
previous_time = 0
320315
end
@@ -373,7 +368,9 @@ local function redraw_cmd_mode(force)
373368
end
374369

375370
local function animate()
376-
animating = true
371+
local start_time = vim.uv.now()
372+
if not animating then return end
373+
377374
local must_redraw_cmd_mode = check_smear_outside_cmd_row()
378375
local time_interval = get_effective_time_interval()
379376
local index_head, index_tail = update(time_interval)
@@ -461,12 +458,15 @@ local function animate()
461458
draw.draw_particles(particles, target_position)
462459
draw.draw_quad(drawn_corners, target_position, cursor_is_vertical_bar(), gradient_origin, gradient_direction_scaled)
463460
redraw_cmd_mode(must_redraw_cmd_mode)
461+
462+
local end_time = vim.uv.now()
463+
vim.defer_fn(animate, math.max(0, config.time_interval - (end_time - start_time)))
464464
end
465465

466466
local function start_anination()
467-
if timer ~= nil then return end
468-
timer = vim.uv.new_timer()
469-
timer:start(0, config.time_interval, vim.schedule_wrap(animate))
467+
if animating then return end
468+
animating = true
469+
animate()
470470
end
471471

472472
local function set_stiffnesses()

0 commit comments

Comments
 (0)