Skip to content

Instantly share code, notes, and snippets.

@henrahmagix
Created September 12, 2014 14:15
Show Gist options
  • Save henrahmagix/e0ec0600cc4a3b098e96 to your computer and use it in GitHub Desktop.
Save henrahmagix/e0ec0600cc4a3b098e96 to your computer and use it in GitHub Desktop.
// Parse the value returned by $.css('-webkit-transform')
var parseTranslationValue = function (value, suffix, axis) {
if (angular.isUndefined(value) || value === '') {
// return early if value isn't passed.
return 0;
} else {
// Default suffix value.
if (angular.isUndefined(suffix)) {
suffix = '';
}
// Default axis value.
if (angular.isUndefined(axis)) {
axis = 'z';
}
// The axis must be uppercase.
axis = axis.toUpperCase();
// Find the value of the transform for axis and ending with
// suffix. A regexp was not chosen for performance in iOS.
// http://jsperf.com/regexp-vs-indexof-for-transform-values
var strings = value.split(' ');
var translationValue;
var axisPosition;
var cutStart;
var cutEnd;
// Use a for loop as it's quicker than [].forEach
var i;
var str;
for (i = 0; i < strings.length; i++) {
str = strings[i];
axisPosition = str.indexOf(axis);
if (axisPosition !== -1) {
// Cut between the opening paren and the suffix. axisPosition is
// the axis character, so we need to move forward by two to
// cover it and the opening paren. The substring end value
// doesn't include the character at that position so we don't
// need to adjust it. cutEnd is either the first character of
// the suffix or the closing paren.
cutStart = axisPosition + 2;
if (suffix.length > 0) {
cutEnd = str.indexOf(suffix.charAt(0));
}
if (suffix.length === 0 || cutEnd === -1) {
cutEnd = str.indexOf(')');
}
translationValue = str.substring(cutStart, cutEnd);
// Break out of the loop because we have the value.
break;
}
};
// Return an integer.
return parseInt(translationValue, 10);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment