1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | (function (global, factory) {
|
24 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
25 | typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
26 | (factory((global.Rematrix = {})));
|
27 | }(this, (function (exports) { 'use strict';
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function format (source) {
|
46 | if (source.constructor !== Array) {
|
47 | throw new TypeError('Expected array.')
|
48 | }
|
49 | if (source.length === 16) {
|
50 | return source
|
51 | }
|
52 | if (source.length === 6) {
|
53 | var matrix = identity();
|
54 | matrix[0] = source[0];
|
55 | matrix[1] = source[1];
|
56 | matrix[4] = source[2];
|
57 | matrix[5] = source[3];
|
58 | matrix[12] = source[4];
|
59 | matrix[13] = source[5];
|
60 | return matrix
|
61 | }
|
62 | throw new RangeError('Expected array with either 6 or 16 values.')
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function identity () {
|
74 | var matrix = [];
|
75 | for (var i = 0; i < 16; i++) {
|
76 | i % 5 == 0 ? matrix.push(1) : matrix.push(0);
|
77 | }
|
78 | return matrix
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | function inverse (source) {
|
92 | var m = format(source);
|
93 |
|
94 | var s0 = m[0] * m[5] - m[4] * m[1];
|
95 | var s1 = m[0] * m[6] - m[4] * m[2];
|
96 | var s2 = m[0] * m[7] - m[4] * m[3];
|
97 | var s3 = m[1] * m[6] - m[5] * m[2];
|
98 | var s4 = m[1] * m[7] - m[5] * m[3];
|
99 | var s5 = m[2] * m[7] - m[6] * m[3];
|
100 |
|
101 | var c5 = m[10] * m[15] - m[14] * m[11];
|
102 | var c4 = m[9] * m[15] - m[13] * m[11];
|
103 | var c3 = m[9] * m[14] - m[13] * m[10];
|
104 | var c2 = m[8] * m[15] - m[12] * m[11];
|
105 | var c1 = m[8] * m[14] - m[12] * m[10];
|
106 | var c0 = m[8] * m[13] - m[12] * m[9];
|
107 |
|
108 | var determinant = 1 / (s0 * c5 - s1 * c4 + s2 * c3 + s3 * c2 - s4 * c1 + s5 * c0);
|
109 |
|
110 | if (isNaN(determinant) || determinant === Infinity) {
|
111 | throw new Error('Inverse determinant attempted to divide by zero.')
|
112 | }
|
113 |
|
114 | return [
|
115 | (m[5] * c5 - m[6] * c4 + m[7] * c3) * determinant,
|
116 | (-m[1] * c5 + m[2] * c4 - m[3] * c3) * determinant,
|
117 | (m[13] * s5 - m[14] * s4 + m[15] * s3) * determinant,
|
118 | (-m[9] * s5 + m[10] * s4 - m[11] * s3) * determinant,
|
119 |
|
120 | (-m[4] * c5 + m[6] * c2 - m[7] * c1) * determinant,
|
121 | (m[0] * c5 - m[2] * c2 + m[3] * c1) * determinant,
|
122 | (-m[12] * s5 + m[14] * s2 - m[15] * s1) * determinant,
|
123 | (m[8] * s5 - m[10] * s2 + m[11] * s1) * determinant,
|
124 |
|
125 | (m[4] * c4 - m[5] * c2 + m[7] * c0) * determinant,
|
126 | (-m[0] * c4 + m[1] * c2 - m[3] * c0) * determinant,
|
127 | (m[12] * s4 - m[13] * s2 + m[15] * s0) * determinant,
|
128 | (-m[8] * s4 + m[9] * s2 - m[11] * s0) * determinant,
|
129 |
|
130 | (-m[4] * c3 + m[5] * c1 - m[6] * c0) * determinant,
|
131 | (m[0] * c3 - m[1] * c1 + m[2] * c0) * determinant,
|
132 | (-m[12] * s3 + m[13] * s1 - m[14] * s0) * determinant,
|
133 | (m[8] * s3 - m[9] * s1 + m[10] * s0) * determinant ]
|
134 | }
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | function multiply (m, x) {
|
150 | var fm = format(m);
|
151 | var fx = format(x);
|
152 | var product = [];
|
153 |
|
154 | for (var i = 0; i < 4; i++) {
|
155 | var row = [fm[i], fm[i + 4], fm[i + 8], fm[i + 12]];
|
156 | for (var j = 0; j < 4; j++) {
|
157 | var k = j * 4;
|
158 | var col = [fx[k], fx[k + 1], fx[k + 2], fx[k + 3]];
|
159 | var result = row[0] * col[0] + row[1] * col[1] + row[2] * col[2] + row[3] * col[3];
|
160 |
|
161 | product[i + k] = result;
|
162 | }
|
163 | }
|
164 |
|
165 | return product
|
166 | }
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | function parse (source) {
|
181 | if (typeof source === 'string') {
|
182 | var match = source.match(/matrix(3d)?\(([^)]+)\)/);
|
183 | if (match) {
|
184 | var raw = match[2].split(', ').map(parseFloat);
|
185 | return format(raw)
|
186 | }
|
187 | }
|
188 | return identity()
|
189 | }
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | function rotate (angle) {
|
198 | return rotateZ(angle)
|
199 | }
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 | function rotateX (angle) {
|
208 | var theta = Math.PI / 180 * angle;
|
209 | var matrix = identity();
|
210 |
|
211 | matrix[5] = matrix[10] = Math.cos(theta);
|
212 | matrix[6] = matrix[9] = Math.sin(theta);
|
213 | matrix[9] *= -1;
|
214 |
|
215 | return matrix
|
216 | }
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | function rotateY (angle) {
|
225 | var theta = Math.PI / 180 * angle;
|
226 | var matrix = identity();
|
227 |
|
228 | matrix[0] = matrix[10] = Math.cos(theta);
|
229 | matrix[2] = matrix[8] = Math.sin(theta);
|
230 | matrix[2] *= -1;
|
231 |
|
232 | return matrix
|
233 | }
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | function rotateZ (angle) {
|
242 | var theta = Math.PI / 180 * angle;
|
243 | var matrix = identity();
|
244 |
|
245 | matrix[0] = matrix[5] = Math.cos(theta);
|
246 | matrix[1] = matrix[4] = Math.sin(theta);
|
247 | matrix[4] *= -1;
|
248 |
|
249 | return matrix
|
250 | }
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | function scale (scalar, scalarY) {
|
262 | var matrix = identity();
|
263 |
|
264 | matrix[0] = scalar;
|
265 | matrix[5] = typeof scalarY === 'number' ? scalarY : scalar;
|
266 |
|
267 | return matrix
|
268 | }
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | function scaleX (scalar) {
|
277 | var matrix = identity();
|
278 | matrix[0] = scalar;
|
279 | return matrix
|
280 | }
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 | function scaleY (scalar) {
|
289 | var matrix = identity();
|
290 | matrix[5] = scalar;
|
291 | return matrix
|
292 | }
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 | function scaleZ (scalar) {
|
301 | var matrix = identity();
|
302 | matrix[10] = scalar;
|
303 | return matrix
|
304 | }
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 | function skew (angleX, angleY) {
|
316 | var thetaX = Math.PI / 180 * angleX;
|
317 | var matrix = identity();
|
318 |
|
319 | matrix[4] = Math.tan(thetaX);
|
320 |
|
321 | if (angleY) {
|
322 | var thetaY = Math.PI / 180 * angleY;
|
323 | matrix[1] = Math.tan(thetaY);
|
324 | }
|
325 |
|
326 | return matrix
|
327 | }
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 | function skewX (angle) {
|
336 | var theta = Math.PI / 180 * angle;
|
337 | var matrix = identity();
|
338 |
|
339 | matrix[4] = Math.tan(theta);
|
340 |
|
341 | return matrix
|
342 | }
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 | function skewY (angle) {
|
351 | var theta = Math.PI / 180 * angle;
|
352 | var matrix = identity();
|
353 |
|
354 | matrix[1] = Math.tan(theta);
|
355 |
|
356 | return matrix
|
357 | }
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 | function translate (distanceX, distanceY) {
|
369 | var matrix = identity();
|
370 | matrix[12] = distanceX;
|
371 |
|
372 | if (distanceY) {
|
373 | matrix[13] = distanceY;
|
374 | }
|
375 |
|
376 | return matrix
|
377 | }
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 | function translateX (distance) {
|
386 | var matrix = identity();
|
387 | matrix[12] = distance;
|
388 | return matrix
|
389 | }
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 | function translateY (distance) {
|
398 | var matrix = identity();
|
399 | matrix[13] = distance;
|
400 | return matrix
|
401 | }
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 | function translateZ (distance) {
|
410 | var matrix = identity();
|
411 | matrix[14] = distance;
|
412 | return matrix
|
413 | }
|
414 |
|
415 | exports.format = format;
|
416 | exports.identity = identity;
|
417 | exports.inverse = inverse;
|
418 | exports.multiply = multiply;
|
419 | exports.parse = parse;
|
420 | exports.rotate = rotate;
|
421 | exports.rotateX = rotateX;
|
422 | exports.rotateY = rotateY;
|
423 | exports.rotateZ = rotateZ;
|
424 | exports.scale = scale;
|
425 | exports.scaleX = scaleX;
|
426 | exports.scaleY = scaleY;
|
427 | exports.scaleZ = scaleZ;
|
428 | exports.skew = skew;
|
429 | exports.skewX = skewX;
|
430 | exports.skewY = skewY;
|
431 | exports.translate = translate;
|
432 | exports.translateX = translateX;
|
433 | exports.translateY = translateY;
|
434 | exports.translateZ = translateZ;
|
435 |
|
436 | Object.defineProperty(exports, '__esModule', { value: true });
|
437 |
|
438 | })));
|