DEV Community

Vijay Kumar
Vijay Kumar

Posted on

Image Uploader with a Draggable text

Today we are going to implement the draggable text using HTML,CSS,JAVASCRIPT

Main thing here is to understand javascript code

  1. I have written all the explanation with code i.e see comments.
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Image Uploader with Draggable Text</title>
    <style>
      body {
        font-family: Arial, sans-serif;
        text-align: center;
      }

      #image-container {
        position: relative;
        display: inline-block;
        margin: 20px;
      }

      #uploaded-image {
        max-width: 100%;
        max-height: 400px;
      }

      #draggable-text {
        position: absolute;
        top: 50px;
        left: 50px;
        color: white;
        background-color: rgba(0, 0, 0, 0.5);
        padding: 5px 10px;
        cursor: move;
        font-size: 18px;
      }

      #text-input {
        margin-top: 20px;
        width: 300px;
        padding: 10px;
        font-size: 16px;
      }
    </style>
  </head>
  <body>
    <h1>Image Uploader with Draggable Text</h1>

    <!-- Image uploader -->
    <input type="file" id="image-input" accept="image/*" /><br /><br />

    <!-- Image and draggable text container -->
    <div id="image-container">
      <img
        id="uploaded-image"
        src=""
        alt="Your uploaded image"
        style="display: none"
      />
      <div id="draggable-text" draggable="true">Your text here</div>
    </div>

    <!-- Text input for modifying the text on the image -->
    <textarea
      id="text-input"
      placeholder="Enter text to display on image"
    ></textarea>

    <script>
      const imageInput = document.getElementById("image-input");
      const uploadedImage = document.getElementById("uploaded-image");
      const draggableText = document.getElementById("draggable-text");
      const textInput = document.getElementById("text-input");

      // Handle image upload
      imageInput.addEventListener("change", function (e) {
        const file = e.target.files[0]; // files is an array like object but not an array, but somehow we can access the elements of this array like object in a way we access in an array. first index number store first image,second store second and so on.
        if (file) {
          const reader = new FileReader(); // return a filereader object
          reader.onload = function (event) {
            uploadedImage.src = event.target.result; // this onload function is called automatically when file reading is complete
            uploadedImage.style.display = "block";
          };
          reader.readAsDataURL(file); // readAsDataURL reads the file
        }
      });

      // Update the draggable text based on textarea input
      textInput.addEventListener("input", function () {
        draggableText.textContent = textInput.value;
      });

      // Drag and Drop functionality for the text
      let offsetX, offsetY;

      draggableText.addEventListener("dragstart", function (e) {
        const rect = draggableText.getBoundingClientRect(); // it returns an object which have position of element according to viewport and dimensions
        offsetX = e.clientX - rect.left;//e.clientX is mouse dragstart horizontal position and rect.left element current horizontal position.
        offsetY = e.clientY - rect.top;//e.clientY is mouse dragstart vertical position and rect.top element current vertical position.
      });

      document.addEventListener("dragover", function (e) {// without this the dragged element will not be drop.
        e.preventDefault();
      });

      document.addEventListener("drop", function (e) {
        e.preventDefault();
        const dropX = e.clientX - offsetX;
        const dropY = e.clientY - offsetY;
        draggableText.style.left = `${dropX}px`;
        draggableText.style.top = `${dropY}px`;
      });
    </script>
  </body>
</html>

Enter fullscreen mode Exit fullscreen mode

Top comments (0)