1 // Copyright 2014 Google Inc. All rights reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
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];
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]];
46 for (var i = 0; i < 4; i++) {
47 matrix[i][3] = perspective[i];
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];
56 var x = quat[0], y = quat[1], z = quat[2], w = quat[3];
58 var rotMatrix = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
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);
70 matrix = multiply(matrix, rotMatrix);
72 var temp = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
75 matrix = multiply(matrix, temp);
81 matrix = multiply(matrix, temp);
87 matrix = multiply(matrix, temp);
90 for (var i = 0; i < 3; i++) {
91 for (var j = 0; j < 3; j++) {
92 matrix[i][j] *= scale[i];
97 return [matrix[0][0], matrix[0][1], matrix[1][0], matrix[1][1], matrix[3][0], matrix[3][1]];
99 return matrix[0].concat(matrix[1], matrix[2], matrix[3]);
101 return composeMatrix;
104 function clamp(x, min, max) {
105 return Math.max(Math.min(x, max), min);
108 function quat(fromQ, toQ, f) {
109 var product = scope.dot(fromQ, toQ);
110 product = clamp(product, -1.0, 1.0);
113 if (product === 1.0) {
116 var theta = Math.acos(product);
117 var w = Math.sin(f * theta) * 1 / Math.sqrt(1 - product * product);
119 for (var i = 0; i < 4; i++) {
120 quat.push(fromQ[i] * (Math.cos(f * theta) - product * w) +
127 scope.composeMatrix = composeMatrix;
130 })(webAnimations1, webAnimationsTesting);