Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
JAVASCRIPT: change background color of a div, when hover over it while pressing the mouse click down

I am trying to change the div background color when i hover over them clicking my mouse button.This is what i did so far. I want to make it like a drawing pen, so it won't change color when hover over but it will when hover over while pressing the mouse key.

const container = document.querySelector('.container');


function canvas(size) {
    container.style.setProperty('--grid-column', size);
    container.style.setProperty('--grid-row', size);

    for (let i = 0; i < size*size; i++) {
        const cell = document.createElement('div');
        cell.addEventListener('mouseover', changeColor);
        cell.addEventListener('mousedown', changeColor);
        container.appendChild(cell).className = 'grid-items';
    }
}
canvas(16)


function changeColor(e) {
    if (e.type == 'mouseover' && !mousedown) return
    else{
        let color1 = Math.floor(Math.random() * 256);
        let color2 = Math.floor(Math.random() * 256);
        let color3 = Math.floor(Math.random() * 256);

        e.target.style.backgroundColor = `rgb(${color1},${color2},${color3})`
    }
}

live site : https://alucard2169.github.io/Etch-a-Sketch/

repo: https://github.com/Alucard2169/Etch-a-Sketch.git

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I found this to work the best

const container = document.querySelector('.container');
const button = document.createElement('button');
let size;

button.classList.add('clear');
button.innerText = 'CLEAR';
document.body.appendChild(button);

var mouseDown = false;
document.body.onmousedown = function() {
  mouseDown = true;
};
document.body.onmouseup = function() {
  mouseDown = false;
};

button.addEventListener('click', function() {
  container.innerHTML = '';
  size = prompt('what size do you want your grid to be: ');
  checkSize(size);
});

function canvas(size) {
  container.style.setProperty('--grid-column', size);
  container.style.setProperty('--grid-row', size);

  for (let i = 0; i < size * size; i++) {
    const cell = document.createElement('div');
    cell.addEventListener('mouseover', changeColor);
    cell.addEventListener('mousedown', changeColor);
    container.appendChild(cell).className = 'grid-items';
  }
}
canvas(16);

function changeColor(e) {
  if (mouseDown) {
    let color1 = Math.floor(Math.random() * 256);
    let color2 = Math.floor(Math.random() * 256);
    let color3 = Math.floor(Math.random() * 256);

    e.target.style.backgroundColor = `rgb(${color1},${color2},${color3})`;
  }
}

function checkSize(e) {
  if (e > 100) {
    alert("size can't be greater than 100");
    canvas(16);
  } else {
    canvas(e);
  }
}
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background: black;
  display: flex;
  height: 100vh;
  align-items: center;
  justify-content: center;
}

.container {
  display: grid;
  width: 25rem;
  height: 30rem;
  background: white;
  grid-template-columns: repeat(var(--grid-column), 1fr);
  grid-template-rows: repeat(var(--grid-row), 1fr)
}

.grid-items {}

.clear {
  padding: 1rem 2rem;
  border: none;
  border-radius: 10px;
  margin: 5rem 0 0 5rem;
}
<div class="container"></div>

In this if the user is pressing the mouse down over the canvas, the color changes as the mouse moves.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!