0:01 / 0:30
Playback speed

Alpha

Place the character over the tree and lower opacity until you can see the trunk through it.

Interactive demo#SpritesAndImages

How see-through a pixel is, from fully solid to invisible.

Every pixel stores red, green, blue and a fourth number, alpha. At full alpha the pixel covers what’s behind it; at zero it’s invisible; in between, the two colours mix. It’s how sprites get non-rectangular shapes, and how things fade.

Code

src/demos/alpha.goose
namespace alpha_lesson;
import stage;
import std;
import math;

export "alpha" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
    let alpha = number_control("alpha", 0.5);
    artwork("valley");
    rect(0, 0, W, H, "rgba(26,43,55,0.43)");
    draw_map(cell_count(), -1);
    tree(60, MY + 32, 2);
    let x = if hover { px } else { 48 + sin(time) * 22 };
    opacity(alpha);
    actor(x, MY + 40, walk(time), 3);
    opacity(1);
    checker(118, 8, 34, 12, 6);
    opacity(alpha);
    rect(118, 8, 34, 12, SUN);
    opacity(1);
    box(118, 8, 34, 12, INK);
}

Stage drawing helpers · All demos on GitHub

Comments

No comments yet.