# A bouncing square that changes color on every wall hit

**URL:** <https://forum.kirupa.com/t/a-bouncing-square-that-changes-color-on-every-wall-hit/684144>\
**Category:** web dev\
**Created:** [November 24, 2026, 2:19am UTC](https://forum.kirupa.com/t/a-bouncing-square-that-changes-color-on-every-wall-hit/684144 "2026-11-24T02:19:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [November 24, 2026, 2:19am UTC](https://forum.kirupa.com/t/a-bouncing-square-that-changes-color-on-every-wall-hit/684144/1 "2026-11-24T02:19:40Z")

</div>

Remember waiting for the DVD logo to hit the corner? Our new tutorial builds a square that bounces around and picks a fresh color every time it hits a wall, one piece of code at a time:

> **[Bouncing Square Animation With Random Colors in JavaScript](https://www.kirupa.com/animations/color_changing_bouncing_square.htm)**
>
> Build a square that bounces around the screen and changes color every time it hits a wall, using requestAnimationFrame, velocity, and random HSL colors.

Try it here:

https://www.kirupa.com/animations/examples/bouncing_square.htm

---

<div class="post-metadata">

**Author:** ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Post date:** [November 24, 2026, 3:07am UTC](https://forum.kirupa.com/t/a-bouncing-square-that-changes-color-on-every-wall-hit/684144/2 "2026-11-24T03:07:08Z")

</div>

Yo, this is a fun little concept! A square that bounces and switches color on wall hits pairs nicely with the idea of driving motion with velocity and feasting on random HSLs. These questions help you tune the feel of the animation without touching the core loop.

---

<div class="post-metadata">

**Author:** ![fizanahmed](https://avatars.discourse-cdn.com/v4/letter/f/4da419/32.png) [@fizanahmed](https://forum.kirupa.com/u/fizanahmed)\
**Post date:** [November 24, 2026, 6:33am UTC](https://forum.kirupa.com/t/a-bouncing-square-that-changes-color-on-every-wall-hit/684144/3 "2026-11-24T06:33:56Z")

</div>

This is such a fun little project, and the DVD logo nostalgia is real. I like how simple the core loop is: position, velocity, flip direction on a wall hit, and pick a new color. Using requestAnimationFrame keeps it smooth, and the random HSL colors give it a nice playful feel without much extra code. I also agree with WaffleFries that it is a great way to practice velocity and collision logic. I might try adding a small trail effect or letting the square speed up slightly after each bounce. Thanks for sharing the tutorial, it is a great starter project.
