Canvas

by Urrby

HTML

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="Etch A Sketch_style.css">
        <title>Etch A Sketch</title>
    </head>
    <body>
        <div class="wrap">
            <canvas id="canvas" width="1500" height="1000"></canvas>
            <div class="toolbar">
                <div class="colors">
                    <button type="button" class="btn" id="btn_black">Black</button>
                    <button type="button" class="btn" id="btn_red">Red</button>
                    <button type="button" class="btn" id="btn_blue">Blue</button>
                    <button type="button" class="btn" id="btn_green">Green</button>
                    <button type="button" class="btn" id="btn_yellow">Yellow</button>
                </div>
                
                <div class="stroke_weight">
                    <button type="button" class="btn utility" id="btn_minus">-</button>
                    <button type="button" class="btn utility" id="btn_plus">+</button>
                </div>
                <div class="stroke_style">
                    <button type="button" class="btn utility" id="btn_round">Round</button>
                    <button type="button" class="btn utility" id="btn_square">Square</button>
                </div>
                <button type="button" class="btn" id="btn_erase">Erase</button>
            </div>
        </div>
    <script src="Etch A Sketch_script.js"></script>
    </body>
</html>

CSS

body{
    min-height: 100vh;
    background-color: red;
    display: grid;
    align-items: center;
    justify-items: center;
    background-color: rgba(143, 143, 143, 1);
    margin: 0;
}

.wrap{
    background-color: rgba(255, 10, 39, 1);
    padding: 20px 20px 0 20px;
    border-radius: 2%;
    box-shadow: 3px 3px 8px black;
    border: solid black 1px;
}

#canvas{
    width: 900px;
    height: 600px;
    background-color: white;
    border-radius: 2%;
    border: solid black 1px;
}

.toolbar{
    padding: 30px 0;
    display: grid;
    grid-gap: 10px;
    grid-template-columns: auto 1fr 1fr auto;
    width: 900px;
}

input[type="radio"]{
    margin: 0;
}

.btn{
    border: none;
    background-color: red;
    color: white;
    border-radius: 10px;
    padding: 5px;
    width: 55px;
    height: 40px;
    border: solid white 1px;
    box-shadow: 1px 1px 2px black;
    margin-left: 5px;
}

.utility{
    background-color: rgba(148, 148, 148, 1);
}

#btn_black{
    background-color: rgba(0, 0, 0, 1);
    margin-left: 0px;
}

#btn_red{
    background-color:rgba(204, 51, 51, 1);
}

#btn_blue{
    background-color: rgba(51, 89, 204, 1);
}

#btn_green{
    background-color: rgba(82, 204, 51, 1);
}

#btn_yellow{
    background-color: rgba(204, 199, 51, 1);
}

#btn_erase{
    background-color: rgba(240, 240, 240, 1);
    width: 70px;
    color: black;
    border: solid black 1px;
}


#btn_plus, #btn_minus{
    font-size: 24px;
}

.label_title{
    margin: 0 3px;
    font-size: 18px;
}

select{
    margin: 0 50px 0 0;
}

.stroke_weight{
    text-align: right;
}

* {
    margin: 0;
    padding: 0;
  cursor:url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg'  width='40' height='48' viewport='0 0 100 100' style='fill:black;font-size:24px;'><text y='50%'>✍️</text></svg>") 16 0,auto;
  }
  canvas {
    width: 100vw;
    height: 100vh;
  }

JavaScript

const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
let coord = { x: 0, y: 0 };

function resize() {
    ctx.canvas.width = window.innerWidth;
    ctx.canvas.height = window.innerHeight;
  }
  
  resize();

  function start(event) {
    document.addEventListener("mousemove", draw);
    reposition(event);
  }

  function reposition(event) {
    coord.x = event.clientX - canvas.offsetLeft;
    coord.y = event.clientY - canvas.offsetTop;
  }

  function stop() {
    document.removeEventListener("mousemove", draw);
  }

  function draw(event) {
    ctx.beginPath();
    ctx.lineWidth = 5;
    ctx.lineCap = "round";
    ctx.strokeStyle = "#ACD3ED";
    ctx.moveTo(coord.x, coord.y);
    reposition(event);
    ctx.lineTo(coord.x, coord.y);
    ctx.stroke();
  }


document.addEventListener("mousedown", start);
document.addEventListener("mouseup", stop);
window.addEventListener("resize", resize);