Sharing is caring!

JavaScript Project: Online Drawing Board

JavaScript Project: Online Drawing Board: This code will create an online drawing board where users can draw, sketch, or create digital artwork using various tools and color options. The elements will be centered on the page, and the design will include color options and a clear button.

CSS: The style

<!DOCTYPE html>
<html lang="en">
<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">
    <title>Free Drawing Board</title>

           <!-- CSS -->

    <style>
        body {
              display: flex;
              justify-content: center;
              align-items: center;
              height: 100vh;
            }
        
            .container {
              text-align: center;
            }
        
            .titre{
                color: #154415;
                font-size: 58px;
            }
        
            #drawing-board {
              border: 1px solid #ccc;
              background-color: #fff;
            }
        
            #color-options {
              margin-top: 10px;
              display: flex;
              justify-content: center;
            }
        
            .color-option {
              display: inline-block;
              width: 20px;
              height: 20px;
              border: 1px solid #ccc;
              margin-right: 5px;
              cursor: pointer;
              border-radius: 15px;
            }
        
            #clear-btn {
              margin-top: 10px;
              background-color: #154415;
              color: #fff;
              border: none;
              padding: 10px 20px;
              font-size: 16px;
              cursor: pointer;
              border-radius: 30px;
            }
        
            .selected {
              border: 2px solid #000;
            }
        
            #clear-btn {
              margin-top: 10px;
            }
          </style>
</head>

HTML: the body

<body>
    <h1 class="titre">Online Drawing Board</h1>
    <canvas id="drawing-board" width="800" height="600"></canvas>
    <br>
    <div id="color-options">
      <div class="color-option selected" style="background-color: #000000;"></div>
      <div class="color-option" style="background-color: #ff0000;"></div>
      <div class="color-option" style="background-color: #00ff00;"></div>
      <div class="color-option" style="background-color: #0000ff;"></div>
      <div class="color-option" style="background-color: #ffff00;"></div>
      <div class="color-option" style="background-color: #ff00ff;"></div>
      <div class="color-option" style="background-color: #00ffff;"></div>
    </div>
    <br>
    <button id="clear-btn">Clear</button>

Javascript

                <!-- Javasctript -->

    <script>
        // Get the canvas element and its context
        var canvas = document.getElementById('drawing-board');
        var context = canvas.getContext('2d');
        
        // Set initial drawing color and line width
        var color = '#000000';
        var lineWidth = 5;
        
        // Flag to track drawing state
        var isDrawing = false;
        
        // Function to start drawing
        function startDrawing(event) {
          isDrawing = true;
          
          // Get the coordinates of the mouse pointer
          var x = event.pageX - canvas.offsetLeft;
          var y = event.pageY - canvas.offsetTop;
          
          // Begin a new drawing path
          context.beginPath();
          
          // Set the starting point of the path
          context.moveTo(x, y);
        }
        
        // Function to continue drawing
        function continueDrawing(event) {
          if (isDrawing) {
            // Get the coordinates of the mouse pointer
            var x = event.pageX - canvas.offsetLeft;
            var y = event.pageY - canvas.offsetTop;
            
            // Draw a line to the current mouse position
            context.lineTo(x, y);
            
            // Set the line color and width
            context.strokeStyle = color;
            context.lineWidth = lineWidth;
            
            // Stroke the path to display the line
            context.stroke();
          }
        }
        
        // Function to stop drawing
        function stopDrawing() {
          isDrawing = false;
        }
        
        // Function to clear the drawing board
        function clearDrawingBoard() {
          context.clearRect(0, 0, canvas.width, canvas.height);
        }
        
        // Function to set the selected color
        function setSelectedColor(selectedColor) {
          // Remove the "selected" class from all color options
          var colorOptions = document.querySelectorAll('.color-option');
          colorOptions.forEach(function(option) {
            option.classList.remove('selected');
          });
          
          // Add the "selected" class to the clicked color option
          selectedColor.classList.add('selected');
          
          // Get the background color of the selected color option
          color = selectedColor.style.backgroundColor;
        }
        
        // Event listeners for drawing actions
        canvas.addEventListener('mousedown', startDrawing);
        canvas.addEventListener('mousemove', continueDrawing);
        canvas.addEventListener('mouseup', stopDrawing);
        canvas.addEventListener('mouseleave', stopDrawing);
        
        // Event listener for clear button
        var clearButton = document.getElementById('clear-btn');
        clearButton.addEventListener('click', clearDrawingBoard);
        
        // Event listener for color options
        var colorOptions = document.querySelectorAll('.color-option');
        colorOptions.forEach(function(option) {
          option.addEventListener('click', function() {
            setSelectedColor(this);
          });
        });
      </script>
</body>
</html>

JavaScript ile neler yapılabilir? JavaScript öğrenmek ne kadar sürer? JavaScript hangi program? JavaScript neden popüler?¿Qué proyectos se pueden hacer con JavaScript? ¿Qué es un proyecto en JavaScript? ¿Cómo empezar un proyecto de JavaScript? ¿Qué programas se han hecho con JavaScript? Wird JavaScript noch benötigt? Was kann man alles mit JavaScript machen? Ist JavaScript für Anfänger? Wie schwierig ist JavaScript? مشاريع جافا سكريبت للمبتدئين مشاريع جافا سكريبت جاهزة pdf مشروع جافا سكريبت javascript مشروع جافا سكريبت github تفعيل جافا سكريبت على الهاتف مشاريع جافا للمبتدئين جافا سكريبت تحميل تحميل جافا سكريبت للاندرويد


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *