Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Transforms

The 2D transform system works like Processing’s pushMatrix() / popMatrix(), but shortened to push() / pop().

Push & Pop

Save and restore the current transform:

#![allow(unused)]
fn main() {
push();
// ... transforms and drawing here ...
pop();
// back to the previous transform
}

Translate

Move the origin:

#![allow(unused)]
fn main() {
translate(dx, dy);
}

Rotate

Rotate around the current origin, in radians:

#![allow(unused)]
fn main() {
rotate(angle);
}

Positive angle rotates counter-clockwise. Use std::f32::consts::TAU for a full turn, FRAC_PI_2 for 90 degrees, etc.

Scale

Scale the coordinate system:

#![allow(unused)]
fn main() {
scale(sx, sy);
}

Order matters

Transforms apply in the order you call them. This is the same as Processing:

#![allow(unused)]
fn main() {
push();
translate(400.0, 300.0); // move origin to center
rotate(0.5);             // rotate around center
rect(-25.0, -25.0, 50.0, 50.0); // draw at origin
pop();
}

Reset

The transform resets to identity at the start of each frame, so there is no carry-over between frames.