dimanche 22 juillet 2018

Paint JS changing color onclick

I'm trying to make Paint in JS here's the code. I want to change color of the trail from black to red by clicking on div "red" for example,and I'm out of ideas (without jQuery).

HTML:

<!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="containter">
        <div id="red"></div>
        <div class="green"></div>
        <div class="blue"></div>
        <div class="yellow"></div>
        </div>
    <script src="script.js"></script>
</body>
</html>

CSS

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%;
}
.yellow{
    width:25px;
    height:25px;
    background-color:yellow;
    border-radius:0;
    margin-top: 18%;
    margin-left: 12%;
}
.green{
    width:25px;
    height:25px;
    background-color:green;
    border-radius:0;
    margin-top: 6%;
}   
.first{
    background-color:red !important;
    width:5px;
    height:5px;
    border-radius:50%;
    position:absolute;
}
.mainColor{
    background-color:red !important;
    width:5px;
    height:5px;
    border-radius:50%;
    position:absolute;
}

JS

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(){
    //div.classList.add("mainColor");
}
document.getElementById("red").onclick = changeColor;
document.body.addEventListener("mousemove",draw);
document.body.addEventListener("mousedown",drawActive);
document.body.addEventListener("mouseup",drawActive);

I tried to make it with setAttribute but I had some errors.I don't know which details should I give.I don't know which details should I give.I don't know which details should I give.I don't know which details should I give.I don't know which details should I give.I don't know which details should I give.I don't know which details should I give.I don't know which details should I give.




Aucun commentaire:

Enregistrer un commentaire