お絵描きアプリをJS勉強会で作りました。

JavaScriptの基本文法を用いて、サクッと30分ほどでお絵描きwebアプリを作成いたしました。

目次

  • HTML
  • CSS
  • JavaScript
  • 完成品

HTML

<!DOCTYPE html>
<html lang="ja">

<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">
    <link rel="stylesheet" href="style.css">
    <title>お絵描きアプリ</title>
</head>

<body>
    <div class="canvas">
        <canvas id="canvas" width="700" height="400"></canvas>
        <ul class="palette">
            <li class="item"><img src="img/red.jpg" alt=""></li>
            <li class="item"><img src="img/black.jpg" alt=""></li>
            <li class="item"><img src="img/eraser.png" alt=""></li>
            <li class="item"><img src="img/random.png" alt=""></li>
        </ul>
    </div>
</body>

<script src="main.js"></script>
</html

CSS

@charset 'UTF-8';
body{
    margin: 0;
}

#canvas{
    /* background-color: #eeeeee; */
    border: 15px solid gray;
    border-radius: 5px;
}

.canvas{
    position: relative;
    margin: 0 auto;
    width: 700px;
    height: 400px;
}

.palette{
    position: absolute;
    right: 10px;
    bottom: 10px;
    display: flex;
    margin: 0;
    padding: 0;
    list-style: none;
}

.palette li{
    margin-left: 10px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
}

.palette img{
    width: 40px;
    height: 40px;
    border-radius: 50%;
}

JavaScript

const $canvas = document.getElementById('canvas');
const context = $canvas.getContext('2d');
let getX, getY;
let bool = false;

context.lineWidth = '10';
context.strokeStyle = '#fff';
context.lineCap = 'round';
context.lineJoin = 'round';

$canvas.addEventListener('mousedown', e => {
    getX = e.offsetX;
    getY = e.offsetY;
    bool = true;
})


$canvas.addEventListener('mouseup', e => {

    bool = false;

})

$canvas.addEventListener('mousemove', e => {
    // クリックしていないときは描画しない
    if (!bool) {
        return;
    } else {
        // パスの開始
        context.beginPath();
        // 最初の座標
        context.moveTo(getX, getY);
        // 
        context.lineTo(e.offsetX, e.offsetY);
        // 線の描画
        context.stroke();
    }

    getX = e.offsetX;
    getY = e.offsetY;
})

const $item = document.querySelectorAll('.item');

context.strokeStyle = '#000';
$item[1].style.outline = '5px solid #ff0';
$item[3].style.outline = 'none';

$item[0].addEventListener('click', e => {
    context.globalCompositeOperation = 'source-over';
    context.strokeStyle = '#f00';
    for (let i = 0; i < $item.length; i++) {
        $item[i].style.outline = 'none';
    }
    $item[0].style.outline = '5px solid #ff0';
})

$item[1].addEventListener('click', e => {
    context.globalCompositeOperation = 'source-over';
    context.strokeStyle = '#000';

    for (let i = 0; i < $item.length; i++) {
        $item[i].style.outline = 'none';
    }
    $item[1].style.outline = '5px solid #ff0';
})

$item[2].addEventListener('click', e => {
    context.globalCompositeOperation = 'destination-out';
    for (let i = 0; i < $item.length; i++) {
        $item[i].style.outline = 'none';
    }
    $item[2].style.outline = '5px solid #ff0';
})

$item[3].addEventListener('click', e => {
    if ($item[3].style.outline !== 'none') {
        return;
    } else {
        const num = [Math.floor(Math.random() * 256), Math.floor(Math.random() * 256), Math.floor(Math.random() * 256)];
        context.globalCompositeOperation = 'source-over';
        context.strokeStyle = `rgb(${num[0]},${num[1]},${num[2]})`;
        for (let i = 0; i < $item.length; i++) {
            $item[i].style.outline = 'none';
        }
        $item[3].style.outline = '5px solid #ff0';
    }
})

完成品