<!DOCTYPE html>
<html>
<!-- ahhh -->

<body style="margin:0;">
  <canvas id="myCanvas" width="10" height="10"
    style="display: block;box-sizing: border-box;">
    Your browser does not support the HTML5 canvas tag.</canvas>
  <script>
    let c = document.getElementById("myCanvas");
    let ctx = c.getContext("2d");
    ctx.canvas.width = window.innerWidth;
    ctx.canvas.height = window.innerHeight;

    let deg_per_sec = 30;
    let time = 120;
    let fps = 60;

    centerX = ctx.canvas.width / 2;
    centerY = ctx.canvas.height / 2;

    rotation = 0; //was = j = angle
    currentFrame = 0; //was = i

    
    function render() {
      if (currentFrame < time / (1 / fps)) {
        setTimeout(() => {
          render();
          render_clear();

          DrawSquareTwist_angle(400,0,rotation,"red")
         
        }, 1000 / fps);
        rotation += deg_per_sec / fps; // was = j = innerRotation, now = rotation
        currentFrame += 1; // was = i
      }
    }

    render();



function DrawSquareTwist_angle(width, rotation,innerRotation, colour){
    let out_angle = innerRotation;
    let widthMultiplier = 1 / (2 * Math.sin(Math.PI / 180 * (130 - out_angle + 90 * Math.floor(out_angle / 90))))+0.5

    for (let i = 0; i < 25; i++) {
      drawSquare(innerRotation*i,width*widthMultiplier**i, colour)
    } 

  }

  function drawSquare(angle,size, colour) {
    ctx.save();
    ctx.translate(centerX, centerY)//-(Math.sin(rad(angle)) *centerX));
    ctx.rotate(rad(angle + 180));
    ctx.beginPath();
    ctx.strokeStyle = colour;
    ctx.rect(-size/2, -size/2, size, size);
    ctx.stroke();
    ctx.restore();
    }

    function rad(degrees) {
      var pi = Math.PI;
      return degrees * (pi / 180);
    }



    function render_clear() {
      ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
      ctx.fillStyle = "black";
      ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    }
    
  </script>
</body>

</html>