transform.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2014 The Closure Library Authors. 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. * @fileoverview Utility methods to deal with CSS3 transforms programmatically.
  16. */
  17. goog.provide('goog.style.transform');
  18. goog.require('goog.functions');
  19. goog.require('goog.math.Coordinate');
  20. goog.require('goog.math.Coordinate3');
  21. goog.require('goog.style');
  22. goog.require('goog.userAgent');
  23. goog.require('goog.userAgent.product.isVersion');
  24. /**
  25. * Whether CSS3 transform translate() is supported. IE 9 supports 2D transforms
  26. * and IE 10 supports 3D transforms. IE 8 supports neither.
  27. * @return {boolean} Whether the current environment supports CSS3 transforms.
  28. */
  29. goog.style.transform.isSupported = goog.functions.cacheReturnValue(function() {
  30. return !goog.userAgent.IE || goog.userAgent.product.isVersion(9);
  31. });
  32. /**
  33. * Whether CSS3 transform translate3d() is supported. If the current browser
  34. * supports this transform strategy.
  35. * @return {boolean} Whether the current environment supports CSS3 transforms.
  36. */
  37. goog.style.transform.is3dSupported =
  38. goog.functions.cacheReturnValue(function() {
  39. return goog.userAgent.WEBKIT || goog.userAgent.EDGE ||
  40. (goog.userAgent.GECKO && goog.userAgent.product.isVersion(10)) ||
  41. (goog.userAgent.IE && goog.userAgent.product.isVersion(10));
  42. });
  43. /**
  44. * Returns the x,y translation component of any CSS transforms applied to the
  45. * element, in pixels.
  46. *
  47. * @param {!Element} element The element to get the translation of.
  48. * @return {!goog.math.Coordinate} The CSS translation of the element in px.
  49. */
  50. goog.style.transform.getTranslation = function(element) {
  51. var transform = goog.style.getComputedTransform(element);
  52. var matrixConstructor = goog.style.transform.matrixConstructor_();
  53. if (transform && matrixConstructor) {
  54. var matrix = new matrixConstructor(transform);
  55. if (matrix) {
  56. return new goog.math.Coordinate(matrix.m41, matrix.m42);
  57. }
  58. }
  59. return new goog.math.Coordinate(0, 0);
  60. };
  61. /**
  62. * Translates an element's position using the CSS3 transform property.
  63. * NOTE: This replaces all other transforms already defined on the element.
  64. * @param {Element} element The element to translate.
  65. * @param {number} x The horizontal translation.
  66. * @param {number} y The vertical translation.
  67. * @return {boolean} Whether the CSS translation was set.
  68. */
  69. goog.style.transform.setTranslation = function(element, x, y) {
  70. if (!goog.style.transform.isSupported()) {
  71. return false;
  72. }
  73. // TODO(user): After http://crbug.com/324107 is fixed, it will be faster to
  74. // use something like: translation = new CSSMatrix().translate(x, y, 0);
  75. var translation = goog.style.transform.is3dSupported() ?
  76. 'translate3d(' + x + 'px,' + y + 'px,' +
  77. '0px)' :
  78. 'translate(' + x + 'px,' + y + 'px)';
  79. goog.style.setStyle(
  80. element, goog.style.transform.getTransformProperty_(), translation);
  81. return true;
  82. };
  83. /**
  84. * Returns the scale of the x, y and z dimensions of CSS transforms applied to
  85. * the element.
  86. *
  87. * @param {!Element} element The element to get the scale of.
  88. * @return {!goog.math.Coordinate3} The scale of the element.
  89. */
  90. goog.style.transform.getScale = function(element) {
  91. var transform = goog.style.getComputedTransform(element);
  92. var matrixConstructor = goog.style.transform.matrixConstructor_();
  93. if (transform && matrixConstructor) {
  94. var matrix = new matrixConstructor(transform);
  95. if (matrix) {
  96. return new goog.math.Coordinate3(matrix.m11, matrix.m22, matrix.m33);
  97. }
  98. }
  99. return new goog.math.Coordinate3(0, 0, 0);
  100. };
  101. /**
  102. * Scales an element using the CSS3 transform property.
  103. * NOTE: This replaces all other transforms already defined on the element.
  104. * @param {!Element} element The element to scale.
  105. * @param {number} x The horizontal scale.
  106. * @param {number} y The vertical scale.
  107. * @param {number} z The depth scale.
  108. * @return {boolean} Whether the CSS scale was set.
  109. */
  110. goog.style.transform.setScale = function(element, x, y, z) {
  111. if (!goog.style.transform.isSupported()) {
  112. return false;
  113. }
  114. var scale = goog.style.transform.is3dSupported() ?
  115. 'scale3d(' + x + ',' + y + ',' + z + ')' :
  116. 'scale(' + x + ',' + y + ')';
  117. goog.style.setStyle(
  118. element, goog.style.transform.getTransformProperty_(), scale);
  119. return true;
  120. };
  121. /**
  122. * Returns the rotation CSS transform applied to the element.
  123. * @param {!Element} element The element to get the rotation of.
  124. * @return {number} The rotation of the element in degrees.
  125. */
  126. goog.style.transform.getRotation = function(element) {
  127. var transform = goog.style.getComputedTransform(element);
  128. var matrixConstructor = goog.style.transform.matrixConstructor_();
  129. if (transform && matrixConstructor) {
  130. var matrix = new matrixConstructor(transform);
  131. if (matrix) {
  132. var x = matrix.m11 + matrix.m22;
  133. var y = matrix.m12 - matrix.m21;
  134. return Math.atan2(y, x) * (180 / Math.PI);
  135. }
  136. }
  137. return 0;
  138. };
  139. /**
  140. * Rotates an element using the CSS3 transform property.
  141. * NOTE: This replaces all other transforms already defined on the element.
  142. * @param {!Element} element The element to rotate.
  143. * @param {number} degrees The number of degrees to rotate by.
  144. * @return {boolean} Whether the CSS rotation was set.
  145. */
  146. goog.style.transform.setRotation = function(element, degrees) {
  147. if (!goog.style.transform.isSupported()) {
  148. return false;
  149. }
  150. var rotation = goog.style.transform.is3dSupported() ?
  151. 'rotate3d(0,0,1,' + degrees + 'deg)' :
  152. 'rotate(' + degrees + 'deg)';
  153. goog.style.setStyle(
  154. element, goog.style.transform.getTransformProperty_(), rotation);
  155. return true;
  156. };
  157. /**
  158. * A cached value of the transform property depending on whether the useragent
  159. * is IE9.
  160. * @return {string} The transform property depending on whether the useragent
  161. * is IE9.
  162. * @private
  163. */
  164. goog.style.transform.getTransformProperty_ =
  165. goog.functions.cacheReturnValue(function() {
  166. return goog.userAgent.IE && goog.userAgent.DOCUMENT_MODE == 9 ?
  167. '-ms-transform' :
  168. 'transform';
  169. });
  170. /**
  171. * Gets the constructor for a CSSMatrix object.
  172. * @return {function(new:CSSMatrix, string)?} A constructor for a CSSMatrix
  173. * object (or null).
  174. * @private
  175. */
  176. goog.style.transform.matrixConstructor_ =
  177. goog.functions.cacheReturnValue(function() {
  178. if (goog.isDef(goog.global['WebKitCSSMatrix'])) {
  179. return goog.global['WebKitCSSMatrix'];
  180. }
  181. if (goog.isDef(goog.global['MSCSSMatrix'])) {
  182. return goog.global['MSCSSMatrix'];
  183. }
  184. if (goog.isDef(goog.global['CSSMatrix'])) {
  185. return goog.global['CSSMatrix'];
  186. }
  187. return null;
  188. });