Skip to content

Commit b11a2fc

Browse files
committed
Animations using after_draw() don't work properly
The Sparkle animation uses `after_draw` to setup the pixels currently being sparkled for the next frame, assuming `show()` will not be called until then. It also calls `show()` itself first to show the changes made in `draw()`. The problem is that `animate()` already calls `show()` after calling draw and after_draw. So the frame where the sparkle should show is actually skipped and only briefly appears instead of staying for a frame according to the speed parameter. The refactor from PR adafruit#23 changed the protocol where animations should no longer call `show()` themselves. We can't change `after_draw` to be called after the call to show() in animate(), because it will break on animation groups using the same strip. If another animation triggers a show() sooner, it will show the next frame of the sparkle prematurely. It also makes the protocol less predictable. I have used `animate(False)`, followed by setting a pixel an calling `show()` for example to force the color of a single or a few pixels without going through the hassle of creating pixelmaps or groups (and also to simply make it look like it's superimposed over the animation). So the solution would be to drop `after_draw` and instead memorize the previous list of pixels and set them back in the next draw.
1 parent 80ac0d0 commit b11a2fc

File tree

5 files changed

+208
-214
lines changed

5 files changed

+208
-214
lines changed

adafruit_led_animation/animation/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ def animate(self, show=True):
8383
for anim in self._peers:
8484
anim.draw_count += 1
8585
anim.draw()
86-
anim.after_draw()
8786

8887
if show:
8988
for anim in self._peers:

adafruit_led_animation/animation/rainbowsparkle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ def generate_rainbow(self):
8989
int(self._background_brightness * color[2]),
9090
)
9191

92-
def after_draw(self):
93-
self.show()
92+
def draw(self):
93+
super().draw()
9494
pixels = [
9595
random.randint(0, len(self.pixel_object) - 1)
9696
for n in range(self._num_sparkles)

adafruit_led_animation/animation/sparkle.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,10 @@ def _random_in_mask(self):
8383
return self._mask[random.randint(0, (len(self._mask) - 1))]
8484

8585
def draw(self):
86-
self._pixels = [self._random_in_mask() for _ in range(self._num_sparkles)]
87-
for pixel in self._pixels:
88-
self.pixel_object[pixel] = self._sparkle_color
89-
90-
def after_draw(self):
91-
self.show()
9286
for pixel in self._pixels:
9387
self.pixel_object[pixel % self._num_pixels] = self._half_color
9488
if (pixel + 1) % self._num_pixels in self._mask:
9589
self.pixel_object[(pixel + 1) % self._num_pixels] = self._dim_color
90+
self._pixels = [self._random_in_mask() for _ in range(self._num_sparkles)]
91+
for pixel in self._pixels:
92+
self.pixel_object[pixel] = self._sparkle_color

adafruit_led_animation/animation/sparklepulse.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ def __init__(
6464

6565
def _set_color(self, color):
6666
self._color = color
67+
super()._set_color(color)
6768

6869
def draw(self):
6970
self._sparkle_color = next(self._generator)
7071
super().draw()
71-
72-
def after_draw(self):
73-
self.show()

0 commit comments

Comments
 (0)