canvas/unpackage/dist/dev/mp-weixin/pages/index/transform-matrix.js

66 lines
1.6 KiB
JavaScript

"use strict";
class TransformMatrix {
constructor() {
this.reset();
}
reset() {
this.a = 1;
this.b = 0;
this.c = 0;
this.d = 1;
this.tx = 0;
this.ty = 0;
this.stack = [];
}
translate(dx, dy) {
this.tx += this.a * dx + this.c * dy;
this.ty += this.b * dx + this.d * dy;
}
scale(sx, sy, cx = 0, cy = 0) {
this.translate(cx, cy);
this.a *= sx;
this.b *= sx;
this.c *= sy;
this.d *= sy;
this.translate(-cx, -cy);
}
toArray() {
return [this.a, this.b, this.c, this.d, this.tx, this.ty];
}
// 用于调试的字符串表示
toString() {
return `[${this.a.toFixed(2)}, ${this.b.toFixed(2)}, ${this.c.toFixed(2)}, ${this.d.toFixed(2)}, ${this.tx.toFixed(1)}, ${this.ty.toFixed(1)}]`;
}
// 克隆方法
clone() {
const clone = new TransformMatrix();
clone.a = this.a;
clone.b = this.b;
clone.c = this.c;
clone.d = this.d;
clone.tx = this.tx;
clone.ty = this.ty;
return clone;
}
// 添加点坐标转换方法
transformPoint(x, y) {
return {
x: this.a * x + this.c * y + this.tx,
y: this.b * x + this.d * y + this.ty
};
}
// 添加逆矩阵转换方法
invertPoint(x, y) {
const det = this.a * this.d - this.b * this.c;
if (Math.abs(det) < 1e-4) {
return { x, y };
}
return {
x: (this.d * (x - this.tx) - this.c * (y - this.ty)) / det,
y: (this.a * (y - this.ty) - this.b * (x - this.tx)) / det
};
}
}
exports.TransformMatrix = TransformMatrix;
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/index/transform-matrix.js.map