Matrix Class
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);
Item Index
Methods
Properties
Methods
this
()
Object constructor creates new 3x3 matrix
postRotate
degrees
px
py
Postconcats the matrix with the specified rotation. M' = R(degrees, px, py) * M.
Parameters:
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]
Postconcats the matrix with the specified scale. M' = S(sx, sy, px, py) * M.
Parameters:
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]
Postconcats the matrix with the specified skew. M' = K(kx, ky, px, py) * M.
Parameters:
Returns:
postTranslate
dx
dy
Postconcats the matrix with the specified translation. M' = T(dx, dy) * M.
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
Preconcats the matrix with the specified rotation. M' = M * R(degrees, px, py).
Parameters:
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]
Preconcats the matrix with the specified scale. M' = M * S(sx, sy, px, py).
Parameters:
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]
Preconcats the matrix with the specified skew. M' = M * K(kx, ky, px, py).
Parameters:
Returns:
preTranslate
dx
dy
Preconcats the matrix with the specified translation. M' = M * T(dx, dy).
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
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
IntegerThe source rectangle to map from.
-
srctop
IntegerThe source rectangle to map from.
-
srcright
IntegerThe source rectangle to map from.
-
srcbottom
IntegerThe source rectangle to map from.
-
dstleft
IntegerThe destination rectangle to map to.
-
dsttop
IntegerThe destination rectangle to map to.
-
dstright
IntegerThe destination rectangle to map to.
-
dstbottom
IntegerThe destination rectangle to map to.
-
scaleToFit
SymbolThe 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
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:
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]
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:
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]
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.
setTranslate
dx
dy
Set the matrix to translate by (dx, dy).
Example:
// create transform matrix
var m = new Matrix();
// apply movement
m.setTranslate(320, 480);
// apply transformation to the path
this._path.transform(m);