Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.47 KB | None | 0 0
  1. /* Author:
  2.  
  3. */
  4.  
  5. $.fn.center = function () {
  6. this.css("position","absolute");
  7. this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
  8. this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
  9. };
  10.  
  11. var getViewport = (function(){
  12.  
  13. var viewportwidth;
  14. var viewportheight;
  15.  
  16. // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
  17.  
  18. if (typeof window.innerWidth != 'undefined')
  19. {
  20. viewportwidth = window.innerWidth,
  21. viewportheight = window.innerHeight
  22. }
  23.  
  24. // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
  25.  
  26. else if (typeof document.documentElement != 'undefined'
  27. && typeof document.documentElement.clientWidth !=
  28. 'undefined' && document.documentElement.clientWidth != 0)
  29. {
  30. viewportwidth = document.documentElement.clientWidth,
  31. viewportheight = document.documentElement.clientHeight
  32. }
  33.  
  34. // older versions of IE
  35.  
  36. else
  37. {
  38. viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
  39. viewportheight = document.getElementsByTagName('body')[0].clientHeight
  40. }
  41. jQuery('body').css({ 'min-height' : viewportheight-25 });
  42. return { 'width' : viewportwidth-25, 'height' : viewportheight-25 };
  43. });
  44.  
  45. var jChess = (function() {
  46. jChess.player();
  47. });
  48.  
  49. jChess.currentColor = (function(lightSquares, darkSquares, currentColor) {
  50. if (!currentColor) { var currentColor = lightSquares } else {
  51. (currentColor === lightSquares) ? currentColor = darkSquares : currentColor = lightSquares;
  52. }
  53. return currentColor;
  54. });
  55.  
  56. jChess.board = (function(elementSize, lightSquares, darkSquares, borderColor) {
  57. this.column = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
  58. this.row = ['1', '2', '3', '4', '5', '6', '7', '8'];
  59. jQuery('body').append('<div id="jChessBoard"></div>').css({'overflow-y' : 'hidden'});
  60. jQuery('#jChessBoard').css({'width' : elementSize, 'height' : elementSize, 'background-color' : borderColor, 'border-radius' : '15px' }).center();
  61. var squareSize = elementSize / 8;
  62. elementSize -= 3;
  63. squareSize = Math.floor(squareSize-5);
  64. var currentColor = lightSquares;
  65. var squares = [new Array(), new Array(), new Array(), new Array(), new Array(), new Array(), new Array(), new Array()];
  66. var board = new jChess.drawCanvas('#jChessBoard', 'jChessBoardCanvas', elementSize);
  67. if (this.playerColor === "White") {
  68. var c=0; clen=this.column.length; while (c<clen) {
  69. var posY = (elementSize)-((c+1)*squareSize)-20;
  70. currentColor = jChess.currentColor(lightSquares, darkSquares, currentColor);
  71. var r=0; rlen=this.row.length; while (r<rlen) {
  72. posX = (r*squareSize)+20;
  73. squares[c][r] = new jChess.square(this.column[r], this.row[c], board, elementSize, squareSize, posX, posY, lightSquares, darkSquares, currentColor, borderColor);
  74. currentColor = jChess.currentColor(lightSquares, darkSquares, currentColor);
  75. r++;
  76. };
  77. board.draw.font = "bold 10px sans-serif";
  78. board.draw.fillStyle = "#fff";
  79. board.draw.fillText(this.row[c], 10, posY+(squareSize/2)+6);
  80. board.draw.fillText(this.row[c], elementSize-15, posY+(squareSize/2)+6);
  81. board.draw.fillText(this.column[7-c], (posX-(c*squareSize)+squareSize/2), elementSize-6);
  82. board.draw.fillText(this.column[7-c], (posX-(c*squareSize)+squareSize/2)-5, 15);
  83. c++;
  84. };
  85. } else if (this.playerColor === "Black") {
  86. var c=0; clen=this.column.length; while (c<clen) {
  87. var posY = (c*squareSize)+20;
  88. currentColor = jChess.currentColor(lightSquares, darkSquares, currentColor);
  89. var r=0; rlen=this.row.length; while (r<rlen) {
  90. posX = (elementSize)-((r+1)*squareSize)-20;
  91. squares[c][r] = new jChess.square(this.column[r], this.row[c], board, elementSize, squareSize, posX, posY, lightSquares, darkSquares, currentColor, borderColor);
  92. currentColor = jChess.currentColor(lightSquares, darkSquares, currentColor);
  93. r++;
  94. };
  95. board.draw.font = "bold 10px sans-serif";
  96. board.draw.fillStyle = "#fff";
  97. board.draw.fillText(this.row[c], 10, posY+(squareSize/2)+6);
  98. board.draw.fillText(this.row[c], elementSize-15, posY+(squareSize/2)+6);
  99. board.draw.fillText(this.column[c], (posX+(c*squareSize)+squareSize/2)-5, elementSize-6);
  100. board.draw.fillText(this.column[c], (posX+(c*squareSize)+squareSize/2)-5, 15);
  101. c++;
  102. };
  103. } else {
  104. alert('Player color not set!');
  105. };
  106. return;
  107. });
  108.  
  109. jChess.player = (function() {
  110. this.playerColor = "Black";
  111. jChess.sizeBoard(this.playercolor);
  112. });
  113. jChess.square = (function(squareColumn, squareRow, boardCanvas, elementSize, squareSize, posX, posY, lightSquares, darkSquares, currentColor, borderColor){
  114. this.ID = squareColumn+squareRow;
  115. this.posX = posX;
  116. this.posY = posY;
  117. var board = boardCanvas;
  118. // var square = new jChess.drawCanvas('#jChessBoard', this.ID, squareSize, posX, posY);
  119. // jQuery('#'+this.ID).css({'background-color' : currentColor, 'border' : '1px solid '+borderColor, 'position' : 'absolute', 'left' : this.posX, 'top' : this.posY});
  120. board.draw.fillStyle = currentColor;
  121. board.draw.fillRect(posX, posY, squareSize, squareSize);
  122. board.draw.fillStyle = borderColor;
  123. board.draw.strokeRect(posX, posY, squareSize, squareSize);
  124. currentColor = jChess.currentColor(lightSquares, darkSquares, currentColor);
  125. });
  126.  
  127. jChess.drawCanvas = (function(targetElementID, createdElementID, elementSize, posX, posY){
  128. this.targetElementID = targetElementID;
  129. this.createdElementID = createdElementID;
  130. this.elementSize = elementSize;
  131. this.element = document.createElement('canvas');
  132. this.draw = this.element.getContext('2d');
  133. jQuery(this.element)
  134. .appendTo(this.targetElementID)
  135. .attr({'id' : this.createdElementID, 'width' : this.elementSize, 'height' : this.elementSize})
  136. .text('Browser doesn\'t support canvas.');
  137. });
  138.  
  139. jChess.sizeBoard = (function(){
  140. var lightSquares = "#eee";
  141. var darkSquares = "#999";
  142. var borderColor = "#333";
  143. var vp = getViewport();
  144. (vp.width >= vp.height) ? elementSize = vp.height : elementSize = vp.width;
  145. jQuery('#jChessBoard').remove();
  146. jChess.board(elementSize, lightSquares, darkSquares, borderColor);
  147. });
  148.  
  149. $(document).ready(function() {
  150. var game = new jChess();
  151. });
  152.  
  153. var doit;
  154. $(window).resize(function() {
  155. clearTimeout(doit);
  156. doit = setTimeout(function(){jChess.sizeBoard();},200);
  157. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement