UNPKG

tav-media

Version:

Cross platform media editing framework

676 lines (675 loc) 21 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; import { tav } from "../tav"; import { allowCallNativeAnytime } from "./tav-object"; import { Rect } from "./tav-rect"; /*** * Matrix holds a 3x3 matrix for transforming coordinates. This allows mapping Point and vectors * with translation, scaling, skewing, rotation, and perspective. * Matrix elements are in row major order. Matrix does not have a constructor, so it must be * explicitly initialized. setIdentity() initializes Matrix so it has no effect. * setTranslate(), setScale(), setSkew(), setRotate(), set9 and setAll() initializes all Matrix * elements with the corresponding mapping. */ export class Matrix { // 默认构建一个单位矩阵,SKMatrix默认是全0矩阵 constructor(nativeMatrix) { if (nativeMatrix) { this._nativeMatrix = nativeMatrix; } else { this._nativeMatrix = this.makeNativeMatrix(); } } /** * Sets Matrix to scale by (sx, sy). Returned matrix is: * * | sx 0 0 | * | 0 sy 0 | * | 0 0 1 | * * @param sx horizontal scale factor * @param sy vertical scale factor * @return Matrix with scale */ static MakeScale(sx, sy = sx) { const matrix = tav.TAVMatrix.MakeScale(sx, sy); const wrapper = new Matrix(matrix); return wrapper; } /** * Sets Matrix to translate by (dx, dy). Returned matrix is: * * | 1 0 dx | * | 0 1 dy | * | 0 0 1 | * * @param dx horizontal translation * @param dy vertical translation * @return Matrix with translation */ static MakeTrans(dx, dy) { const matrix = tav.TAVMatrix.MakeTrans(dx, dy); const wrapper = new Matrix(matrix); return wrapper; } /** * Sets Matrix to: * * | scaleX skewX transX | * | skewY scaleY transY | * | pers0 pers1 pers2 | * * @param scaleX horizontal scale factor * @param skewX horizontal skew factor * @param transX horizontal translation * @param skewY vertical skew factor * @param scaleY vertical scale factor * @param transY vertical translation * @param pers0 input x-axis perspective factor * @param pers1 input y-axis perspective factor * @param pers2 perspective scale factor * @return Matrix constructed from parameters */ static MakeAll(scaleX, skewX, transX, skewY, scaleY, transY, pers0, pers1, pers2) { const matrix = tav.TAVMatrix.MakeAll(scaleX, skewX, transX, skewY, scaleY, transY, pers0, pers1, pers2); const wrapper = new Matrix(matrix); return wrapper; } build() { return this._nativeMatrix; } setNativeMatrix(nativeMatrix) { this._nativeMatrix = nativeMatrix; } /** * Returns true if Matrix is identity. Identity matrix is: * * | 1 0 0 | * | 0 1 0 | * | 0 0 1 | * * @return true if Matrix has no effect */ isIdentity() { return this._nativeMatrix.isIdentity(); } /** * Returns one matrix value. */ get(index) { return this._nativeMatrix.get(index); } /** * Returns scale factor multiplied by x-axis input, contributing to x-axis output. With * mapPoints(), scales Point along the x-axis. * @return horizontal scale factor */ getScaleX() { return this._nativeMatrix.getScaleX(); } /** * Returns scale factor multiplied by y-axis input, contributing to y-axis output. With * mapPoints(), scales Point along the y-axis. * @return vertical scale factor */ getScaleY() { return this._nativeMatrix.getScaleY(); } /** * Returns scale factor multiplied by x-axis input, contributing to y-axis output. With * mapPoints(), skews Point along the y-axis. Skewing both axes can rotate Point. * @return vertical skew factor */ getSkewY() { return this._nativeMatrix.getSkewY(); } /** * Returns scale factor multiplied by y-axis input, contributing to x-axis output. With * mapPoints(), skews Point along the x-axis. Skewing both axes can rotate Point. * @return horizontal scale factor */ getSkewX() { return this._nativeMatrix.getSkewX(); } /** * Returns translation contributing to x-axis output. With mapPoints(), moves Point along the * x-axis. * @return horizontal translation factor */ getTranslateX() { return this._nativeMatrix.getTranslateX(); } /** * Returns translation contributing to y-axis output. With mapPoints(), moves Point along the * y-axis. * @return vertical translation factor */ getTranslateY() { return this._nativeMatrix.getTranslateY(); } /** * Sets Matrix value. */ set(index, value) { this._nativeMatrix.set(index, value); } /** * Sets horizontal scale factor. * @param v horizontal scale factor to store */ setScaleX(v) { this._nativeMatrix.setScaleX(v); } /** * Sets vertical scale factor. * @param v vertical scale factor to store */ setScaleY(v) { this._nativeMatrix.setScaleY(v); } /** * Sets vertical skew factor. * @param v vertical skew factor to store */ setSkewY(v) { this._nativeMatrix.setSkewY(v); } /** * Sets horizontal skew factor. * @param v horizontal skew factor to store */ setSkewX(v) { this._nativeMatrix.setSkewX(v); } /** * Sets horizontal translation. * @param v horizontal translation to store */ setTranslateX(v) { this._nativeMatrix.setTranslateX(v); } /** * Sets vertical translation. * @param v vertical translation to store */ setTranslateY(v) { this._nativeMatrix.setTranslateY(v); } /** * Sets all values from parameters. Sets matrix to: * * | scaleX skewX transX | * | skewY scaleY transY | * | persp0 persp1 persp2 | * * @param scaleX horizontal scale factor to store * @param skewX horizontal skew factor to store * @param transX horizontal translation to store * @param skewY vertical skew factor to store * @param scaleY vertical scale factor to store * @param transY vertical translation to store * @param persp0 input x-axis values perspective factor to store * @param persp1 input y-axis values perspective factor to store * @param persp2 perspective scale factor to store */ setAll(scaleX, skewX, transX, skewY, scaleY, transY, persp0, persp1, persp2) { this._nativeMatrix.setAll(scaleX, skewX, transX, skewY, scaleY, transY, persp0, persp1, persp2); } setAffine(a, b, c, d, tx, ty) { this._nativeMatrix.setAffine(a, b, c, d, tx, ty); } /** * Copies nine scalar values contained by Matrix into buffer, in member value ascending order: * kMScaleX, kMSkewX, kMTransX, kMSkewY, kMScaleY, kMTransY, kMPersp0, kMPersp1, kMPersp2. * @param buffer storage for nine scalar values */ get9() { return this._nativeMatrix.get9(); } /** * Sets Matrix to nine scalar values in buffer, in member value ascending order: kMScaleX, * kMSkewX, kMTransX, kMSkewY, kMScaleY, kMTransY, kMPersp0, kMPersp1, kMPersp2. * * Sets matrix to: * * | buffer[0] buffer[1] buffer[2] | * | buffer[3] buffer[4] buffer[5] | * | buffer[6] buffer[7] buffer[8] | * * @param buffer nine scalar values */ set9(buffer) { this._nativeMatrix.set9(buffer); } /** * Sets Matrix to identity; which has no effect on mapped Point. Sets Matrix to: * * | 1 0 0 | * | 0 1 0 | * | 0 0 1 | * * Also called setIdentity(){}; use the one that provides better inline documentation. */ reset() { this._nativeMatrix.reset(); } /** * Sets Matrix to identity; which has no effect on mapped Point. Sets Matrix to: * * | 1 0 0 | * | 0 1 0 | * | 0 0 1 | * * Also called reset(){}; use the one that provides better inline documentation. */ setIdentity() { this._nativeMatrix.setIdentity(); } /** * Sets Matrix to translate by (dx, dy). * @param dx horizontal translation * @param dy vertical translation */ setTranslate(dx, dy) { this._nativeMatrix.setTranslate(dx, dy); } /** * Sets Matrix to scale by sx and sy, about a pivot point at (px, py). The pivot point is * unchanged when mapped with Matrix. * @param sx horizontal scale factor * @param sy vertical scale factor * @param px pivot on x-axis * @param py pivot on y-axis */ setScale(sx, sy, px, py) { if (px === undefined || py === undefined) { this._nativeMatrix.setScale_xy(sx, sy); } else { this._nativeMatrix.setScale(sx, sy, px, py); } } /** * Sets Matrix to rotate by degrees about a pivot point at (px, py). The pivot point is * unchanged when mapped with Matrix. Positive degrees rotates clockwise. * @param degrees angle of axes relative to upright axes * @param px pivot on x-axis * @param py pivot on y-axis */ setRotate(degrees, px, py) { if (px === undefined || py === undefined) { this._nativeMatrix.setRotate_deg(degrees); } else { this._nativeMatrix.setRotate(degrees, px, py); } } ; /** * Sets Matrix to rotate by sinValue and cosValue, about a pivot point at (px, py). * The pivot point is unchanged when mapped with Matrix. * Vector (sinValue, cosValue) describes the angle of rotation relative to (0, 1). * Vector length specifies scale. */ setSinCos(sinV, cosV, px, py) { if (px === undefined || py === undefined) { this._nativeMatrix.setSinCos_sc(sinV, cosV); } else { this._nativeMatrix.setSinCos(sinV, cosV, px, py); } } ; /** * Sets Matrix to skew by kx and ky, about a pivot point at (px, py). The pivot point is * unchanged when mapped with Matrix. * @param kx horizontal skew factor * @param ky vertical skew factor * @param px pivot on x-axis * @param py pivot on y-axis */ setSkew(kx, ky, px, py) { if (px === undefined || py === undefined) { this._nativeMatrix.setSkew_xy(kx, ky); } else { this._nativeMatrix.setSkew(kx, ky, px, py); } } /** * Sets Matrix to Matrix a multiplied by Matrix b. Either a or b may be this. * * Given: * * | A B C | | J K L | * a = | D E F |, b = | M N O | * | G H I | | P Q R | * * sets Matrix to: * * | A B C | | J K L | | AJ+BM+CP AK+BN+CQ AL+BO+CR | * a * b = | D E F | * | M N O | = | DJ+EM+FP DK+EN+FQ DL+EO+FR | * | G H I | | P Q R | | GJ+HM+IP GK+HN+IQ GL+HO+IR | * * @param a Matrix on left side of multiply expression * @param b Matrix on right side of multiply expression */ setConcat(a, b) { this._nativeMatrix.setConcat(a._nativeMatrix, b._nativeMatrix); } ; /** * Preconcats the matrix with the specified translate. M' = M * T(dx, dy) */ preTranslate(dx, dy) { this._nativeMatrix.preTranslate(dx, dy); } ; /** * Postconcats the matrix with the specified scale. M' = S(sx, sy, px, py) * M */ preScale(sx, sy, px, py) { if (px === undefined || py === undefined) { this._nativeMatrix.preScale_xy(sx, sy); } else { this._nativeMatrix.preScale(sx, sy, px, py); } } ; /** * Preconcats the matrix with the specified rotation. M' = M * R(degrees, px, py) */ preRotate(degrees, px, py) { if (px === undefined || py === undefined) { this._nativeMatrix.preRotate_deg(degrees); } else { this._nativeMatrix.preRotate(degrees, px, py); } } ; /** * Preconcats the matrix with the specified skew. M' = M * K(kx, ky, px, py) */ preSkew(kx, ky, px, py) { if (px === undefined || py === undefined) { this._nativeMatrix.preSkew_xy(kx, ky); } else { this._nativeMatrix.preSkew(kx, ky, px, py); } } ; /** * Preconcats the matrix with the specified matrix. M' = M * other */ preConcat(other) { this._nativeMatrix.preConcat(other._nativeMatrix); } ; /** * Postconcats the matrix with the specified translation. M' = T(dx, dy) * M */ postTranslate(dx, dy) { this._nativeMatrix.postTranslate(dx, dy); } ; /** * Postconcats the matrix with the specified scale. M' = S(sx, sy, px, py) * M */ postScale(sx, sy, px, py) { if (px === undefined || py === undefined) { this._nativeMatrix.postScale_xy(sx, sy); } else { this._nativeMatrix.postScale(sx, sy, px, py); } } ; /** * Postconcats the matrix with the specified rotation. M' = R(degrees, px, py) * M */ postRotate(degrees, px, py) { if (px === undefined || py === undefined) { this._nativeMatrix.postRotate_deg(degrees); } else { this._nativeMatrix.postRotate(degrees, px, py); } } ; /** * Postconcats the matrix with the specified skew. M' = K(kx, ky, px, py) * M */ postSkew(kx, ky, px, py) { if (px === undefined || py === undefined) { this._nativeMatrix.postSkew_xy(kx, ky); } else { this._nativeMatrix.postSkew(kx, ky, px, py); } } ; /** * Postconcats the matrix with the specified matrix. M' = other * M */ postConcat(other) { this._nativeMatrix.postConcat(other._nativeMatrix); } ; /** * If this matrix can be inverted, return true and if inverse is not null, set inverse to be the * inverse of this matrix. If this matrix cannot be inverted, ignore inverse and return false. */ invert(inverse) { return this._nativeMatrix.invert(inverse._nativeMatrix); } invertible() { return this._nativeMatrix.invertible(); } ; /** * Returns Point (x, y) multiplied by Matrix. Given: * * | A B C | | x | * Matrix = | D E F |, pt = | y | * | G H I | | 1 | * * result is computed as: * * |A B C| |x| Ax+By+C Dx+Ey+F * Matrix * pt = |D E F| |y| = |Ax+By+C Dx+Ey+F Gx+Hy+I| = ------- , ------- * |G H I| |1| Gx+Hy+I Gx+Hy+I * * @param x x-axis value of Point to map * @param y y-axis value of Point to map * @return mapped Point */ mapXY(x, y) { return this._nativeMatrix.mapXY(x, y); } /** * Returns bounds of src corners mapped by Matrix. */ mapRect(src) { const rect = this._nativeMatrix.mapRect(src.build()); return new Rect(rect); } /** * Returns the minimum scaling factor of Matrix by decomposing the scaling and skewing elements. * Returns -1 if scale factor overflows or Matrix contains perspective. */ getMinScale() { return this._nativeMatrix.getMinScale(); } ; /** * Returns the maximum scaling factor of Matrix by decomposing the scaling and skewing elements. * Returns -1 if scale factor overflows or Matrix contains perspective. */ getMaxScale() { return this._nativeMatrix.getMaxScale(); } ; /** * Returns true if all elements of the matrix are finite. Returns false if any * element is infinity, or NaN. */ isFinite() { return this._nativeMatrix.isFinite(); } ; makeNativeMatrix() { return new tav.TAVMatrix(); } } __decorate([ allowCallNativeAnytime ], Matrix.prototype, "isIdentity", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "get", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "getScaleX", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "getScaleY", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "getSkewY", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "getSkewX", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "getTranslateX", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "getTranslateY", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "set", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "setScaleX", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "setScaleY", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "setSkewY", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "setSkewX", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "setTranslateX", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "setTranslateY", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "setAll", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "setAffine", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "get9", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "set9", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "reset", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "setIdentity", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "setTranslate", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "setScale", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "setRotate", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "setSinCos", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "setSkew", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "setConcat", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "preTranslate", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "preScale", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "preRotate", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "preSkew", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "preConcat", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "postTranslate", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "postScale", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "postRotate", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "postSkew", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "postConcat", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "invert", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "invertible", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "mapXY", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "mapRect", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "getMinScale", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "getMaxScale", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "isFinite", null); __decorate([ allowCallNativeAnytime ], Matrix.prototype, "makeNativeMatrix", null); __decorate([ allowCallNativeAnytime ], Matrix, "MakeScale", null); __decorate([ allowCallNativeAnytime ], Matrix, "MakeTrans", null); __decorate([ allowCallNativeAnytime ], Matrix, "MakeAll", null);