forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixStack.js
More file actions
146 lines (117 loc) · 4.08 KB
/
Copy pathMatrixStack.js
File metadata and controls
146 lines (117 loc) · 4.08 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var MatrixStack = {
matrixStack: null,
currentMatrix: null,
currentMatrixIndex: 0,
initMatrixStack: function ()
{
this.matrixStack = new Float32Array(6000); // up to 1000 matrices
this.currentMatrix = new Float32Array([ 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 ]);
this.currentMatrixIndex = 0;
return this;
},
save: function ()
{
if (this.currentMatrixIndex >= this.matrixStack.length) { return this; }
var matrixStack = this.matrixStack;
var currentMatrix = this.currentMatrix;
var currentMatrixIndex = this.currentMatrixIndex;
this.currentMatrixIndex += 6;
matrixStack[currentMatrixIndex + 0] = currentMatrix[0];
matrixStack[currentMatrixIndex + 1] = currentMatrix[1];
matrixStack[currentMatrixIndex + 2] = currentMatrix[2];
matrixStack[currentMatrixIndex + 3] = currentMatrix[3];
matrixStack[currentMatrixIndex + 4] = currentMatrix[4];
matrixStack[currentMatrixIndex + 5] = currentMatrix[5];
return this;
},
restore: function ()
{
if (this.currentMatrixIndex <= 0) { return this; }
this.currentMatrixIndex -= 6;
var matrixStack = this.matrixStack;
var currentMatrix = this.currentMatrix;
var currentMatrixIndex = this.currentMatrixIndex;
currentMatrix[0] = matrixStack[currentMatrixIndex + 0];
currentMatrix[1] = matrixStack[currentMatrixIndex + 1];
currentMatrix[2] = matrixStack[currentMatrixIndex + 2];
currentMatrix[3] = matrixStack[currentMatrixIndex + 3];
currentMatrix[4] = matrixStack[currentMatrixIndex + 4];
currentMatrix[5] = matrixStack[currentMatrixIndex + 5];
return this;
},
loadIdentity: function ()
{
this.setTransform(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
return this;
},
transform: function (a, b, c, d, tx, ty)
{
var currentMatrix = this.currentMatrix;
var m0 = currentMatrix[0];
var m1 = currentMatrix[1];
var m2 = currentMatrix[2];
var m3 = currentMatrix[3];
var m4 = currentMatrix[4];
var m5 = currentMatrix[5];
currentMatrix[0] = m0 * a + m2 * b;
currentMatrix[1] = m1 * a + m3 * b;
currentMatrix[2] = m0 * c + m2 * d;
currentMatrix[3] = m1 * c + m3 * d;
currentMatrix[4] = m0 * tx + m2 * ty + m4;
currentMatrix[5] = m1 * tx + m3 * ty + m5;
return this;
},
setTransform: function (a, b, c, d, tx, ty)
{
var currentMatrix = this.currentMatrix;
currentMatrix[0] = a;
currentMatrix[1] = b;
currentMatrix[2] = c;
currentMatrix[3] = d;
currentMatrix[4] = tx;
currentMatrix[5] = ty;
return this;
},
translate: function (x, y)
{
var currentMatrix = this.currentMatrix;
var m0 = currentMatrix[0];
var m1 = currentMatrix[1];
var m2 = currentMatrix[2];
var m3 = currentMatrix[3];
var m4 = currentMatrix[4];
var m5 = currentMatrix[5];
currentMatrix[4] = m0 * x + m2 * y + m4;
currentMatrix[5] = m1 * x + m3 * y + m5;
return this;
},
scale: function (x, y)
{
var currentMatrix = this.currentMatrix;
var m0 = currentMatrix[0];
var m1 = currentMatrix[1];
var m2 = currentMatrix[2];
var m3 = currentMatrix[3];
currentMatrix[0] = m0 * x;
currentMatrix[1] = m1 * x;
currentMatrix[2] = m2 * y;
currentMatrix[3] = m3 * y;
return this;
},
rotate: function (t)
{
var currentMatrix = this.currentMatrix;
var m0 = currentMatrix[0];
var m1 = currentMatrix[1];
var m2 = currentMatrix[2];
var m3 = currentMatrix[3];
var st = Math.sin(t);
var ct = Math.cos(t);
currentMatrix[0] = m0 * ct + m2 * st;
currentMatrix[1] = m1 * ct + m3 * st;
currentMatrix[2] = m0 * -st + m2 * ct;
currentMatrix[3] = m1 * -st + m3 * ct;
return this;
}
};
module.exports = MatrixStack;