JS1K X part 2: Nyan Cat 2.0

My second entry for JS1K was an animation: Nyan Cat 2.0.


So, if I already made one back in 2014, and even wrote a blog post about it, why would I do it again? Well, for me it was really frustrating not to be able to find a way add the twinkling stars in the background.

Comparing Nyan Cat GIF and JS

It took me quite a lot of time to get that starless version down to 1Kb back then, so I just gave up, but from time to time I still thought about it… until one day I had an Eureka moment.

A more efficient encoding

As I described in the 2014 Nyan Cat entry blog post, there sprites were compressed using a run-length encoding system, so a string like AABCCC (which in that case would be “2 cells of the first color, 1 of the second, and 3 of the third”) was compressed to A3BC4.

In this particular animation, the different parts have rather small amount of colors; for instance, the cat’s head only has 6 different colors, while the legs and tail have 3, and this characteristic allows a quite interesting trick: encoding the color and the amount on a single character.

So, let’s say that our basic colors are ABCDEF; that means that we want to paint a single cell with each of those colors; then, the next 6 characters, GHIJKL, are used to represent two cells; then, the next 6 (MNOPQR) three cells, and so on. So, if we had AABCCC, it would be compressed to GBO, 2 bytes smaller. Success!

Not only did it make the data more compact (around 80 bytes), but also the new sprite drawing function ended up being 40 bytes smaller. So now I had some wiggle room in order to add those stars.

What else?

In the previous Nyan Cat blog post I predicted that the stars would take somewhere between 200 and 400 bytes… in the end it was 234 bytes before compression, so quite close.

So, I had to roll up my sleeves and find some additional optimizations; an easy one was to change the cat body, as it was using some custom rectangle fillings in order to make it a perfect copy of the one in the original GIF animation; instead, I made a very close aproximation using the same sprite painter function that it’s used everywhere else, while the other ones mostly involved renaming things around in order to increase RegPack’s efficiency.

Commented source code


// Color picker

// Sprite painter

// Function that paints the star

  // Paint rainbow
  // Paint the stars


  // Paint the cat body parts

Game Over

Sadly, JS1K is now over. This competition was great to motivate myself to create these tiny games and demos, and I was able to work on most of the ideas I had… well, at some point I wanted to build a shoot’em up game, but other than that, I’m satisfied with the work I created. I participated for the challenge and the learning, and certainly entertained me; gave me a excuse to create a plasma effect, a 3D renderer, cat animations, and even some games.

What now? Well, I’ve been thinking about participating in JS13K, but that may be a major time commitment, considering how long it took me to complete some of the single-kilobyte entries… Still, would be nice to do it at least once. Maybe 2020!!