Please note in advance that the scope of this article is still limited to 2D transform.
I really love the transform attribute of CSS3. With this feature, various special effects can be easily achieved. To quote a lyric, "Spinning, jumping, I won't stop." Transform is so fashionable and willful. Of course, his willfulness is not only in terms of function, but also in terms of use. Let’s take a look at the introduction to the use of transform 2D on the W3C official website.
There are a lot of them, so let’s classify them first: one matrix, three translate, three scale, three skew, and one rotate. The three translates are divided into three types: XY, X, and Y. I drew a picture to roughly show the relationship between these operations.
Seeing this, some people may wonder why matrix is at the top of all other operations. In fact, other operations except matrix are an extension of matrix. What does it mean? ? The operations of translate, scale, skew and rotate are all used by people who do not understand the matrix principle. I am afraid that you do not understand the principle, so I have encapsulated several methods to allow you to use the matrix method conveniently. How to prove it? Interested children can check the style of the element (note: not the class rule, but the calculated style), or output the transform attribute of the element. See if you get uniform matrix results, similar to:
matrix(1, 0.466307658154999, 0, 1, 0, 0)
No more verbosity, since this is an operation, it all ends up matrix, so what is the relationship between them? If I use the knowledge of matrices to tell you how to calculate, it will not fit the title of this article, and there are already many such articles, so I will not show my shame and stick to the principle of the title: simple and crude explanation.
First let’s talk about translate, which means “transfer”. First, explain the syntax of translate:
transform:translate(25px,26px)
transform: matrix(1, 0, 0, 1, 25, 26);
Then there is scale. I am used to translating it into "zoom", so the translation also corresponds to its function: scaling component. Demonstrate the syntax:
transform:scale(1.1,1.2);
transform: matrix(1.1, 0, 0, 1.2, 0, 0);
Let’s take a look at skew again. I translate it as “tilt”. The function of skew is to tilt the component at a certain angle. It can be used to draw parallelograms and resemble the “inertial motion” we have seen before. " is achieved through this. It is more reliable to use a single Skew to demonstrate
transform: skewX(45deg);transform: skewY(45deg);
transform: matrix(1, 0, 1, 1, 0, 0); transform: matrix(1, 1, 0, 1, 0, 0);
At this point, we understand what the six values of the matrix represent:
The first element: X-axis magnification
The second Element: The tan value of the Y-axis tilt angle
The third element: The tan value of the X-axis tilt angle
The fourth element: Remarks for Y-axis amplification
Five elements: the pixel size shifted to the right
The sixth element: the pixel size shifted downward.
It’s not over yet. There is another ratio that has not been explained. Before explaining it, let’s take a look at an illustration:
These two pictures look like Is the effect almost the same? They are indeed almost the same, but the difference is that the above picture is realized through rotate, while the picture below is realized through skew, which is rotated at an angle of 45 degrees. Take a look at the two generated styles respectively:
transform: matrix(0.707106781186548, 0.707106781186548, -0.707106781186548, 0.707106781186548, 0, 0); transform: matrix(1, 0.466307658154999, 0.466307658154999, 1, 0, 0);
Well, it’s no longer simple to say it like this. It’s a bit deviated from my title. Let’s explain it simply and crudely. The above matrix is replaced by: (a,b,c,d,e,f ) six values, then calculate the relative coordinates of each pixel based on the center point, and then calculate the new coordinates. The calculation formula for the new coordinates is:
x' = a * x c * y e; y' = b * x d * y f; When rotating, the values passed in are cosθ, sinθ, sinθ and cosθ respectively, and the values passed in by skew are: cosθ/consθ, sinθ/cosθ, sinθ/cosθ, cosθ/cosθ. So this is why the image is enlarged by a factor of 2 when using skew for rotation.
In fact, under normal circumstances, it is enough to use the matrix formula using the six calculation methods mentioned above. This last part of this article is only written for the completeness of the article, but it has lost the principle of simplicity and crudeness. .