Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 50 ยท Getting Started

Pixels, Resolution and the Framebuffer

Understand pixels, resolution, aspect ratio and bit depth, and learn how a framebuffer is just an array you can fill using canvas ImageData.

Every image on a screen is made of pixels (picture elements): tiny squares, each lit with one color. A pixel has no shape or size of its own in the mathematical sense; it is just one cell in a grid, holding one color value. When a graphics program "draws", all it ever does is change values in that grid.

The grid lives in memory as a framebuffer (or frame buffer): a block of memory with one entry per pixel. The graphics hardware reads the framebuffer many times per second and sends it to the display. If you understand the framebuffer, you understand what every rendering algorithm in this course is really doing: deciding which entries to write, and with what color.

Resolution and aspect ratio

The resolution of an image is its size in pixels, written width x height. A Full HD screen is 1920 x 1080, so it has 1920 x 1080 = 2,073,600 pixels. A 4K screen (3840 x 2160) has four times as many.

The aspect ratio is width divided by height. Full HD is 1920 / 1080 = 16:9 (about 1.78). Older monitors used 4:3 (1.33). Aspect ratio matters because drawing code usually works in a coordinate system independent of the screen. If you ignore the aspect ratio when you map that system to pixels, circles become ovals. The lesson on window-to-viewport mapping comes back to this.

A related idea is pixel density: how many pixels fit in an inch. A phone with a very dense screen packs many pixels into a small space, which is why text looks sharp. Browsers hide this by giving you "CSS pixels", which may be several device pixels wide. For canvas work that is why you sometimes multiply sizes by window.devicePixelRatio (see the canvas lesson in the JavaScript tutorial).

The framebuffer is an array

Think of the framebuffer as a one-dimensional array. A grid has two dimensions, but memory is a long line, so we store the grid row by row. This is called row-major order. With a width of W pixels, the pixel at column x and row y is found at:

TEXT
index = y * W + x

If every pixel takes 4 bytes (red, green, blue, alpha), multiply by 4 to get the byte offset: offset = (y * W + x) * 4. This formula is the most important line of code in the lesson. You will use it again and again.

flowchart LR
  subgraph Grid["Grid: W = 4, H = 2"]
    direction TB
    R0["row 0: (0,0) (1,0) (2,0) (3,0)"]
    R1["row 1: (0,1) (1,1) (2,1) (3,1)"]
  end
  Grid --> Mem["Memory: index 0..7<br/>(0,0) (1,0) (2,0) (3,0) (0,1) (1,1) (2,1) (3,1)"]

The 2D grid is stored row after row. Pixel (x, y) sits at index y * W + x.

Bit depth and color

How many different colors can one pixel hold? That is decided by its bit depth, the number of bits used per pixel.

Bits per pixel Distinct colors Typical use
1 2 Black and white (fax, simple icons)
8 256 Indexed palettes (old GIFs, early PCs)
24 about 16.7 million "True color": 8 bits each for red, green, blue
32 same as 24, plus 8 bits of alpha RGBA: color with transparency

Most web graphics use 8 bits per channel, so every channel is a number from 0 to 255. With three channels that makes 256 x 256 x 256 = 16,777,216 colors. The extra alpha channel says how opaque a pixel is (0 is fully transparent, 255 fully opaque).

With fewer bits, smooth gradients break into visible bands. The demo further down shows exactly that.

Writing pixels with ImageData

A canvas has a framebuffer behind it, and ImageData gives you direct access. ctx.createImageData(w, h) makes a blank buffer; its .data property is a Uint8ClampedArray of length w * h * 4, ordered R, G, B, A for each pixel. You fill it, then call ctx.putImageData(image, 0, 0) to copy it to the canvas. (To read existing pixels, ctx.getImageData() does the opposite. Browsers refuse that call if the canvas was tainted by a cross-origin image.)

Clamped means that values are forced into 0 to 255: assigning 300 stores 255, and -5 stores 0.

The next example writes a framebuffer by hand. Each pixel's color is computed from its coordinates, no drawing commands involved. Then it prints one pixel's index and values.

Filling a framebuffer by hand

Writing pixels one by one in JavaScript is fine for learning, and for the software renderers you build in this course. Real-time engines let the GPU fill the framebuffer instead, because it can shade thousands of pixels in parallel.

Seeing bit depth

To simulate lower bit depth, we can keep only the top n bits of each 8-bit channel. The formula levels = 2^n, then value = round(value / 255 * (levels - 1)) * 255 / (levels - 1), snaps a value to the nearest of levels evenly spaced steps. Below, the same horizontal gradient is shown with 1, 2, 3 and 8 bits per channel.

Banding at low bit depth

Double buffering

If a program draws straight into the buffer the display is reading, the viewer can see a half-finished picture: part of the old frame, part of the new one. In animation this shows up as flicker or tearing. The standard cure is double buffering. The program draws the whole next frame into a hidden back buffer, while the display shows the front buffer. When the frame is complete, the roles are swapped (or the back buffer is copied to the front), so the viewer only ever sees finished frames.

flowchart LR
  P["Program draws"] --> B["Back buffer<br/>(hidden)"]
  B -->|"swap when frame is done"| F["Front buffer<br/>(being displayed)"]
  F --> D["Display"]

The display only reads the front buffer, so half-drawn frames are never visible.

Browsers already double buffer the page for you, but you can still build the same idea in a software renderer. The animation below renders each frame into one of two arrays and swaps them. Only a complete frame is ever copied to the canvas.

Swapping two buffers

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A framebuffer is 640 pixels wide and stores 4 bytes per pixel. At which byte offset does the pixel (x = 10, y = 2) start?

    Row-major order: index = y * W + x = 1290, and every pixel takes 4 bytes, so the offset is 1290 * 4 = 5160.
  2. 2. How many different colors can a 24-bit true-color pixel represent?

    24 bits split into three 8-bit channels give 256 x 256 x 256 = 16,777,216 colors.
  3. 3. What does the fourth byte (alpha) of an RGBA pixel describe?

    Alpha is opacity: 0 is fully transparent and 255 fully opaque.
  4. 4. Why do renderers use double buffering?

    The next frame is built in a hidden back buffer and swapped in when finished, which prevents flicker and tearing.
  5. 5. Why does the lesson use Uint8ClampedArray for pixel data?

    A clamped array saturates values: 300 becomes 255 and -5 becomes 0, which matches 8-bit color channels.

Summary

  • A pixel is one cell of a grid that holds one color; the framebuffer is the memory holding all of them.
  • Resolution is width x height in pixels; aspect ratio is width divided by height.
  • Pixels are stored row by row: index = y * W + x, and RGBA pixels take 4 bytes each.
  • Bit depth sets how many colors a pixel can show; too few bits cause visible banding.
  • ImageData and putImageData let you write a canvas framebuffer directly.
  • Double buffering draws into a hidden buffer and swaps, so only finished frames are displayed.

Next: Coordinate Systems.