0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

webでお絵かきアプリ

Posted at

はじめに

とりあえずプログラムを載せときます。HTMLとCSSが一緒になったやつ。結構な時間をかけて作ったので

いいねを押してくれると嬉しいです。

<!DOCTYPE html>
<html>
<head>
  <style>
    body {
      display: flex;
      flex-direction: column;
      align-items: center;
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
      background-color: #f1f1f1;
    }

    canvas {
      border: 1px solid black;
    }

    #tools {
      display: flex;
      margin-top: 10px;
    }

    button {
      margin: 0 5px;
      padding: 5px 10px;
      border: none;
      cursor: pointer;
    }
  </style>
</head>
 <title>お絵かきアプリ</title>
<body>
  <canvas id="canvas" width="800" height="600"></canvas>
  <div id="tools">
    <input type="color" id="colorPicker">
    <input type="range" id="penSize" min="1" max="20" value="5">
    <button id="eraser">消しゴム</button>
    <input type="range" id="eraserSize" min="5" max="50" value="20">
    <button id="clear">全消去</button>
  </div>

  <script>
    const canvas = document.getElementById("canvas");
    const context = canvas.getContext("2d");
    let painting = false;
    let color = "black";
    let erasing = false;
    let penSize = 5;

    canvas.addEventListener("mousedown", startPainting);
    canvas.addEventListener("mouseup", stopPainting);
    canvas.addEventListener("mousemove", draw);

    document.getElementById("colorPicker").addEventListener("change", e => {
      color = e.target.value;
      erasing = false;
    });

    document.getElementById("eraser").addEventListener("click", () => {
      color = "white";
      erasing = true;
      penSize = document.getElementById("eraserSize").value;
    });

    document.getElementById("penSize").addEventListener("input", e => {
      penSize = e.target.value;
      erasing = false;
    });

    document.getElementById("eraserSize").addEventListener("input", e => {
      penSize = e.target.value;
      if (erasing) {
        erasing = true;
      }
    });

    document.getElementById("clear").addEventListener("click", clearCanvas);

    function startPainting(event) {
      painting = true;
      draw(event);
    }

    function stopPainting() {
      painting = false;
      context.beginPath();
    }

    function draw(event) {
      if (!painting) return;

      context.lineWidth = penSize;
      context.lineCap = "round";
      context.strokeStyle = color;

      if (erasing) {
        context.globalCompositeOperation = "destination-out";
      } else {
        context.globalCompositeOperation = "source-over";
      }

      context.lineTo(event.clientX - canvas.getBoundingClientRect().left, event.clientY - canvas.getBoundingClientRect().top);
      context.stroke();
      context.beginPath();
      context.moveTo(event.clientX - canvas.getBoundingClientRect().left, event.clientY - canvas.getBoundingClientRect().top);
    }

    function clearCanvas() {
      if (confirm("本当に全ての描画を消去しますか?")) {
        context.clearRect(0, 0, canvas.width, canvas.height);
      }
    }
  </script>
</body>
</html>

jsのプログラム

const clickButton = document.getElementById("clickButton");
const countElement = document.getElementById("count");

let count = 0;

clickButton.addEventListener("click", () => {
  count++;
  countElement.textContent = count;

  if (count === 10) {
    alert("Congratulations! You won!");
    count = 0;
    countElement.textContent = count;
  }
});

このプログラムで質問がある方はコメント欄にお願いします。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?