OLD | NEW |
1 /* | 1 /* |
2 Distributed under both the W3C Test Suite License [1] and the W3C | 2 Distributed under both the W3C Test Suite License [1] and the W3C |
3 3-clause BSD License [2]. To contribute to a W3C Test Suite, see the | 3 3-clause BSD License [2]. To contribute to a W3C Test Suite, see the |
4 policies and contribution forms [3]. | 4 policies and contribution forms [3]. |
5 | 5 |
6 [1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license | 6 [1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license |
7 [2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license | 7 [2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license |
8 [3] http://www.w3.org/2004/10/27-testcases | 8 [3] http://www.w3.org/2004/10/27-testcases |
9 */ | 9 */ |
10 | 10 |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 return new Promise(function(resolve) { | 167 return new Promise(function(resolve) { |
168 (function handleFrame() { | 168 (function handleFrame() { |
169 if (document.timeline.currentTime - startTime >= minDelay) { | 169 if (document.timeline.currentTime - startTime >= minDelay) { |
170 resolve(); | 170 resolve(); |
171 } else { | 171 } else { |
172 window.requestAnimationFrame(handleFrame); | 172 window.requestAnimationFrame(handleFrame); |
173 } | 173 } |
174 }()); | 174 }()); |
175 }); | 175 }); |
176 } | 176 } |
| 177 |
| 178 // Returns 'matrix()' or 'matrix3d()' function string generated from an array. |
| 179 function createMatrixFromArray(array) { |
| 180 return (array.length == 16 ? 'matrix3d' : 'matrix') + |
| 181 '(' + array.join() + ')'; |
| 182 } |
| 183 |
| 184 // Returns 'matrix3d()' function string equivalent to |
| 185 // 'rotate3d(x, y, z, radian)'. |
| 186 function rotate3dToMatrix3d(x, y, z, radian) { |
| 187 return createMatrixFromArray(rotate3dToMatrix(x, y, z, radian)); |
| 188 } |
| 189 |
| 190 // Returns an array of the 4x4 matrix equivalent to 'rotate3d(x, y, z, radian)'. |
| 191 // https://www.w3.org/TR/css-transforms-1/#Rotate3dDefined |
| 192 function rotate3dToMatrix(x, y, z, radian) { |
| 193 var sc = Math.sin(radian / 2) * Math.cos(radian / 2); |
| 194 var sq = Math.sin(radian / 2) * Math.sin(radian / 2); |
| 195 |
| 196 // Normalize the vector. |
| 197 var length = Math.sqrt(x*x + y*y + z*z); |
| 198 x /= length; |
| 199 y /= length; |
| 200 z /= length; |
| 201 |
| 202 return [ |
| 203 1 - 2 * (y*y + z*z) * sq, |
| 204 2 * (x * y * sq + z * sc), |
| 205 2 * (x * z * sq - y * sc), |
| 206 0, |
| 207 2 * (x * y * sq - z * sc), |
| 208 1 - 2 * (x*x + z*z) * sq, |
| 209 2 * (y * z * sq + x * sc), |
| 210 0, |
| 211 2 * (x * z * sq + y * sc), |
| 212 2 * (y * z * sq - x * sc), |
| 213 1 - 2 * (x*x + y*y) * sq, |
| 214 0, |
| 215 0, |
| 216 0, |
| 217 0, |
| 218 1 |
| 219 ]; |
| 220 } |
| 221 |
| 222 // Compare matrix string like 'matrix(1, 0, 0, 1, 100, 0)' with tolerances. |
| 223 function assert_matrix_equals(actual, expected, description) { |
| 224 var matrixRegExp = /^matrix(?:3d)*\((.+)\)/; |
| 225 assert_regexp_match(actual, matrixRegExp, |
| 226 'Actual value is not a matrix') |
| 227 assert_regexp_match(expected, matrixRegExp, |
| 228 'Expected value is not a matrix'); |
| 229 |
| 230 var actualMatrixArray = |
| 231 actual.match(matrixRegExp)[1].split(',').map(Number); |
| 232 var expectedMatrixArray = |
| 233 expected.match(matrixRegExp)[1].split(',').map(Number); |
| 234 |
| 235 assert_equals(actualMatrixArray.length, expectedMatrixArray.length, |
| 236 'dimension of the matrix: ' + description); |
| 237 for (var i = 0; i < actualMatrixArray.length; i++) { |
| 238 assert_approx_equals(actualMatrixArray[i], expectedMatrixArray[i], 0.0001, |
| 239 'expecetd ' + expected + ' but got ' + actual + ": " + description); |
| 240 } |
| 241 } |
OLD | NEW |