Matrix2D - bobby169/createjsDoc GitHub Wiki

append(a, b, c, d, tx, ty)

将指定的矩阵属性附加到此矩阵,这相当于乘以(此矩阵)*(指定矩阵)

prepend(a, b, c, d, tx, ty)

Prepends the specified matrix properties to this matrix.这相当于乘以(指定矩阵)*(此矩阵)

appendMatrix(matrix) 与append一样

p.appendMatrix = function(matrix) {
    return this.append(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty);
};

prependMatrix(matrix) 与prepend一样

p.prependMatrix = function(matrix) {
    return this.prepend(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty);
};

appendTransform(x, y, scaleX, scaleY, rotation, skewX, skewY, regX, regY)

从指定的显示对象转换属性生成矩阵属性,并将其附加到此矩阵。非常有用

function demo1 () {
  let box = game.add.box(100, 100, 'red', {
    x: 100,
    y: 100,
    rotation: 60
  })
  console.info(box)
  let mtx = new createjs.Matrix2D()
  // 把box的位置信息转换成一个Matrix2D,并append到当前的Matrix2D上
  let resMtx = mtx.appendTransform(box.x, box.y, box.scaleX, box.scaleY, box.rotation)
  console.info(resMtx)

  let box2 = game.add.box(50, 50, 'blue')
  // 现在box2会用box的矩阵变换
  // box2.transformMatrix = resMtx

  // 或直接获取box的matrix
  box2.transformMatrix = box.getMatrix()
}

prependTransform(x, y, scaleX, scaleY, rotation, skewX, skewY, regX, regY)

从指定的显示对象转换属性生成矩阵属性,并将其前置到此矩阵。非常有用

function demo2 () {
  let con1 = new createjs.Container()
  con1.set({ x: 100, y: 100, rotation: 30 })
  let box = game.make.box(100, 100, 'red', {
    x: 100,
    y: 100,
    rotation: 30
  })
  con1.addChild(box)
  game.stage.addChild(con1)

  let resMtx = new createjs.Matrix2D()

  // 一次性把box的所有父级变换计算出来
  do {
    // prepend each parent's transformation in turn:
    resMtx.prependTransform(box.x, box.y, box.scaleX, box.scaleY, box.rotation, box.skewX, box.skewY, box.regX, box.regY)
  } while ((box = box.parent))
  console.info(resMtx)

  let box2 = game.make.box(50, 50, 'blue')
  game.stage.addChild(box2)
  // 现在box2整体rotation = 60, 位移是x = 200, y = 200
  box2.transformMatrix = resMtx
}

invert()

反转矩阵,使其执行相反的转换

rotate(angle)

对矩阵应用顺时针旋转转换。 比较少用

skew(skewX, skew)

对矩阵应用倾斜转换。比较少用

scale(x, y)

对矩阵应用缩放转换。比较少用

translate(x, y)

Translates the matrix on the x and y axes.

p._cacheDraw = function (gl, target, filters, manager) {
    var mtx = target.getMatrix();
    mtx = mtx.clone();
    mtx.scale(1/manager.scale, 1/manager.scale);
    mtx = mtx.invert();
    mtx.translate(-manager.offX/manager.scale*target.scaleX, -manager.offY/manager.scale*target.scaleY);
}

identity()

重置变换矩阵,即应用空转换,等于context.setTransfrom(1,0,0,1,0,0)

p.identity = function() {
    this.a = this.d = 1;
    this.b = this.c = this.tx = this.ty = 0;
    return this;
};

isIdentity()

判断一个矩阵是否重置

p.isIdentity = function() {
    return this.tx === 0 && this.ty === 0 && this.a === 1 && this.b === 0 && this.c === 0 && this.d === 1;
};

equals(matrix)

判断一个矩阵是否与当前矩阵相等

p.equals = function(matrix) {
    return this.tx === matrix.tx && this.ty === matrix.ty && this.a === matrix.a && this.b === matrix.b && this.c === matrix.c && this.d === matrix.d;
};

transformPoint(x, y, pt)

根据这个矩阵变换一个点

p.localToGlobal = function(x, y, pt) {
  return this.getConcatenatedMatrix(this._props.matrix).transformPoint(x,y, pt||new createjs.Point());
};

decompose(target)

将矩阵分解为变换属性(x、y、scaleX、scaleY、rotation)。请注意,这些值可能与用于生成矩阵的转换属性不匹配,尽管它们将产生相同的视觉效果。

copy(matrix)

Copies all properties from the specified matrix to this matrix.

p.copy = function(matrix) {
    return this.setValues(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty);
};

clone()

Returns a clone of the Matrix2D instance.

p.clone = function() {
    return new Matrix2D(this.a, this.b, this.c, this.d, this.tx, this.ty);
};