Skip to content

Matrix2D

何波 edited this page Feb 22, 2019 · 7 revisions

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
}

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);
}

Clone this wiki locally