Skip to content

Instantly share code, notes, and snippets.

@SpenceDiNicolantonio
Last active February 2, 2019 07:00
Show Gist options
  • Save SpenceDiNicolantonio/2fdda0420d61641bbd49cf32e37648f7 to your computer and use it in GitHub Desktop.
Save SpenceDiNicolantonio/2fdda0420d61641bbd49cf32e37648f7 to your computer and use it in GitHub Desktop.
[CSS Translation Extract] Function to extract translation values from CSS transform value #css
/**
* Extracts the translation values from a given CSS transformation.
* @param {string} transformValue The CSS transform string from which the translation values will be extracted (e.g. ‘translate3d(10px, 25px, 30px)’)
* @returns {object} translation values found in the given transform (e.g. { x: 10, y: 25, z: 30 })
*/
function getTranslation(transformValue) {
var matches = transformValue.match(/translate(3d)*\((\d+.?\d*(px)?),\s*(\d+.?\d*(px)?)(,\s*(\d+.?\d*(px)?))?\)/);
if (matches) {
return {
x: parseInt(matches[2]) || 0,
y: parseInt(matches[4]) || 0,
z: parseInt(matches[7]) || 0
};
}
return { x: 0, y: 0, z: 0 };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment