Moscrif API Docs for: 2012q3
Show:

Matrix Class

Library: core

The Matrix class holds a 3x3 matrix. This matrix is used mostly to set transform information for Bitmap or Canvas transformation.

Example:

var bitmap = Bitmap.fromFile("dat://moscrif.jpg");

var matrix = new Matrix();
//move bitmap to the midle of screen
matrix.setTranslate(System.width / 2 - bitmap.width / 2, System.height / 2 - bitmap.height / 2);

//rotate bitmap to 45°
matrix.preRotate(45);

//draw rotated bitmap
canvas.drawBitmapMatrix(bitmap, matrix);

Methods

this

()

Object constructor creates new 3x3 matrix

postRotate

(
  • degrees
  • px
  • py
)
Boolean chainable

Postconcats the matrix with the specified rotation. M' = R(degrees, px, py) * M.

Parameters:

  • degrees Float

    Number of degrees to rotate (in clockwise).

  • px Float

    Coordinates of pivot point on x axis.

  • py Float

    Coordinates of pivot point on y axis.

Returns:

Example:

this._path = Path.fromSVG("M1....8.63z");
// create transform matrix
var (l, t, r, b) = this._path.getBounds();
this._path.offset(-1*l, -1*t);
this._path.offset((System.width - (r - l)) / 2, (System.height  - (b - t)) / 2);

var m = new Matrix();
// scale path
m.setScale(2, 2, System.width / 2, System.height / 2);
// apply rotation
m.postRotate(-90, System.width / 3, System.height / 2); // rotate CCW
// apply transformation to the object
this._path.transform(m);

postScale

(
  • sx
  • sy
  • [px]
  • [py]
)
Boolean chainable

Postconcats the matrix with the specified scale. M' = S(sx, sy, px, py) * M.

Parameters:

  • sx Float

    x scale.

  • sy Float

    y scale.

  • [px] Float optional

    Coordinates of pivot point on x axis.

  • [py] Float optional

    Coordinates of pivot point on y axis.

Returns:

Example:

this._path = Path.fromSVG("M16...3z");
// create transform matrix
var (l, t, r, b) = this._path.getBounds();
this._path.offset(-1*l, -1*t);
this._path.offset((System.width - (r - l)) / 2, (System.height  - (b - t)) / 2);

var m = new Matrix();
// scale path
m.setTranslate(0, -1*System.height / 4);
m.postScale(2, 2, System.width / 2, System.height / 4);
// apply transformation to the object
this._path.transform(m);

postSkew

(
  • kx
  • ky
  • [px]
  • [py]
)
Boolean chainable

Postconcats the matrix with the specified skew. M' = K(kx, ky, px, py) * M.

Parameters:

  • kx Float

    x skew.

  • ky Float

    y skew.

  • [px] Float optional

    Coordinates of pivot point on x axis.

  • [py] Float optional

    Coordinates of pivot point on y axis.

Returns:

postTranslate

(
  • dx
  • dy
)
Boolean

Postconcats the matrix with the specified translation. M' = T(dx, dy) * M.

Parameters:

  • dx Float

    Move on the x axis.

  • dy Float

    Move on the x axis.

Returns:

Example:

this._path = Path.fromSVG("M1...3z");
// create transform matrix
var (l, t, r, b) = this._path.getBounds();
this._path.offset(-1*l, -1*t);
this._path.offset((System.width - (r - l)) / 2, (System.height  - (b - t)) / 2);

var m = new Matrix();
// scale path
m.setScale(2, 2, System.width / 2, System.height / 2);
// apply movement
m.postTranslate(-1*System.width / 8, 0);
// apply transformation to the object
this._path.transform(m);

preRotate

(
  • degrees
  • px
  • py
)
Boolean chainable

Preconcats the matrix with the specified rotation. M' = M * R(degrees, px, py).

Parameters:

  • degrees Float

    Number of degrees to rotate (in clockwise).

  • px Float

    Coordinates of pivot point on x axis.

  • py Float

    Coordinates of pivot point on y axis.

Returns:

Example:

this._path = Path.fromSVG("M16.......63z");
// create transform matrix
var (l, t, r, b) = this._path.getBounds();
this._path.offset(-1*l, -1*t);
this._path.offset((System.width - (r - l)) / 2, (System.height  - (b - t)) / 2);

var m = new Matrix();
// scale path
m.setScale(2, 2, System.width / 2, System.height / 2);
// apply rotation
m.preRotate(-90, System.width / 3, System.height / 2);
// apply transformation to the object
this._path.transform(m);

preScale

(
  • sx
  • sy
  • [px]
  • [py]
)
Boolean chainable

Preconcats the matrix with the specified scale. M' = M * S(sx, sy, px, py).

Parameters:

  • sx Float

    x scale.

  • sy Float

    y scale.

  • [px] Float optional

    Coordinates of pivot point on x axis.

  • [py] Float optional

    Coordinates of pivot point on y axis.

Returns:

Example:

this._path = Path.fromSVG("M16...3z");
// create transform matrix
var (l, t, r, b) = this._path.getBounds();
this._path.offset(-1*l, -1*t);
this._path.offset((System.width - (r - l)) / 2, (System.height  - (b - t)) / 2);

var m = new Matrix();
// scale path
m.setTranslate(0, -1*System.height / 4);
m.preScale(2, 2, System.width / 2, System.height / 4);
// apply transformation to the object
this._path.transform(m);

preSkew

(
  • kx
  • ky
  • [px]
  • [py]
)
Boolean chainable

Preconcats the matrix with the specified skew. M' = M * K(kx, ky, px, py).

Parameters:

  • kx Float

    x skew.

  • ky Float

    y skew.

  • [px] Float optional

    Coordinates of pivot point on x axis.

  • [py] Float optional

    Coordinates of pivot point on y axis.

Returns:

preTranslate

(
  • dx
  • dy
)
Boolean chainable

Preconcats the matrix with the specified translation. M' = M * T(dx, dy).

Parameters:

  • dx Float

    Move on the x axis.

  • dy Float

    Move on the x axis.

Returns:

Example:

this._path = Path.fromSVG("M1...3z");
// create transform matrix
var (l, t, r, b) = this._path.getBounds();
this._path.offset(-1*l, -1*t);
this._path.offset((System.width - (r - l)) / 2, (System.height  - (b - t)) / 2);

var m = new Matrix();
// scale path
m.setScale(2, 2, System.width / 2, System.height / 2);
// apply movement
m.preTranslate(-1*System.width / 8, 0);
// apply transformation to the object
this._path.transform(m);

reset

()

Reset matrix to the matrix identity.

setRectToRect

(
  • srcleft
  • srctop
  • srcright
  • srcbottom
  • dstleft
  • dsttop
  • dstright
  • dstbottom
  • scaleToFit
)
Boolean chainable

Set the matrix to the scale and translate values that map the source rectangle to the destination rectangle, returning true if the the result can be represented.

Parameters:

  • srcleft Integer

    The source rectangle to map from.

  • srctop Integer

    The source rectangle to map from.

  • srcright Integer

    The source rectangle to map from.

  • srcbottom Integer

    The source rectangle to map from.

  • dstleft Integer

    The destination rectangle to map to.

  • dsttop Integer

    The destination rectangle to map to.

  • dstright Integer

    The destination rectangle to map to.

  • dstbottom Integer

    The destination rectangle to map to.

  • scaleToFit Symbol

    The source rectangle to map from.

    • #fill - Scale in X and Y independently, so that src matches dst exactly. This may change the aspect ratio of the src.
    • #start - Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. kStart aligns the result to the left and top edges of dst.
    • #center - Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. The result is centered inside dst.
    • #end - Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. kEnd aligns the result to the right and bottom edges of dst.

Returns:

Example:

this._path = Path.fromSVG("M16...63z");
// create transform matrix
var (l, t, r, b) = this._path.getBounds();
this._path.offset(-1*l, -1*t);
this._path.offset((System.width - (r - l)) / 2, (System.height  - (b - t)) / 2);

var m = new Matrix();
// map whole screen to the left bottom quarter of the screen
m.setRectToRect(0, 0, System.width, System.height,System.width / 2, System.height / 2, System.width, System.height, #fill);
// apply to the path object
this._path.transform(m);

setRotate

(
  • degrees
  • px
  • py
)
chainable

Set the matrix to rotate around a pivot point at (px, py). The pivot point is the coordinate that should remain unchanged by the specified transformation.

Parameters:

  • degrees Float

    Number of degrees to rotate (in clockwise).

  • px Float

    Coordinates of pivot point on x axis.

  • py Float

    Coordinates of pivot point on y axis.

Example:

this._path = Path.fromSVG("M16....128.63z");
// create transform matrix
var (l, t, r, b) = this._path.getBounds();
this._path.offset(-1*l, -1*t);
this._path.offset((System.width - (r - l)) / 2, (System.height - (b - t)) / 2);

var m = new Matrix();
// enlarge pictures, keep coordinates of the center
m.setScale(4, 4, System.width / 2, System.height / 2);

// apply transformation to the path
this._path.transform(m);

setScale

(
  • sx
  • sy
  • [px]
  • [py]
)
chainable

Set the matrix to scale by sx and sy, with a pivot point at (px, py). The pivot point is the coordinate that should stay unchanged by the specified transformation.

Parameters:

  • sx Float

    x scale.

  • sy Float

    y scale.

  • [px] Float optional

    Coordinates of pivot point on x axis.

  • [py] Float optional

    Coordinates of pivot point on y axis.

Example:

this._path = Path.fromSVG("M16....128.63z");
// create transform matrix
var (l, t, r, b) = this._path.getBounds();
this._path.offset(-1*l, -1*t);
this._path.offset((System.width - (r - l)) / 2, (System.height - (b - t)) / 2);

var m = new Matrix();
// enlarge pictures, keep coordinates of the center
m.setScale(4, 4, System.width / 2, System.height / 2);

// apply transformation to the path
this._path.transform(m);

setSkew

(
  • kx
  • ky
  • [px]
  • [py]
)
chainable

Set the matrix to skew by sx and sy, with a pivot point at (px, py). The pivot point is the coordinate that should remain unchanged by the specified transformation.

Parameters:

  • kx Float

    x skew.

  • ky Float

    y skew.

  • [px] Float optional

    Coordinates of pivot point on x axis.

  • [py] Float optional

    Coordinates of pivot point on y axis.

setTranslate

(
  • dx
  • dy
)
chainable

Set the matrix to translate by (dx, dy).

Parameters:

  • dx Float

    move on the x axis.

  • dy Float

    move on the y axis.

Example:

// create transform matrix
var m = new Matrix();
// apply movement
m.setTranslate(320, 480);

// apply transformation to the path
this._path.transform(m);

Properties

scaleX

Float

Scale on X axis

scaleY

Float

Scale on Y axis

skewX

Float

Skew on X axis

skewY

Float

Skew on Y axis.

translateX

Float

Translate on Y axis.

translateY

Float

Translate on Y axis.