Transformations

On this page · 11 entries

PushMatrix · PopMatrix

  • PushMatrix()
  • PopMatrix()

Saves and restores the canvas's transform stack.

Translate

  • Translate(x, y)

Shifts the origin.

Rotate · RotateDegrees

  • Rotate(float angle)
  • RotateDegrees(float degrees)

Rotate() takes radians, like Processing and p5.js: Rotate(HALF_PI) is a quarter turn. If you think in degrees, use RotateDegrees(90) or Rotate(Radians(90)). (Before 1.0, Rotate() took degrees; sketches written for 0.x need Radians() around the angle.)

Scale

  • Scale(float sx, float? sy = null)

Uniform scale when sy is omitted.

ShearX · ShearY

  • ShearX(float angleRadians)
  • ShearY(float angleRadians)

Shears drawing along one axis.

ResetMatrix

  • ResetMatrix()

Replaces the current transform with the identity, discarding every Translate/Rotate/Scale/Shear/ApplyMatrix applied so far — including ones from before the innermost PushMatrix(). Stronger than PopMatrix(), which only undoes back to the last PushMatrix().

ApplyMatrix

  • ApplyMatrix(a, b, c, d, e, f)

Multiplies the current transform by an arbitrary affine matrix [[a c e][b d f][0 0 1]] — for effects the named transform calls can't express directly.

PrintMatrix

  • PrintMatrix()

Prints the current transformation matrix to the console as a 3×3 row-major matrix — a debugging aid for figuring out what a chain of transform calls actually produced.

Clip · NoClip

  • Clip(x, y, w, h)
  • NoClip()

Restricts drawing to a rectangle until NoClip() is called — or until Clip() is called again with a new rectangle, which replaces rather than intersects the old one. Always uses corner-style x/y/w/h regardless of the current RectMode().

Don't straddle PushMatrix/PopMatrix. Implemented with its own internal save/restore pair on the canvas — pair a Clip() with its NoClip() at the same matrix-nesting depth, rather than spanning a PushMatrix()/PopMatrix() boundary.

Hint

  • Hint(string hintName)

A logged no-op. Every hint constant Processing defines is either meaningless for a single always-antialiased 2D software renderer, or already covered by a dedicated method (Smooth()/NoSmooth()). Exists so ported sketches that defensively call hint(SOME_CONSTANT) still compile and run.

Runnable example

A complete sketch using this section. Test in Dana opens it in the IDE, or paste it into a new tab and press Run.

C#
// Translate, Rotate (radians) and Scale, with PushMatrix/PopMatrix to undo them.
public class Clock : Sketch
{
    public override void Setup() => Size(400, 400);

    public override void Draw()
    {
        Background(245);
        Translate(Width / 2, Height / 2);           // origin to the centre

        for (int i = 0; i < 12; i++)
        {
PushMatrix();
Rotate(TWO_PI * i / 12);                 // radians; RotateDegrees(30 * i) also works
Fill(i % 3 == 0 ? 40 : 160);
NoStroke();
Rect(-4, -170, 8, i % 3 == 0 ? 30 : 15);
PopMatrix();
        }

        float s = Second() + Millis() % 1000 / 1000f;
        PushMatrix();
        Rotate(TWO_PI * s / 60);
        Stroke(220, 60, 60);
        StrokeWeight(3);
        Line(0, 20, 0, -150);
        PopMatrix();

        PushMatrix();
        Scale(1 + Sin(FrameCount * 0.05f) * 0.2f);   // a breathing dot
        Fill(220, 60, 60);
        Circle(0, 0, 16);
        PopMatrix();
    }
}