I have been playing around with the Web Animations API and have found it to quite intutive to get going.
From my point of view, I want to make a pretty simple animation, where a element is rotated in one direction and after a button press is rotated in the other direction. When using the standard reverse method, the animation stops because it runs out time.
I have tried using the setKeyframes Method to change the transform direction, which doesnt seem to be working in Chrome. Inside my Firefox Developer Edition its working just fine...
Can someone look over my approach and tell me if I am missing something? Please bear in mind, that im pretty new to JS in general.
Thanks in advance,
Niklas
//This is what my JS Code looks like
//I have commented out the parts that throw a error inside Chrome
var fan = document.getElementById('fan');
var cw = new KeyframeEffect(
fan, [{
transform: "rotate(0)"
},
{
transform: "rotate(360deg)",
}
], {
// timing options
duration: 8000,
iterations: Infinity,
});
//creates new Animation object and starts it in clockwise rotation
var fananim = new Animation(cw, document.timeline);
//Stops Animation so it doesnt run automatically
fananim.pause();
var clockwise = function() {
/* cw.setKeyframes(
[{
transform: "rotate(0)"
},
{
transform: "rotate(360deg)",
}
]
); */
fananim.play();
}
var counterclockwise = function() {
/* cw.setKeyframes(
[{
transform: "rotate(0)"
},
{
transform: "rotate(-360deg)",
}
]
); */
fananim.play();
//this leads to the time running out and the animation stopping
//fananim.reverse();
}
var stop = function() {
fananim.pause();
}
// Click Handlers for Buttons on Control Baord
var ela = document.getElementById("cw");
ela.addEventListener("click", clockwise, false);
var elb = document.getElementById("ccw");
elb.addEventListener("click", counterclockwise, false);
var elc = document.getElementById("stop");
elc.addEventListener("click", stop, false);