Visualizations
334 / 500

334 · Type and image

Pixel Sorting

Glitch-art pixel sorting run live on painted photos, at any angle.

Three 'photographs' are painted in code (a dusk coast, a neon city, an aurora over a lake) and then cut into parallel digital lines running in the sort direction, built by shearing so every pixel lands on exactly one line at any angle. A brightness threshold band picks out intervals along each line, and only those runs are sorted, by brightness, hue or saturation, which is why skies melt into streaks while silhouettes stay sharp. The animation is a real binary insertion sort running on every interval at once: the sorted part grows in the flow direction with its brightest pixel riding the front like a rising wave. While you drag, a stable counting sort per interval re-sorts the whole picture every frame.

Try it. Drag on the picture to set the direction pixels flow. Drag the handles on the histogram to move the threshold band (the sorted intervals light up while you do). Use the buttons to switch scenes, sort key and mask, or replay the animated sort; keys 1 to 3, K, I, the arrows and Space do the same.

  • Threshold interval masks
  • Binary insertion sort
  • Counting sort
  • Sheared line traversal
  • Procedural painting

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a pixel sorting glitch-art tool with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Paint a "photo" in code on a 480x300 offscreen canvas: a sunset sky gradient, a bright sun, a few soft clouds, dark mountain silhouettes and a sea below. Read it back with getImageData.
- For every column of pixels, walk from top to bottom and find intervals: runs of consecutive pixels whose brightness is between a low and a high threshold (start with 70 and 255).
- Sort only the pixels inside each interval by brightness, so the bright end of each run lands at the top, and write the result back. Show the sorted image scaled up to fill the window with image smoothing turned off.

Once that works, make it beautiful:
- Animate the sort instead of jumping to the end: run an insertion sort on every interval at once, inserting a few pixels per frame, so you can watch the streaks grow.
- Draw a brightness histogram with two draggable handles for the thresholds, and tint the intervals that will sort while I drag.
- Add buttons to sort by hue or saturation instead of brightness, to sort rows instead of columns, and to reverse the direction.
- Re-sort instantly while I drag a handle, with a counting sort per interval (keys are 0 to 255, so it is linear time).

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as sorting along lines at any angle, masking intervals by edges instead of brightness, or loading my own photo from a file input.
PreviousBiomorphsDawkins' Blind Watchmaker: breed recursive ink creatures by choosing a child. NextHologramRecord an off-axis hologram of glowing points, then rebuild the 3D scene from it.

Related visualizations

  • Sorting TapestryAlgorithms Six sorting algorithms cross-stitch a scrambled sampler back into a rainbow.
  • Sorting NetworkAlgorithms A sorting network as a wooden marble run: brass gates swap marbles into order.
  • Dither LabRetro Seven dithering algorithms race on a moving scene in 1-bit, handheld green, CGA and EGA.
  • Image TracerAlgorithms Pixel art and a painted landscape traced into crisp, infinitely zoomable curves.
  • Palette CyclingRetro A waterfall, harbor rain and an aurora, animated without changing a single pixel.
  • Droste SpiralType and image Escher's Print Gallery effect: a painted harbor town that spirals into itself forever.
  • Sprite StackingRetro A cozy pixel town where every house, car and tree is a pile of flat slices.
  • Hidden in PixelsType and image A treasure map hides in a landscape's lowest bits. Peel the bit planes to find it.
  • KnittingType and image Pixel art becomes Fair Isle knitting, stitch by twisted stitch, live on the needle.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs