Guest User

Untitled

a guest
Dec 11th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. $(document).ready(function()
  2. {
  3. var hexSideLength = 30;
  4.  
  5. var r = Raphael("map", 1024, 768), i, j, map = [], current = {};
  6.  
  7. var centerPixel;
  8.  
  9. var Position = function(x, y)
  10. {
  11. this.x = x;
  12. this.y = y;
  13. };
  14.  
  15. var Pixel = function(x, y)
  16. {
  17. this.x = x;
  18. this.y = y;
  19. };
  20.  
  21. var Hexagon = function(x, y, sideLength)
  22. {
  23. this.position = new Position(x, y);
  24. this.sideLength = sideLength;
  25.  
  26. var hexA, hexB, hexC;
  27. };
  28.  
  29. Hexagon.prototype.draw = function()
  30. {
  31. var hexA, hexB, hexC;
  32.  
  33. this.hexC = this.sideLength;
  34. this.hexB = (Math.sqrt(3) / 2) * this.hexC;
  35. this.hexA = 0.5 * this.hexC;
  36.  
  37. hexA = this.hexA, hexB = this.hexB, hexC = this.hexC;
  38.  
  39. this.getCenterPixel();
  40.  
  41. return r.path(
  42. "M" + (this.position.x * (hexA+hexC)) + " " + (768-((this.position.y * 2+2-this.position.x) * hexB)) +
  43. "l" + hexA + " " + (-hexB) +
  44. "l" + hexC + " 0" +
  45. "l" + (hexC - hexA) + " " + hexB +
  46. "l" + (hexA - hexC) + " " + hexB +
  47. "l" + (-hexC) + " 0" +
  48. "Z");
  49. };
  50.  
  51. Hexagon.prototype.getCenterPixel = function()
  52. {
  53. this.centerPixel = new Pixel(
  54. (this.hexC+this.position.x * (this.hexA+this.hexC)),
  55. (768-((this.position.y * 2+2-this.position.x) * this.hexB))
  56. );
  57. };
  58.  
  59. var randomInt = function(min, max)
  60. {
  61. return Math.floor(Math.random() * (max - min + 1)) + min;
  62. };
  63.  
  64. var colours = [
  65. "rgb(255, 000, 000)", // red
  66. "rgb(000, 127, 000)", // green
  67. "rgb(000, 000, 255)", // blue
  68. "rgb(255, 255, 000)", // yellow
  69. "rgb(255, 127, 000)", // orange
  70. "rgb(127, 000, 255)" // purple
  71. ];
  72.  
  73. for (i = 0; i < 8; i++)
  74. {
  75. map[i] = [];
  76.  
  77. for (j = 0; j < 16; j++)
  78. {
  79.  
  80. hex = new Hexagon(i, j, hexSideLength);
  81.  
  82. current.shape = map[i][j] = hex.draw().attr({
  83. fill: colours[randomInt(0, 5)],
  84. "stroke-width": 1,
  85. "fill-opacity": 0.3
  86. }).hover(function()
  87. {
  88. this.attr({"fill-opacity": 1});
  89. }, function()
  90. {
  91. this.attr({"fill-opacity": 0.3});
  92. });
  93.  
  94. r.text(hex.centerPixel.x, hex.centerPixel.y, hex.position.x + ", " + hex.position.y);
  95. }
  96. }
  97. });
Add Comment
Please, Sign In to add comment