CSS matrix3d() Function – How to Create a 3D Transformation Matrix
The CSS matrix3d() function is a shorthand for defining 3D transformations.
In other words, instead of writing:
You can alternatively use the matrix3d()
function to shorten your code like so:
The CSS matrix3d()
Function’s Syntax
The matrix()
function accepts 16 values. Here’s the syntax:
You can represent the CSS matrix’s values as homogeneous coordinates on ℝℙ3 like so:
Tools for Converting Transform Functions to matrix3d()
The two tools you can use to do a quick conversion of transform functions to matrix3d()
are:
- JavaScript’s
window.getComputedStyle()
method - Eric Meyer and Aaron Gustafson’s matrix resolution tool
How to use window.getComputedStyle()
to convert transform functions to matrix3d()
Suppose you want to convert the following transform functions to matrix:
You will add an id
attribute to the image element.
Then, in JavaScript, you will:
- Use the
id
attribute to get the image element. - Use the
window.getComputedStyle()
method to get the image’stransform
property’s value.
Here’s the code:
Browsers, by default, convert a CSS transform
property’s value to its matrix equivalent. So, the snippet above returned the image’s computed value.
Let’s now discuss the second conversion tool.
How to use the matrix resolutions tool to convert transform functions to matrix3d()
Suppose you want to convert the following transform functions to matrix:
You will do the following:
- Go to The Matrix Resolutions website: https://meyerweb.com/eric/tools/matrix/.
- Paste your transform functions into the first text field.
- Click “The Red Pill” button to generate the transform functions’ matrix equivalence.