aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/Transform.ts
blob: 1a07dd6ae88a6c1551f7f29be130d04f70b83058 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
export class Transform {
    private _translateX: number = 0;
    private _translateY: number = 0;
    private _scale: number = 1;
    private _rotate: number = 0;

    static Identity(): Transform {
        return new Transform(0, 0, 1);
    }

    get TranslateX(): number {
        return this._translateX;
    }
    get TranslateY(): number {
        return this._translateY;
    }
    get Scale(): number {
        return this._scale;
    }
    get Rotate(): number {
        return this._rotate;
    }
    get RotateDeg(): number {
        return (this._rotate * 180) / Math.PI;
    }

    /**
     * Represents a transformation/scale matrix (can contain a rotation value, but it is not used when transforming points)
     * @param x
     * @param y
     * @param scale
     * @param rotation NOTE: this is passed along but is NOT used by any of the transformation functionsStores
     */
    constructor(x: number, y: number, scale: number, rotationRadians?: number) {
        this._translateX = x;
        this._translateY = y;
        this._scale = scale;
        this._rotate = rotationRadians ?? 0;
    }

    /**
     * Rotate in radians
     * @param rot
     * @returns the modified transformation
     */
    rotate = (rot: number): this => {
        this._rotate += rot;
        return this;
    };
    /**
     * Rotation in degrees
     * @param rot
     * @returns the modified transformation
     */
    rotateDeg = (rot: number): this => {
        this._rotate += (rot * Math.PI) / 180;
        return this;
    };

    translate = (x: number, y: number): this => {
        this._translateX += x;
        this._translateY += y;
        return this;
    };

    scale = (scale: number): this => {
        this._scale *= scale;
        this._translateX *= scale;
        this._translateY *= scale;
        return this;
    };

    scaleAbout = (scale: number, x: number, y: number): this => {
        this._translateX += x * this._scale - x * this._scale * scale;
        this._translateY += y * this._scale - y * this._scale * scale;
        this._scale *= scale;
        return this;
    };

    transform = (transform: Transform): this => {
        this._translateX = transform._translateX + transform._scale * this._translateX;
        this._translateY = transform._translateY + transform._scale * this._translateY;
        this._scale *= transform._scale;
        return this;
    };

    preTranslate = (x: number, y: number): this => {
        this._translateX += this._scale * x;
        this._translateY += this._scale * y;
        return this;
    };

    preScale = (scale: number): this => {
        this._scale *= scale;
        return this;
    };

    preTransform = (transform: Transform): this => {
        this._translateX += transform._translateX * this._scale;
        this._translateY += transform._translateY * this._scale;
        this._scale *= transform._scale;
        return this;
    };

    translated = (x: number, y: number): Transform => this.copy().translate(x, y);

    preTranslated = (x: number, y: number): Transform => this.copy().preTranslate(x, y);

    scaled = (scale: number): Transform => this.copy().scale(scale);

    scaledAbout = (scale: number, x: number, y: number): Transform => this.copy().scaleAbout(scale, x, y);

    preScaled = (scale: number): Transform => this.copy().preScale(scale);

    transformed = (transform: Transform): Transform => this.copy().transform(transform);

    preTransformed = (transform: Transform): Transform => this.copy().preTransform(transform);

    transformPoint = (x: number, y: number): [number, number] => [x * this._scale + this._translateX, y * this._scale + this._translateY];

    transformDirection = (x: number, y: number): [number, number] => [x * this._scale, y * this._scale];

    transformBounds(x: number, y: number, width: number, height: number): { x: number; y: number; width: number; height: number } {
        const [tx, ty] = this.transformPoint(x, y);
        const [twidth, theight] = this.transformDirection(width, height);
        return { x: tx, y: ty, width: twidth, height: theight };
    }

    inverse = () => new Transform(-this._translateX / this._scale, -this._translateY / this._scale, 1 / this._scale, -this._rotate);

    copy = () => new Transform(this._translateX, this._translateY, this._scale, this._rotate);
}