UNPKG

3.75 kBJavaScriptView Raw
1// Copyright 2014 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15(function(scope, testing) {
16 var composeMatrix = (function() {
17 function multiply(a, b) {
18 var result = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]];
19 for (var i = 0; i < 4; i++) {
20 for (var j = 0; j < 4; j++) {
21 for (var k = 0; k < 4; k++) {
22 result[i][j] += b[i][k] * a[k][j];
23 }
24 }
25 }
26 return result;
27 }
28
29 function is2D(m) {
30 return (
31 m[0][2] == 0 &&
32 m[0][3] == 0 &&
33 m[1][2] == 0 &&
34 m[1][3] == 0 &&
35 m[2][0] == 0 &&
36 m[2][1] == 0 &&
37 m[2][2] == 1 &&
38 m[2][3] == 0 &&
39 m[3][2] == 0 &&
40 m[3][3] == 1);
41 }
42
43 function composeMatrix(translate, scale, skew, quat, perspective) {
44 var matrix = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
45
46 for (var i = 0; i < 4; i++) {
47 matrix[i][3] = perspective[i];
48 }
49
50 for (var i = 0; i < 3; i++) {
51 for (var j = 0; j < 3; j++) {
52 matrix[3][i] += translate[j] * matrix[j][i];
53 }
54 }
55
56 var x = quat[0], y = quat[1], z = quat[2], w = quat[3];
57
58 var rotMatrix = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
59
60 rotMatrix[0][0] = 1 - 2 * (y * y + z * z);
61 rotMatrix[0][1] = 2 * (x * y - z * w);
62 rotMatrix[0][2] = 2 * (x * z + y * w);
63 rotMatrix[1][0] = 2 * (x * y + z * w);
64 rotMatrix[1][1] = 1 - 2 * (x * x + z * z);
65 rotMatrix[1][2] = 2 * (y * z - x * w);
66 rotMatrix[2][0] = 2 * (x * z - y * w);
67 rotMatrix[2][1] = 2 * (y * z + x * w);
68 rotMatrix[2][2] = 1 - 2 * (x * x + y * y);
69
70 matrix = multiply(matrix, rotMatrix);
71
72 var temp = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
73 if (skew[2]) {
74 temp[2][1] = skew[2];
75 matrix = multiply(matrix, temp);
76 }
77
78 if (skew[1]) {
79 temp[2][1] = 0;
80 temp[2][0] = skew[0];
81 matrix = multiply(matrix, temp);
82 }
83
84 if (skew[0]) {
85 temp[2][0] = 0;
86 temp[1][0] = skew[0];
87 matrix = multiply(matrix, temp);
88 }
89
90 for (var i = 0; i < 3; i++) {
91 for (var j = 0; j < 3; j++) {
92 matrix[i][j] *= scale[i];
93 }
94 }
95
96 if (is2D(matrix)) {
97 return [matrix[0][0], matrix[0][1], matrix[1][0], matrix[1][1], matrix[3][0], matrix[3][1]];
98 }
99 return matrix[0].concat(matrix[1], matrix[2], matrix[3]);
100 }
101 return composeMatrix;
102 })();
103
104 function clamp(x, min, max) {
105 return Math.max(Math.min(x, max), min);
106 };
107
108 function quat(fromQ, toQ, f) {
109 var product = scope.dot(fromQ, toQ);
110 product = clamp(product, -1.0, 1.0);
111
112 var quat = [];
113 if (product === 1.0) {
114 quat = fromQ;
115 } else {
116 var theta = Math.acos(product);
117 var w = Math.sin(f * theta) * 1 / Math.sqrt(1 - product * product);
118
119 for (var i = 0; i < 4; i++) {
120 quat.push(fromQ[i] * (Math.cos(f * theta) - product * w) +
121 toQ[i] * w);
122 }
123 }
124 return quat;
125 }
126
127 scope.composeMatrix = composeMatrix;
128 scope.quat = quat;
129
130})(webAnimations1, webAnimationsTesting);