57 lines
1.2 KiB
JavaScript
57 lines
1.2 KiB
JavaScript
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;
|
|
}
|
|
}
|
|
|
|
export default TransformMatrix; |