Advertisement
Guest User

funny fractals animation

a guest
Apr 26th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.23 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Fraktale</title>
  5. </head>
  6. <body>
  7.     <style type="text/css">
  8.         html, body {
  9.             margin: 0;
  10.             padding: 0;
  11.             overflow: hidden;
  12.         }
  13.  
  14.         canvas {
  15.             background-color: black;
  16.         }
  17.     </style>
  18.  
  19.     <div id="wrapper"></div>
  20.  
  21.     <script>
  22.         Math.Tau = Math.PI*2;
  23.         Math.RadiantInDegree = Math.Tau/360;
  24.  
  25.         function rotate(x, y, pX, pY, theta) {
  26.             var rad = theta*Math.RadiantInDegree;
  27.  
  28.             return [
  29.                 x+(pX*Math.cos(rad)+pY*Math.sin(rad)),
  30.                 y+(pX*-Math.sin(rad)+pY*Math.cos(rad))
  31.             ];
  32.         }
  33.  
  34.         String.prototype.replaceAt=function(index, replacement) {
  35.             return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
  36.         }
  37.  
  38.         var wrapper = document.getElementById('wrapper');
  39.         var canvas = document.createElement('canvas');
  40.  
  41.         canvas.width = screen.availWidth;
  42.         canvas.height = screen.availHeight;
  43.  
  44.         wrapper.appendChild(canvas);
  45.  
  46.         var ctx = canvas.getContext('2d');
  47.  
  48.         // settings
  49.         var length = 20;
  50.         ctx.strokeStyle = "white";
  51.         ctx.lineWidth = .5;
  52.  
  53.         // draw vars
  54.         var params = "R";
  55.         var oX = -100, oY = 100;
  56.         var x = canvas.width*.5-oX, y = canvas.height*.5-oY;
  57.         var theta = 90;
  58.  
  59.         // iterations
  60.         var it = 15;
  61.         for(var i = 2; i < it; i++) {
  62.             var before = params+"";
  63.             params += "R";
  64.  
  65.             var len = before.length;
  66.             var m = Math.floor(len*.5);
  67.             before = before.replaceAt(m, "L");
  68.  
  69.             params = params.concat(before);
  70.             console.log(params);
  71.         }
  72.  
  73.         var alpha = 1;
  74.         function loop() {
  75.             requestAnimationFrame(loop);
  76.  
  77.             oX = -100, oY = 100;
  78.             x = canvas.width*.5-oX, y = canvas.height*.5-oY;
  79.             theta = 90;
  80.  
  81.             ctx.clearRect(0,0, canvas.width, canvas.height);
  82.  
  83.             ctx.beginPath();
  84.             ctx.moveTo(x, y);
  85.             var vec = rotate(x, y, length, 0, theta), vX = vec[0], vY = vec[1];
  86.             ctx.lineTo(vX, vY);
  87.             x = vX; y = vY;
  88.  
  89.             for(var i = 0; i < params.length; i++) {
  90.                 if(params[i] == "R") {
  91.                     theta = (360+theta-alpha)%360;
  92.                 } else {
  93.                     theta = (360+theta+alpha)%360;
  94.                 }
  95.  
  96.                 vec = rotate(x, y, length, 0, theta), vX = vec[0], vY = vec[1];
  97.                 ctx.lineTo(vX, vY);
  98.                 x = vX; y = vY;
  99.             }
  100.  
  101.             ctx.stroke();
  102.  
  103.             alpha++;
  104.             console.log(alpha);
  105.         }
  106.  
  107.         requestAnimationFrame(loop);
  108.     </script>
  109. </body>
  110. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement