Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a cross-browser mixin for transform: rotate

Tags:

I want to create a mixin for sass that will apply a rotation to the specified element. The mixin should take one parameter, for the number of degrees of rotation to apply.

From css3please.com, I found a cross-browser way to implement this with CSS:

.box_rotate {
     -moz-transform: rotate(7.5deg);  /* FF3.5+ */
       -o-transform: rotate(7.5deg);  /* Opera 10.5 */
  -webkit-transform: rotate(7.5deg);  /* Saf3.1+, Chrome */
      -ms-transform: rotate(7.5deg);  /* IE9 */
          transform: rotate(7.5deg);  
             filter: progid:DXImageTransform.Microsoft.Matrix(/* IE6–IE9  */
                     M11=0.9914448613738104, M12=-0.13052619222005157,M21=0.13052619222005157, M22=0.9914448613738104, sizingMethod='auto expand');
               zoom: 1;
}

This is all very easy to make a mixin for, except for the pesky IE matrix notation. Does anyone have any suggestions for a way to transform the degrees into the IE matrix transformation using sass, javascript, or a combo of both?

like image 688
Adam Avatar asked Apr 14 '11 20:04

Adam


2 Answers

There you go:

SASS:

@mixin rotate( $degrees )
  -webkit-transform: rotate(#{$degrees}deg)
  -moz-transform: rotate(#{$degrees}deg)
  -ms-transform: rotate(#{$degrees}deg)
  -o-transform: rotate(#{$degrees}deg)
  transform: rotate(#{$degrees}deg)

  filter:  progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11=#{cos($degrees)}, M12=-#{sin($degrees)}, M21=#{sin($degrees)}, M22=#{cos($degrees)})
  -ms-filter: "progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11=#{cos($degrees)}, M12=-#{sin($degrees)}, M21=#{sin($degrees)}, M22=#{cos($degrees)})"
  zoom: 1

SCSS:

@mixin rotate( $degrees ) {
  -webkit-transform: rotate(#{$degrees}deg);
  -moz-transform: rotate(#{$degrees}deg);
  -ms-transform: rotate(#{$degrees}deg);
  -o-transform: rotate(#{$degrees}deg);
  transform: rotate(#{$degrees}deg);

  filter:  progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11=#{cos($degrees)}, M12=-#{sin($degrees)}, M21=#{sin($degrees)}, M22=#{cos($degrees)});
  -ms-filter: "progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11=#{cos($degrees)}, M12=-#{sin($degrees)}, M21=#{sin($degrees)}, M22=#{cos($degrees)})";
  zoom: 1;
 }

usage:

@include rotate( 24 )

or you could simply use compass and make your life a lot easier :P

like image 140
meo Avatar answered Oct 07 '22 15:10

meo


The rotation matrix is defined as

[[cos(A), -sin(A)],
 [sin(A),  cos(A)]]

where A is the angle. M11 in the IE matrix is the first element of the first row; M12: the second element of the first row etc.

JavaScripts Math.sin and Math.cos operate on radians so you will have to turn your degrees into radians

radians = degrees * Math.PI / 180

Putting this together we get this function:

function rotationMatrix(degrees) {
  var A = degrees * Math.PI / 180;
  return [[Math.cos(A), -Math.sin(A)], [Math.sin(A),  Math.cos(A)]]
}

Example:

rotationMatrix(10) 
// => [[0.984807753012208, -0.17364817766693033], 
//     [0.17364817766693033, 0.984807753012208]]
like image 20
adamse Avatar answered Oct 07 '22 15:10

adamse