0:01 / 0:30
Playback speed

Nine-slice

Watch one pink corner stud as the panel grows. Toggle nine-slice and see when the stud stretches.

Interactive demo#SpritesAndImages

A way to stretch a panel to any size without smearing its corners.

The image is cut into a 3 × 3 grid. Corners never scale, edges stretch in one direction, and the middle fills the rest. One small image can then make every dialog box and button in the game.

Code

src/demos/nine_slice.goose
namespace nine_slice_lesson;
import stage;
import std;
import math;

export "nine-slice" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
    artwork("draft");
    let w = floor(mix_value(26, 120, (sin(time * 1.1) + 1) / 2) + 0.5);
    let h = floor(mix_value(24, 70, (cos(time * 0.8) + 1) / 2) + 0.5);
    let x = 80 - floor(w / 2);
    let y = 50 - floor(h / 2);
    if toggle("on") {
        let source = [0.0, 3.5, 14.5, 18.0];
        let xs = [x, x + 9, x + w - 9, x + w];
        let ys = [y, y + 9, y + h - 9, y + h];
        for i in 3 {
            for j in 3 {
                panel_slice(source[i], source[j], source[i + 1] - source[i], source[j + 1] - source[j],
                            xs[i], ys[j], xs[i + 1] - xs[i], ys[j + 1] - ys[j]);
            }
        }
        for gx in [x + 9, x + w - 9] {
            line(gx + 0.5, y - 5, gx + 0.5, y + h + 5, MAG, 2, 2);
        }
        for gy in [y + 9, y + h - 9] {
            line(x - 5, gy + 0.5, x + w + 5, gy + 0.5, MAG, 2, 2);
        }
    } else { panel_slice(0, 0, 18, 18, x, y, w, h); }
}

Stage drawing helpers · All demos on GitHub

Comments

No comments yet.