transform-to-matrix
Version:
A tiny library to get 2/3D matricies from CSS3 transform functions. Fully covered by unit tests, with support for AMD, CommonJS, Node, and browser globals.
110 lines (81 loc) • 1.55 kB
text/coffeescript
transformtomatrix = do ->
fns =
perspective: (d) ->
[
[]
[]
[]
[]
]
rotate: (a) -> fns.rotateZ a
rotateX: (a) -> fns.rotate3d 1, 0, 0, a
rotateY: (a) -> fns.rotate3d 0, 1, 0, a
rotateZ: (a) ->
c = Math.cos a
n = Math.sin a
[
[]
[]
]
rotate3d: (x, y, z, a) ->
s = x*x + y*y + z*z
c = Math.cos a
n = Math.sin a
i = 1 - c
rs = Math.sqrt(s)*n
[
[(x*x + (y*y + z*z)*c)/s, (x*y*i - z*rs)/s, (x*z*i + y*rs)/s, 0]
[(x*y*i + z*rs)/s, (y*y + (x*x + z*z)*c)/s, (y*z*i - x*rs)/s, 0]
[(x*z*i - y*rs)/s, (y*z*i + x*rs)/s, (z*z + (x*x + y*y)*c)/s, 0]
[]
]
scale: (x, y) ->
[
[]
[]
]
scaleX: (x) -> fns.scale x, 1
scaleY: (y) -> fns.scale 1, y
scaleZ: (z) -> fns.scale3d 1, 1, z
scale3d: (x, y, z) ->
[
[]
[]
[]
[]
]
skew: (x, y) ->
[
[]
[]
]
skewX: (x) ->
[
[]
[]
]
skewY: (y) ->
[
[]
[]
]
translate: (x, y) ->
[
[],
[]
]
translateX: (x) -> fns.translate x, 0
translateY: (y) -> fns.translate 0, y
translateZ: (z) -> fns.translate3d 0, 0, z
translate3d: (x, y, z) ->
[
[],
[],
[],
[]
]