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