本文摘自 勾三股四 更早时期的 不老歌 博客。
首先,图形在变形(transform)之前,需要确定一个中心(origin),在SVG、CSS中,默认的中心都是坐标原点(0,0)。如果需要自定义中心,则可以使用transform-origin属性:
Name: transform-origin
Value: [ [ <percentage> | <length> | left | center | right ]
[ <percentage> | <length> | top | center | bottom ]? ] |
[ [ left | center | right ] || [ top | center | bottom ] ]
Initial: 50% 50%
Applies to: block-level and inline-level elements
Inherited: no
Percentages: refer to the size of the element's box
Media: visual
Computed value: For <length> the absolute value, otherwise a percentage然后就是平移(translate)、拉伸(scale)、倾斜(skew)和旋转(rotate)了,这些东西通通使用transform属性来控制:
Name: transform Value: none | <transform-function> [ <transform-function> ]* Initial: none Applies to: block-level and inline-level elements Inherited: no Percentages: refer to the size of the element's box Media: visual Computed value: Same as specified value.
这里面的transform-function就包含了这四种:
这四个东东可以写在同一个css属性里,比如:
<div style="transform:translate(-10px,-20px) scale(2) rotate(45deg) translate(5px,10px)"/>
它的效果相当于:
<div style="transform:translate(-10px,-20px)">
<div style="transform:scale(2)">
<div style="transform:rotate(45deg)">
<div style="transform:translate(5px,10px)">
</div>
</div>
</div>
</div>相信关注css3的童鞋对上面的这些东西并不陌生,没有关注过css3的童鞋,估计看过上面的描述,也可以很快的理解和掌握这些用法。
不过这些变换如果比较复杂,有没有更简单的写法呢?当然有了,这才是我今天要重点介绍的东西:
matrix的写法很吓人:
transform: matrix(a, b, c, d, e, f);
相信很多人看过之后就蒙了,不知道这是啥意思。我自己就是这种情况,一直知道有个matrix,但是始终没有勇气尝试。
今天看到那本SVG的书,用相对通俗易懂的方式讲解了matrix这个东西,让我突然对自己有信心了,于是把我的理解分享给大家。
首先matrix里的6个参数会帮助我们构建一个3*3的矩阵……呃,还记得你们大学学过的线性代数么?就是那个可以点乘和叉乘的东东——没错,稍后会涉及到点乘的概念,不过不清楚也别担心,我会把它转化成代数方程,很好理解。
这个矩阵长这样:
|a c e| |b d f| |0 0 1|
原来图形中的每一个点(x, y)的新位置都是经过这个矩阵计算出来的(x', y'),大概的公示是:
|x| |a c e| |x'| |y| = |b d f| . |y'| |1| |0 0 1| |1 |
这个算是matrix的基本算法,进一步剖析之后,我们会发现:
所以,matrix其实是对多重旋转、缩放、平移等变换的合计缩写。有点transform雪碧的意思哈 O(∩_∩)O~
最后附一个css 3 transform的玩具:http://www.css88.com/tool/css3Preview/Transform.html,你可以在左侧的“变形参数”区域自由的调整matrix中a~f的值,右侧的div自然会随之变换,随便改一改那些属性值看看预览效果,matrix就非常好理解了!
本文摘自 勾三股四 更早时期的 不老歌 博客。
首先,图形在变形(transform)之前,需要确定一个中心(origin),在SVG、CSS中,默认的中心都是坐标原点(0,0)。如果需要自定义中心,则可以使用transform-origin属性:
Name: transform-origin
Value: [ [ <percentage> | <length> | left | center | right ]
[ <percentage> | <length> | top | center | bottom ]? ] |
[ [ left | center | right ] || [ top | center | bottom ] ]
Initial: 50% 50%
Applies to: block-level and inline-level elements
Inherited: no
Percentages: refer to the size of the element's box
Media: visual
Computed value: For <length> the absolute value, otherwise a percentage然后就是平移(translate)、拉伸(scale)、倾斜(skew)和旋转(rotate)了,这些东西通通使用transform属性来控制:
Name: transform Value: none | <transform-function> [ <transform-function> ]* Initial: none Applies to: block-level and inline-level elements Inherited: no Percentages: refer to the size of the element's box Media: visual Computed value: Same as specified value.
这里面的transform-function就包含了这四种:
这四个东东可以写在同一个css属性里,比如:
<div style="transform:translate(-10px,-20px) scale(2) rotate(45deg) translate(5px,10px)"/>
它的效果相当于:
<div style="transform:translate(-10px,-20px)">
<div style="transform:scale(2)">
<div style="transform:rotate(45deg)">
<div style="transform:translate(5px,10px)">
</div>
</div>
</div>
</div>相信关注css3的童鞋对上面的这些东西并不陌生,没有关注过css3的童鞋,估计看过上面的描述,也可以很快的理解和掌握这些用法。
不过这些变换如果比较复杂,有没有更简单的写法呢?当然有了,这才是我今天要重点介绍的东西:
matrix的写法很吓人:
transform: matrix(a, b, c, d, e, f);
相信很多人看过之后就蒙了,不知道这是啥意思。我自己就是这种情况,一直知道有个matrix,但是始终没有勇气尝试。
今天看到那本SVG的书,用相对通俗易懂的方式讲解了matrix这个东西,让我突然对自己有信心了,于是把我的理解分享给大家。
首先matrix里的6个参数会帮助我们构建一个3*3的矩阵……呃,还记得你们大学学过的线性代数么?就是那个可以点乘和叉乘的东东——没错,稍后会涉及到点乘的概念,不过不清楚也别担心,我会把它转化成代数方程,很好理解。
这个矩阵长这样:
|a c e| |b d f| |0 0 1|
原来图形中的每一个点(x, y)的新位置都是经过这个矩阵计算出来的(x', y'),大概的公示是:
|x| |a c e| |x'| |y| = |b d f| . |y'| |1| |0 0 1| |1 |
这个算是matrix的基本算法,进一步剖析之后,我们会发现:
所以,matrix其实是对多重旋转、缩放、平移等变换的合计缩写。有点transform雪碧的意思哈 O(∩_∩)O~
最后附一个css 3 transform的玩具:http://www.css88.com/tool/css3Preview/Transform.html,你可以在左侧的“变形参数”区域自由的调整matrix中a~f的值,右侧的div自然会随之变换,随便改一改那些属性值看看预览效果,matrix就非常好理解了!