Advertisement
Guest User

Pixel Art Maker 2nd try - Juan Carlos Sanchez

a guest
Jul 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. // Select color input
  2. // Select size input
  3.  
  4. // When size is submitted by the user, call makeGrid()
  5. // Variables
  6. const submitButton = document.querySelector('input[type="submit"]');
  7. const gridHeight = document.getElementById('inputHeight').value;
  8. const gridWidth = document.getElementById('inputHeight').value;
  9. const cellColor = document.getElementById('colorPicker').value;
  10.  
  11. function makeGrid() {
  12. // Your code goes here!
  13. // Here's where we build the grid of #rows x #columns
  14.  
  15. for (var rowNumber = 1; rowNumber < gridHeight+1; rowNumber++) {
  16. let row = document.createElement('tr');
  17. row.setAttribute("id", "tr"+rowNumber);
  18. pixelCanvas.appendChild(row);
  19. for (var colNumber = 1; colNumber < gridWidth+1; colNumber++) {
  20. let column = document.createElement('td');
  21. column.setAttribute("id", "td"+colNumber);
  22. row.appendChild(column);
  23. column.addEventListener('click', cellColoring);
  24. }
  25. }
  26. }
  27.  
  28. // Add color to those grid squares "clicked" by the user
  29. function cellColoring(event) {
  30. let cellPicker = document.querySelector('tr'+rowNumber,'td'+colNumber);
  31. cellPicker.addEventListener('click', cellColoring);
  32. cellPicker.style.backgroundColor = cellColor;
  33. }
  34.  
  35. //If there are colored squares in the grid, clicking the "submit" button clears
  36. //them out
  37. submitButton.addEventListener('click', function(event){
  38. event.preventDefault();
  39. makeGrid()
  40. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement