I have created a compositing layer for a div with the following styles:
div {
position: absolute;
height: 50px;
width: 50px;
background: #900;
top: 100px;
left: 200px;
will-change: transform;
transform: translateZ(0);
}
I then apply an animation to it using the Web Animation API:
document.getElementById("box").animate(
[
{ transform: 'rotate(0) translate3D(-50%, -50%, 0)' },
{ transform: 'rotate(360deg) translate3D(-50%, -50%, 0)' }
], {
duration: 500,
iterations: Infinity
}
);
From my understanding, this animation is now being handled by the GPU and this layer is independent from other layers so the GPU can act upon on it in isolation without having to worry about calculating the geometry of other parts of the page.
What I don't understand is if I invoke a CPU intensive function, that animation will halt entirely while the function is running, and will pick up again when the function exits:
function mySlowFunction(baseNumber) {
console.time('mySlowFunction');
var result = 0;
for (var i = Math.pow(baseNumber, 10); i >= 0; i--) {
result += Math.atan(i) * Math.tan(i);
};
console.timeEnd('mySlowFunction');
return result;
}
setTimeout(() => mySlowFunction(5), 3000);
Is there any way to prevent this?