Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Hexogen{
  2.    
  3. }
  4.  
  5.  
  6.     var canvas = document.getElementById('hexagonCanvas');
  7.     var hexHeight,
  8.         hexRadius,
  9.         hexRectangleHeight,
  10.         hexRectangleWidth,
  11.         hexagonAngle = 0.523598776, //30 градусов в радианах
  12.         sideLength = 20, //длина стороны, пискелов
  13.         boardWidth = 200, //ширина "доски" по вертикали
  14.         boardHeight = 100; //высота "доски" по вертикали
  15.    
  16.     hexHeight = Math.sin(hexagonAngle) * sideLength;
  17.     hexRadius = Math.cos(hexagonAngle) * sideLength;
  18.     hexRectangleHeight = sideLength + 2 * hexHeight;
  19.     hexRectangleWidth = 2 * hexRadius;
  20.    
  21.    // if (canvas.getContext) {
  22.      var ctx = canvas.getContext('2d');
  23.      ctx.fillStyle = "#000000";
  24.      ctx.strokeStyle = "#d2d2d2";
  25.      ctx.lineWidth = 2;
  26.      
  27.      drawBoard(ctx, boardWidth, boardHeight);
  28.      /*canvas.addEventListener("mousemove", function(eventInfo) { //слушатель перемещения мыши
  29.       var x = eventInfo.offsetX || eventInfo.layerX;
  30.       var y = eventInfo.offsetY || eventInfo.layerY;
  31.       var hexY = Math.floor(y / (hexHeight + sideLength));
  32.       var hexX = Math.floor((x - (hexY % 2) * hexRadius) / hexRectangleWidth);
  33.       var screenX = hexX * hexRectangleWidth + ((hexY % 2) * hexRadius);
  34.       var screenY = hexY * (hexHeight + sideLength);
  35.       ctx.clearRect(0, 0, canvas.width, canvas.height);
  36.       drawBoard(ctx, boardWidth, boardHeight);
  37.         ctx.fillStyle = "#4dfe4d";//
  38.         drawHexagon (ctx, screenX, screenY, true);
  39.  
  40.      });*/
  41.  
  42.    
  43.     function drawBoard (canvasContext, width, height) {
  44.      for (var i = 0; i < width; i++) {
  45.       for (var j = 0; j < height; j++) {
  46.        drawHexagon (ctx, i * hexRectangleWidth + ((j % 2    ) * hexRadius),
  47.         j * (sideLength + hexHeight), false);
  48.       }
  49.      }
  50.     }
  51.    
  52.     function drawHexagon (canvasContext, x, y, fill) {          
  53.      var fill = fill || false;
  54.      if (fill) canvasContext.fill();
  55.      else canvasContext.stroke();
  56.      canvasContext.beginPath();
  57.      canvasContext.moveTo (x + hexRadius, y);
  58.      canvasContext.lineTo (x + hexRectangleWidth, y + hexHeight);
  59.      canvasContext.lineTo (x + hexRectangleWidth, y + hexHeight + sideLength);
  60.      canvasContext.lineTo (x + hexRadius, y + hexRectangleHeight);
  61.      canvasContext.lineTo (x, y + sideLength + hexHeight);
  62.      canvasContext.lineTo (x, y + hexHeight);
  63.      canvasContext.closePath();
  64.  
  65.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement