Trying to create a Paint feature in JS, here's the code snippet. I am looking to change the color of the trail from black to red by clicking on the "red" div, but I'm running out of ideas (without jQuery).
let active = false;
const draw = function(e) {
if (active == false) {
return;
}
const x = e.clientX;
const y = e.clientY;
let div = document.createElement("div");
div.style.top = y + "px";
div.style.left = x + "px";
document.body.appendChild(div);
}
const drawActive = function() {
active = !active
}
function changeColor() {
// Add logic to change color
}
document.getElementById("red").onclick = changeColor;
document.body.addEventListener("mousemove", draw);
document.body.addEventListener("mousedown", drawActive);
document.body.addEventListener("mouseup", drawActive);
body {
height: 100vh;
margin: 0;
}
#containter {
width: 200px;
height: 200px;
border-radius: 0;
background-color: #fff;
position: fixed;
}
div {
width: 5px;
height: 5px;
border-radius: 50%;
background-color: #000;
position: absolute;
}
#red {
width: 25px;
height: 25px;
background-color: red;
border-radius: 0;
margin-top: 18%;
}
.blue {
width: 25px;
height: 25px;
background-color: blue;
border-radius: 0;
margin-top: 6%;
margin-left: 12%;
}
.
.
.
<!DOCTYPE html>
<html lang="en>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Paint</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="container">
<div id="red"></div>
<div class="green"></div>
<div class="blue"></div>
<div class="yellow"></div>
</div>
<script src="script.js"></script>
</body>
</html>
Attempted using setAttribute method with some errors encountered.
Unsure about the required details for implementation.
Still figuring out which specifics are essential.
@Edit from review: resolved repetitions...