2
<section class="viewport" id="content">
    <div class="cube rotate1 " id="test">
        <div class="front" id="ajax-container"></div>
        <div class="back"></div>
        <div class="right"></div>
        <div class="left"></div>
        <div class="top"></div>
        <div class="bottom"></div>
    </div>
</section>
<button type="button" class="test">Click Me!</button>

jQuery:

$(".test").on('click',function(e){
    $(".cube").addClass('paused');
});

Demo

How can i get the current position of the cube when i pause the animation?
I need its current position(rotatex,rotatey) in jQuery to do stuff from that position.

UPDATED:

i have tried this script :

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;

}

but i get numbers under 180 degrees. It is not working properly.

1 Answers1

3

You can use an animationstart listener on the element, and from there calculate the rotations based on the time expired since the last animation start. You know animation lasts 3 seconds, so the calculation is trivial.

Using animation events

Here's your fiddle with the calculation: https://jsfiddle.net/yzrq367s/2/

And another with live state output: https://jsfiddle.net/yzrq367s/3/

Using animationiteration (wait for one cycle for it to start): https://jsfiddle.net/yzrq367s/6/


$(".test").on('click',function(e){
        $(".cube").addClass('paused');
        clearInterval(int);
        $('p').text(((new Date().getTime() - animStart.getTime()) % 3000 / 3000 * 360).toFixed(2) + " degrees");
        $('.test').hide();
  });
  
  $(".cube").get(0).addEventListener('animationstart', function(){
   animStart = new Date();
    if(!int)
     int = setInterval(function(){
     $('p').text(((new Date().getTime() - animStart.getTime()) % 3000 / 3000 * 360).toFixed(2));
    }, 10)
  })
  
var animStart;
var int;
$(".cube").addClass('running');
.menu {position: absolute; right: 20px; top: 20px; text-align: right}
.viewport {
  width: 400px;
  height: 400px;
  margin-top: 50px;
  position: relative;
  perspective-origin: 50% 50%;
  perspective: 1000px;
}

.cube {
  margin: auto;
  position: relative;
  height: 200px;
  width: 200px;
  transform-style: preserve-3d;
}

.cube div {
  position: absolute;
  padding: 10px;
  box-sizing: border-box;
  height: 100%;
  width: 100%;
  opacity: 0.9;
  background-color: #000;
  border: solid 1px #eee;
  color: #fff;
  font: 10px arial;
  transition: transform 0.2s ease-in;
}

.front {
  transform: translateZ(100px);
  overflow: hidden;
}

.back {
  transform: translateZ(-100px) rotateY(180deg);
}

.right {
  transform: rotateY(-270deg) translateX(100px);
  transform-origin: top right;
}

.left {
  transform: rotateY(270deg) translateX(-100px);
  transform-origin: center left;
}

.top {
  transform: rotateX(-270deg) translateY(-100px);
  transform-origin: top center;
}

.bottom {
  transform: rotateX(270deg) translateY(100px);
  transform-origin: bottom center;
}

@keyframes rotate1 {
  to {
    transform: rotateX(90deg) rotateY(90deg);
  }
}

@keyframes rotate2 {
  to {
    transform: rotateX(180deg) rotateY(180deg);
  }
}

@keyframes rotate3 {
  to {
    transform: rotateX(270deg) rotateY(270deg);
  }
}

@keyframes rotate4 {
  to {
    transform: rotateX(360deg) rotateY(360deg);
  }
}

.cube.running {
  animation: rotate1 3s, rotate2 3s, rotate3 3s, rotate4 3s infinite linear;
  animation-fill-mode: forwards;
}

.paused {
  animation-play-state: paused !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="viewport" id="content">
  <div class="cube" id="test">

    <div class="front" id="ajax-container"></div>
    <div class="back"></div>
    <div class="right"></div>
    <div class="left"></div>
    <div class="top"></div>
    <div class="bottom"></div>

  </div>


</section>
<div class="menu">
  <button type="button" class="test">Click Me!</button>
  <p>Waiting for the animation to start</p>
</div>
Shomz
  • 37,421
  • 4
  • 57
  • 85
  • i saw that too but my animation should contiune after some modifications and i don't know if a listener is reliable. Do you think it is a good idea? – user3771012 Nov 25 '16 at 17:37
  • I think that's the most accurate way and I see no issues with continuing the animation. How exact your calculations need to be? – Shomz Nov 25 '16 at 17:40
  • not so exact, but after i pause it i want to add another animation to the cube and after the second animation ends,i should come back to this one with rotation. – user3771012 Nov 25 '16 at 17:41
  • It should work - I've updated your fiddle, see if you can make use of it. – Shomz Nov 25 '16 at 17:46
  • i saw but look what i was talking about. after you hit ok and close the alert, the timer is still counting and i want after i do stuff to turn at that current place – user3771012 Nov 25 '16 at 18:00
  • Alert is just there to show you how to grab the value, you should never use alerts in production code... I'll write you a snippet here... – Shomz Nov 25 '16 at 18:01
  • saw the last one, i get your point but it prints only degree, nothing after – user3771012 Nov 25 '16 at 18:04
  • What else do you need? – Shomz Nov 25 '16 at 18:11
  • Oh, I thought some data is missing. You're welcome and thank you! :) – Shomz Nov 25 '16 at 18:17
  • hmm.. i have another problem. With your code, i get the degrees after the first iteration. i tried to modify "animationiteration" with "animationstart" and it is not working. Do you have any idea? – user3771012 Nov 25 '16 at 18:54
  • Yes, `animationstart` fires only once (that's why I'm using the `running` class which I dynamically add using JS), and `animationiteration` fires on every iteration (every loop start) in a repetitive animation, **not** including when it starts the first time. – Shomz Nov 25 '16 at 22:50
  • You can listen for both events, and have them point to the same callback function. – Shomz Nov 27 '16 at 07:01
  • i tried to add animationstart but it only fires after i click the button. how can i make it start on document ready? The animation starts after the page is loaded, before click event. What should i do? – user3771012 Dec 07 '16 at 16:05
  • Use a class to control when the animation starts. Take the last couple of lines from my code and put it inside a click listener handler. – Shomz Dec 07 '16 at 23:09