Advertisement
stuppid_bot

Draw Circle

Dec 25th, 2016
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.86 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title></title>
  6.   <style type="text/css">
  7.     #canvas {
  8.       background: #f5f5f5;
  9.     }
  10.   </style>
  11. </head>
  12. <body>
  13.   <canvas id="canvas" width="300" height="240">
  14.     You browser doesn't support canvas!
  15.   </canvas>
  16.   <script type="text/javascript">
  17.     const $=(q)=>document.querySelector(q);
  18.     const $$=(q)=>[...document.querySelectorAll(q)];
  19.     const DEG2RAD=Math.PI/180;
  20.  
  21.     class Point{
  22.       constructor(x, y){
  23.         this.x=x;
  24.         this.y=y;
  25.       }
  26.       set x(v){this._x=Math.round(v);}
  27.       set y(v){this._y=Math.round(v);}
  28.       get x(){return this._x;}
  29.       get y(){return this._y;}
  30.       eq(p){return this.x===p.x&&this.y===p.y}
  31.    }
  32.  
  33.    function rotatePoint(point, pivot, angle) {
  34.      let dx = point.x - pivot.x
  35.        , dy = point.y - pivot.y
  36.        , radians = angle * DEG2RAD
  37.        , sin = Math.sin(radians)
  38.        , cos = Math.cos(radians)
  39.        , x = pivot.x + cos * dx - sin * dy
  40.        , y = pivot.y + sin * dx + cos * dy;
  41.       return new Point(x, y);
  42.     }
  43.  
  44.     let cnv = $('#canvas')
  45.       , ctx = cnv.getContext('2d')
  46.       , imgData=ctx.getImageData(0,0,cnv.width,cnv.height);
  47.  
  48.     // Центр
  49.     let pivot=new Point(cnv.width/2,cnv.height/2);
  50.     console.log(pivot);
  51.     // Радиус
  52.     let r=100;
  53.     // Длина окружности
  54.     let C=2*Math.PI*r;
  55.     // Шаг
  56.     let step=360/C;
  57.     // Точка
  58.     let point=new Point(pivot.x,pivot.y-r);
  59.     for (let angle=0;angle<360;angle+=step){
  60.      let point2=rotatePoint(point,pivot,angle);
  61.      // Вычисляем смещение
  62.      let i=(point2.y*imgData.width+point2.x)*4;
  63.      imgData.data[i+0]=0;
  64.      imgData.data[i+1]=0;
  65.      imgData.data[i+2]=0;
  66.      imgData.data[i+3]=255;
  67.    }
  68.    ctx.putImageData(imgData,0,0);
  69.  </script>
  70. </body>
  71. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement