Guest User

Untitled

a guest
Feb 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>GRID!</title>
  5. <script src="jquery.min.js"></script>
  6. <style>
  7. body {
  8. }
  9.  
  10. form input {
  11. width: 23px;
  12. float: right;
  13. }
  14.  
  15. form label {
  16. height: 25px;
  17. clear: right;
  18. position: relative;
  19. width: 150px;
  20. display: block;
  21. }
  22.  
  23. #grid {
  24. border: 1px solid black;
  25. position: relative;
  26. }
  27.  
  28. #grid:before {
  29. content: "";
  30. border-top: 1px solid rgba(0, 0, 0, 0.2);
  31. height: 1px;
  32. width: 100%;
  33. left: 0;
  34. top: 50%;
  35. position: absolute;
  36. }
  37.  
  38. #grid:after {
  39. content: "";
  40. border-left: 1px solid rgba(0, 0, 0, 0.2);
  41. width: 1px;
  42. height: 100%;
  43. top: 0;
  44. left: 50%;
  45. position: absolute;
  46. }
  47.  
  48. .pixel {
  49. float: left;
  50. background: white;
  51. border: 1px solid #eee;
  52. border-left: 0;
  53. border-top: 0;
  54. z-index: 1;
  55. }
  56.  
  57. .origin-x {
  58. border-right: 1px solid black;
  59. z-index: 10;
  60. }
  61.  
  62. .origin-y {
  63. border-bottom: 1px solid black;
  64. z-index: 10;
  65. }
  66. </style>
  67. </head>
  68. <body>
  69. <div id="grid">
  70. </div>
  71. <script>
  72. // how many blocks in the x and y directions... so 10 is -10,10 / -10,10 (121 blocks)
  73. var gridSize = 25;
  74.  
  75. // how big is each "pixel" - they are square, so this is width and height
  76. var pixelSize = 10;
  77.  
  78. $(function () {
  79. // draw the grid
  80. var x,y;
  81. for (y = gridSize; y >= -gridSize; y--) {
  82. for (x = -gridSize; x <= gridSize; x++) {
  83. var id = pixelId(x, y)
  84. $("<div class='pixel' id='" + id + "'>").appendTo("#grid");
  85. }
  86. }
  87.  
  88. var gridPx = ((gridSize * 2) + 1) * (pixelSize + 1);
  89. $("#grid").css({"width": gridPx + "px", "height": gridPx + "px"});
  90. $(".pixel").css({"width": pixelSize + "px", "height": pixelSize + "px"});
  91.  
  92. function pixelId(x, y, append) {
  93. if (append === undefined) { append = ""; }
  94. return append + "pixel_" + x + "_" + y;
  95. }
  96.  
  97. function setPixel(x, y, color) {
  98. $(pixelId(x, y, "#")).css("backgroundColor", color);
  99. }
  100.  
  101. function getPixel(x, y) {
  102. return $(pixelId(x, y, "#")).css("backgroundColor");
  103. }
  104.  
  105. $(".pixel").click(function () {
  106. var id = $(this).attr("id");
  107. var xy = id.split("_");
  108. var color = getPixel(xy[1], xy[2]);
  109. setPixel(xy[1], xy[2], (getPixel(xy[1], xy[2]) == "rgb(255, 255, 255)") ? "black" : "white")
  110. });
  111. });
  112. </script>
  113. </body>
  114. </html>
Add Comment
Please, Sign In to add comment