Guest User

Untitled

a guest
Feb 17th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. $(document).ready(function(){
  2.  
  3. // first set the global variables
  4. const tr = $("tr");
  5. const td = $("td");
  6. const table = $("#pixelCanvas");
  7. const grid = $("#makeButton");
  8. let action = false;
  9. //default color in color picker (not black but white)
  10. document.querySelector('input[type="color"]').value = '#ffffff'
  11.  
  12. // function that creates grid with the values guven (width-height = columns-rows)
  13. function makeGrid(){
  14.  
  15. let columns = $("#inputHeight").val();
  16. let rows = $("#inputWeight").val();
  17.  
  18. //erases previous cells (td) from <table>
  19. table.children().remove();
  20.  
  21. // if : defines maximum value
  22. if((columns<=50 && columns>=1) && (rows<=50 && rows>=1)){
  23. //creates table according to input
  24. for(let a = 0; a < rows; a++){
  25. table.append("<tr></tr>");
  26. for(let b = 0; b < columns; b++){
  27. table.children().last().append("<td></td>");
  28. $("td").css('background', "#fff");
  29. }
  30. }
  31. // message when invalid input: width-height>50 or decimal
  32. } else {
  33. alert("Please choose the quantity of columns and rows of your GRID by typing numbers between 1-60 in both height and width input areas. Thanks!");
  34. }
  35. } // makeGrid ends
  36.  
  37. // call makeGrid function on click
  38. grid.on("click",
  39. function(event){
  40. event.preventDefault();
  41. makeGrid();
  42. });
  43.  
  44.  
  45. // paint canvas(3 steps)
  46.  
  47. // 1. on click
  48. table.on("mousedown", "td", function(event){
  49. event.preventDefault();
  50. let aColor = $("input[type='color']").val();
  51. $(this).css('background', aColor);
  52. action = true;
  53. });
  54.  
  55. // 2. click and drag
  56. table.on("mouseover", "td", function(event){
  57. event.preventDefault();
  58. if(action === true){
  59. let aColor = $("input[type='color']").val();
  60. $(this).css('background', aColor);
  61. console.log("mouseover");
  62. }
  63. });
  64.  
  65. // 3. stop action/painting when mousedown = false
  66. $("body").on("mouseup", function(){
  67. action = false;
  68. console.log("mouseup");
  69. });
  70.  
  71.  
  72. // when double clicked, cell's color becomes white/"erased"
  73. table.on("dblclick", "td", function(){
  74. $(this).css('background', "#fff");
  75. });
  76.  
  77. // Erase Grid Button
  78. $("#clearGrid").on("click", function(event){
  79. event.preventDefault();
  80. console.log("gio");
  81. $("td").css('background', "#fff");
  82. });
  83. //Fill Grid button, with the selected color
  84. $("#fillGrid") .on("click", function(event){
  85. event.preventDefault();
  86. let aColor = $("input[type='color']").val();
  87. $("td").css("background", aColor);
  88. })
  89.  
  90.  
  91.  
  92. });
Add Comment
Please, Sign In to add comment