Advertisement
SmaJli

Canvas drawing shapes around circle

Feb 20th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.61 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.   <head>
  4.     <meta charset="UTF-8" />
  5.     <title>Document</title>
  6.   </head>
  7.   <body>
  8.     <h1>Lab 6</h1>
  9.     <canvas
  10.      width="600"
  11.      height="600"
  12.      style="border: solid black 1px"
  13.      id="surface"
  14.    >
  15.     </canvas>
  16.  
  17.     <script>
  18.       var distanceFromCenter = 0;
  19.       var numberOfObjects = 0;
  20.  
  21.       function toRad(angleInDegrees) {
  22.         const angleInRadians = angleInDegrees * (Math.PI / 180);
  23.       }
  24.       function isValidNumber(number, min, max) {
  25.         if (isNaN(number) || !(number >= min && number <= max)) {
  26.          return false;
  27.         }
  28.         return true;
  29.       }
  30.  
  31.       function askForQuantity(){
  32.         const number = prompt("How many items do you want to draw [1-10]?");
  33.         if (isValidNumber(number, 1, 10)) {
  34.           return number;
  35.         }
  36.         return 0;
  37.       };
  38.       function askForRadius (){
  39.         const number = prompt("What radius would you like [1-200]");
  40.         if (isValidNumber(number, 1, 200)) {
  41.           return number;
  42.         }
  43.         return 0;
  44.       };
  45.       function askQuestions (){
  46.         do {
  47.           const quantity = askForQuantity();
  48.           if (quantity) {
  49.             numberOfObjects = quantity;
  50.           }
  51.         } while (!numberOfObjects);
  52.  
  53.         do {
  54.           const radius = askForRadius();
  55.           if (radius) {
  56.             distanceFromCenter = radius;
  57.           }
  58.         } while (!distanceFromCenter);
  59.  
  60.         console.log(distanceFromCenter, numberOfObjects);
  61.       };
  62.       function drawTheShapes(objectsNumber, radius){
  63.  
  64.         const canvas = document.getElementById("surface");
  65.         const ctx = canvas.getContext("2d");
  66.  
  67.         // move to center
  68.         ctx.translate(canvas.height / 2, canvas.width / 2);
  69.  
  70.         var num = 0;
  71.  
  72.         while(num<objectsNumber) {
  73.          var ang = (num * Math.PI) / (objectsNumber / 2);
  74.          ctx.rotate(ang);
  75.          ctx.translate(0, -radius);
  76.          ctx.rotate(-ang);
  77.          createShape();
  78.          ctx.rotate(ang);
  79.          ctx.translate(0, radius);
  80.          ctx.rotate(-ang);
  81.          
  82.          num++;
  83.        }
  84.  
  85.        function createShape() {
  86.            // you can create any shape here, now is just a simple circle
  87.          ctx.beginPath();
  88.          ctx.arc(0, 0, 40, 0, 2 * Math.PI);
  89.          ctx.stroke();
  90.          ctx.fill();
  91.        }
  92.      };
  93.  
  94.      window.onload = function() {
  95.        setTimeout(function(){
  96.          askQuestions();
  97.          drawTheShapes(numberOfObjects, distanceFromCenter);
  98.        }, 1000);
  99.      };
  100.    </script>
  101.   </body>
  102. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement