回転アニメーション requestAnimationFrame()

<div class="character">
  <svg xmlns="http://www.w3.org/2000/svg">
  <rect   width="100" height="100" fill="aqua"  />
  </svg>
</div>
const chara = document.querySelector('.character');

let degree = 0;
let requestID;
let countFrame = 0;

loop();

function loop() {
  const rotation = (degree * Math.PI) / 180;
  const targetX = window.innerWidth / 2 + 100 * Math.cos(rotation) - 50;
  const targetY = window.innerHeight / 2 + 100 * Math.sin(rotation) - 50;

  chara.style.left = `${targetX}px`;
  chara.style.top = `${targetY}px`;

  degree += 1;
  requestID = requestAnimationFrame(loop);
  countFrame++;
  console.log(countFrame);

  if (countFrame > 200) {
    // キャンセル処理
    cancelAnimationFrame(requestID);
  }
}

requestAnimationFrame()メソッドは再描画の前に関数の呼び出しを要求する命令
一般的なディスプレイでは1秒間に60回画面が更新される
requestAnimationFrame()メソッドは一度しか呼び出されないので、関数の中で自身を呼び出すことが必要setTimeout()メソッドと一緒