0:01 / 0:30
Playback speed

Anti-aliasing

Pause the circles. Pick one stair-step on the left and find the same spot on the right. What colour fills the edge pixel?

#PixelLook

Softening jagged edges with in-between shades.

A pixel grid can’t draw a true curve, so edges come out as staircases. Anti-aliasing shades each edge pixel by how much of it the shape covers. Left: every pixel is on or off. Right: partial pixels are partly filled.

Code

src/demos/anti_aliasing.goose
namespace anti_aliasing_lesson;
import stage;
import std;
import math;

export "anti-aliasing" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
    artwork("draft");
    let radius = 6 + sin(time * 0.9) * 1.6;
    let cx = 8 + cos(time * 0.5) * 0.8;
    for half in 2 {
        for j in 20 {
            for i in 16 {
                var coverage = 0.0;
                if half == 0 {
                    if hypot(i + 0.5 - cx, j + 0.5 - 10) < radius {
                        coverage = 1.0;
                    }
                } else {
                    for a in 4 {
                        for b in 4 {
                            if hypot(i + (a + 0.5) / 4 - cx, j + (b + 0.5) / 4 - 10) < radius {
                                coverage += 1.0 / 16.0;
                            }
                        }
                    }
                }
                if coverage > 0 {
                    opacity(coverage);
                    rect(half * 80 + i * 5, j * 5, 5, 5, WHITE);
                }
            }
        }
    }
    opacity(0.25);
    for i in 33 {
        rect(i * 5, 0, 1, H, LINE);
    }
    for j in 21 {
        rect(0, j * 5, W, 1, LINE);
    }
    opacity(1);
    rect(79, 0, 2, H, MAG);
}

Stage drawing helpers · All demos on GitHub

Comments

No comments yet.