Continuous call on EventListener on mousedown

180 Views Asked by At

I'm working on an Etch A Sketch app for The Odin Project and I want to adjust one of the requirements where all I need to do is move the mouse on screen as a continuous stroke where each pixel touched on mouseover is filled, which is what's happening right now with my code.

What I want to change is the mouseover on my tile EventListener where it's really a continuous stroke on mousedown. I did try changing the e.target command in the setTiles function to toggle after changing the tile.addEventListener to mousedown, but it only works on each press of the left mouse button.

I'm trying to figure out a way to make this continuous on mousedown instead of using mouseover. I've included the code I have so far in the question.

const container = document.querySelector('#container');
const grid = document.querySelector('#grid');
const userInput = document.querySelector('#user-input');
let penDown = false;

grid.style.fontSize = '1em';
// Set pixel width and height
let wdt = '1em';
let hgt = '1em';

// Ask the user for the number of tiles for the sketch grid
function getUserInput() {
  let input = parseInt(prompt(`Please enter the grid size you'd like`));
  input <= 100 || !isNaN(input)
    ? createSketchGrid(input)
    : alert('Please enter a valid number less than or equal to 100.');
}

// Event listener to create tiles in mouseover
function setTiles(e) {
  e.target.classList.add('fill');
}

// function deleteTiles(e) {
//   e.target.classList.toggle('fill');
// }

container.addEventListener('mousedown', function () {
  penDown = true;
});

container.addEventListener('mouseup', function () {
  penDown = false;
});

// Create the grid
function createSketchGrid(tiles) {
  let gridSize = tiles * tiles;
  for (let i = 0; i < gridSize; i++) {
    let tile = document.createElement('div');
    tile.style.width = wdt;
    tile.style.height = hgt;
    grid.appendChild(tile);
    // tile.addEventListener('mouseover', setTiles, false);
    if ((penDown === true)) {
      tile.addEventListener('mousemove', setTiles);
    }
  }
}

userInput.addEventListener('click', getUserInput);
* {
  box-sizing: border-box;
  margin: 0; 
  padding: 0;
}

html {
  font-size: 10px;
}

h1 {
  font-size: 3rem;
}

.grid {
  /* font-size: 1.6rem; */
  display: flex;
  flex-wrap: wrap;
  gap: 0.1em;
  border: 2px solid black;
  background-color: lightgrey;
  flex: 0 0 32em;
}

.fill {
  flex-wrap: wrap;
  background-color: black;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Etch A Sketch</title>
  <link rel="stylesheet" type="text/css" href="styles/style.css">
  <script type="text/javascript" src="js/main.js" defer></script>
</head>
<body>
  <h1 class="title">Etch A Sketch</h1>
  <div id="container" class="container">
    <button id="user-input" class="btn user-input" value="Grid Size">Grid Size</button>
    <div id="grid" class="grid">

    </div>
  </div>
</body>
</html>

0

There are 0 best solutions below