I'm willing to bet that a good number of people will recognize the below picture almost instantaneously - especially those who grew up in the 90s:
That's a picture from the popular Microsoft Pipes Screensaver - a screenshot of it running natively on a Windows ME laptop I've had since I was a kid.
Funnily enough, there really isn't that much going on in the screensaver: It's just a never-ending animation of interconnecting 3D pipes appearing and growing on screen. Nevertheless, I remember kids in school zoning out in front of it and watching it endlessly. To be fair, the animation was an impressive feat of graphics for the time period - especially when you consider most of the computers back then were falling back to rendering it via software exclusively. It's probably not a surprise then that it left a lasting impression on people and even decades later it's still finding its way into mainstream discussions. As Raymond Chen writes on the Microsoft dev blog, Gizmodo even went so far as to award it the title of "The Best Screensaver of All Time".
I was thinking recently about what I'd like to codegolf next. I wanted to do something fun and unique. People have crammed a lot of stuff into small amounts of code - but from I can tell, a Pipes Screensaver clone in a *very* small footprint has never been attempted. Sure, there are various demos around online that feature 200-300 hundred or so LOC examples, but all the ones I can find end up importing a library (the JS ones typically use Three.js). Even if I were to spin up my own library using WEBGL, the boilerplate code alone would likely easily chew up a couple of kilobytes of space.
I knew there would be some significant concessions that would have to be made to stuff this into such a condensed script. For starters, the scene features one "Camera" angle that is a fixed Isometric perspective. This is the first trick I used to get away with not having to use any trig functions (cos/sin/tan,etc) that would have likely significantly increased code size. Instead, all 3D points are brute-forced, and as you'll see in the full uncompressed code, this allowed me to recycle a nested loop that's used by different geometries.
Despite its extremely small footprint, it even has a (albeit primitive) lighting system to add the look of depth to the pipes. To conserve even more bites the screen simply resets rather dissolving, but true to the original - it loops on forever. Here's a GIF of my 2KB JavaScript version running in Chrome:
Here's the gist that contains the golfed ~2KB code that runs the animation:
I could write a novel about why some of the design decisions were made the way they were (the first few iterations were hardly able to get golfed down to anything under 5KB). Some of the code just has to get stepped through in order to understand it in a meaningful manner if you intend on modifying or extending it.
That said - there are a few particularly head-scratching and unintuitive functions that are worth analyzing in depth. You'll want to read over the comments in the gist carefully.
Few other things worth mentioning:
- The original screensaver had a mix of pipe connectors: an "elbow", "ball", and occasionally a teapot. Via some more brute-force trickery I was able to get an actual 3D elbow-like curved connector painting. The code to approximate the points around the curve and the logic to determine which rotation and orientation to paint each elbow in added a few hundred bytes to the total code right there - so I removed it. Instead, a 3D sphere "ball" is always the connector, and its just wedged in the corners where 2 pipes meet in a way that makes it look enough like an elbow.
- The GIF I originally posted on r/codegolf shows a collision of overlapping pipes a little over 2 seconds in to the video. Ironically - I discovered the root cause of the bug while I was typing up the writeup for this blog post. The GIF above was recorded after the bug was fixed and you'll notice there aren't any overlapping pipes. Still - due to the nature of how 3D points are mapped to the 2D raster, there's no guarantee it won't ever happen, but it should be extremely rare.
- There are enough repeating characters (like similarities of function signatures) in the golfed code using a simple replaceAll() and eval() call could get this down just slightly under 2KB. However, even with the assurance of modern sandboxed browser environments, many people are still (understandably) hesitant about running code they can't clearly see the intent of, so I didn't bother to post a version of that to reddit. Besides, for all intents and purposes 1.9K and 2.2K is kind of the same thing from a competitive perspective. If I managed to strip at least a few hundred more bytes from it I'll post a new version.
There were hours of experimentation and trial and error that went in to piecing together parts of the code in such a way that it A. worked, and B. actually fit in such a small footprint. The end state is way more intuitive to me than it would be to a fresh set of eyes. Please feel free to post a comment below if you're playing around with the script and would like to get clarification on any other parts of the code.
The ungolfed verison can be found in this gist: https://gist.github.com/edev90/b48c2684a1127f3eca949a59f465b9cd
Hope you enjoy this little demo clone of a piece of nostalgia and computing history!





