How to Retrieve the Angle in CSS3 Rotate

css transform. How to get rotation degree value from matrix( 0, 1, -1, 0, 0, 0)?

Without being a mathematician, you can parse the rotation angle from the style.

var style = $(el).attr('style'),
rotation = parseInt(style.substr(style.indexOf("rotate("+7));

Note: This works because jQuery modifies the style attribute. If you set the rotation in CSS initially, it won't work.

How to get the position of element transformed with css rotate

Per your current Question and your requested confirmation of:

var x = termin.top + Math.cos(angle) * div.height;
var y = div.left + Math.sin(angle) * div.height;

The solution can be found in this other SO Answer for a different question, enhanced here:

// return an object with full width/height (including borders), top/bottom coordinates
var getPositionData = function(el) {
return $.extend({
width: el.outerWidth(false),
height: el.outerHeight(false)
}, el.offset());
};

// get rotated dimensions
var transformedDimensions = function(el, angle) {
var dimensions = getPositionData(el);
return {
width: dimensions.width + Math.ceil(dimensions.width * Math.cos(angle)),
height: dimensions.height + Math.ceil(dimensions.height * Math.cos(angle))
};
};



Here's an interactive jsFiddle that provides real-time updates for getPositionData(); function.
You'll be able to see the top and left values at the end of the CSS3 Rotation process you control.

Reference:   jsFiddle

Status Update: The above jsFiddle works great for 0-90deg and can be approved upon for all angles and different units such as rad, grad, and turn.

CSS3 animation on transform: rotate. Way to fetch current deg of the rotating element?

I recently had to write a function that does exactly what you want! Feel free to use it:

// Parameter element should be a DOM Element object.
// Returns the rotation of the element in degrees.
function getRotationDegrees(element) {
// get the computed style object for the element
var style = window.getComputedStyle(element);
// this string will be in the form 'matrix(a, b, c, d, tx, ty)'
var transformString = style['-webkit-transform']
|| style['-moz-transform']
|| style['transform'] ;
if (!transformString || transformString == 'none')
return 0;
var splits = transformString.split(',');
// parse the string to get a and b
var parenLoc = splits[0].indexOf('(');
var a = parseFloat(splits[0].substr(parenLoc+1));
var b = parseFloat(splits[1]);
// doing atan2 on b, a will give you the angle in radians
var rad = Math.atan2(b, a);
var deg = 180 * rad / Math.PI;
// instead of having values from -180 to 180, get 0 to 360
if (deg < 0) deg += 360;
return deg;
}

Hope this helps!

EDIT I updated the code to work with matrix3d strings, but it still only gives the 2d rotation degrees (ie. rotation around the Z axis).

How to css rotate with an angle depending on viewport size

If you forget angles but turn to using clip-path you can have a couple of pseudo elements on your element which have backgrounds one of darker and one of lighter green.

As the clip-paths are defined in terms of the percentage amounts rather than actual angles they automatically adjust to different viewports without the need for media queries:

div {
position: relative;
width: 100vw;
height: 30vw;
display: inline-block;
}

div::before,
div::after {
content: '';
position: absolute;
display: inline-block;
top: 0;
left: 0;
width: 100%;
height: 100%;
}

div::before {
z-index: -2;
background-color: lightgreen;
clip-path: polygon(0 0, 100% 0%, 0 35%, 0 100%, 100% 70%, 100% 95%, 0 100%);
}

div::after {
z-index: -1;
background-color: green;
clip-path: polygon(0 0, 100% 0, 0 25%, 0 100%, 100% 75%, 100% 100%, 0 100%);
}
<div></div>

Getting CSS transform rotateX angle from matrix3d

Figured it out myself. You have to use a = values[5] and b = values[4] for rotateX.

CSS : Change angle of rotation of needle

One solution I found is that transform: rotate(0deg); start from -180deg and keep adding my value to -180deg. Needle will move from bottom to top. For example, I want to rotate needle by 60deg. I will do: -180-60 = -240

This is work around.

Get element -moz-transform:rotate value in jQuery

Here's my solution using jQuery.

This returns a numerical value corresponding to the rotation applied to any HTML element.

function getRotationDegrees(obj) {
var matrix = obj.css("-webkit-transform") ||
obj.css("-moz-transform") ||
obj.css("-ms-transform") ||
obj.css("-o-transform") ||
obj.css("transform");
if(matrix !== 'none') {
var values = matrix.split('(')[1].split(')')[0].split(',');
var a = values[0];
var b = values[1];
var angle = Math.round(Math.atan2(b, a) * (180/Math.PI));
} else { var angle = 0; }
return (angle < 0) ? angle + 360 : angle;
}

angle1 = getRotationDegrees($('#myDiv'));
angle2 = getRotationDegrees($('.mySpan a:last-child'));

etc...

css3 rotate transition, doesn't take shortest way

The transform is doing exactly what you tell it to.

It starts at 359deg and goes to 1deg. You are looking to 'rollover' 360deg back to 1deg, which is really 361deg. The way the transform transitions work is that it interpolates between values.

The solution to your problem is to make a counter variable that holds the degrees of rotation:

var rot = 0;  // lets start at zero, you can apply whatever later

To apply a rotation, change value:

rot = 359;
// note the extra brackets to ensure the expression is evaluated before
// the string is assigned this is require in some browsers
element.style.transform = ("rotate( " + rot + "deg )");

so if you do this:

rot = 1;
element.style.transform = ("rotate( " + rot + "deg )");

it goes back. So you need to see if it is closer to 360 or 0 regardless how many rotations it has been through. You do this by checking the value of element.style.transform which is just the current rot value and then comparing to the new rot value. However, you need to do this with respect to how many rotations may exist, so:

var apparentRot = rot % 360;

Now no matter how many spins it has had, you know how far around it is, negative values are equal to the value + 360:

if ( apparentRot < 0 ) { apparentRot += 360; } 

Now you have normalized any negative values and can ask whether a positive rotation (through 360deg in your case) or negative is needed. Since you seem to be giving the new rotation value as 0-360deg, this simplifies your problem. You can ask if the new rotation + 360 is closer to the old value than the new rotation itself:

var aR,          // what the current rotation appears to be (apparentRot shortened)
nR, // the new rotation desired (newRot)
rot; // what the current rotation is and thus the 'counter'

// there are two interesting events where you have to rotate through 0/360
// the first is when the original rotation is less than 180 and the new one
// is greater than 180deg larger, then we go through the apparent 0 to 359...
if ( aR < 180 && (nR > (aR + 180)) ) {
// rotate back
rot -= 360;
}

// the second case is when the original rotation is over 180deg and the new
// rotation is less than 180deg smaller
if ( aR >= 180 && (nR <= (aR - 180)) ) {
// rotate forward
rot += 360;
}

Other than this, simply adding the value of the new rotation to rot is all that is needed:

rot += (nR - aR); //  if the apparent rotation is bigger, then the difference is
// 'negatively' added to the counter, so the counter is
// correctly kept, same for nR being larger, the difference is
// added to the counter

Clean it up a bit:

var el, rot;

function rotateThis(element, nR) {
var aR;
rot = rot || 0; // if rot undefined or 0, make 0, else rot
aR = rot % 360;
if ( aR < 0 ) { aR += 360; }
if ( aR < 180 && (nR > (aR + 180)) ) { rot -= 360; }
if ( aR >= 180 && (nR <= (aR - 180)) ) { rot += 360; }
rot += (nR - aR);
element.style.transform = ("rotate( " + rot + "deg )");
}

// this is how to intialize and apply 0
el = document.getElementById("elementYouWantToUse");
rotateThis(el, 0);

// now call function
rotateThis(el, 359);
rotateThis(el, 1);

The counter can go positive or negative, it doesn't matter, just use a value between 0-359 for the new rotation.



Related Topics



Leave a reply



Submit