Guide to creating draggable text on a video with HTML

I have a challenge where I need to overlay text on a video and make it draggable across the entire video. Currently, I am able to display the text over the video successfully, but when I try to drag it and then drop it, the text appears below the video and becomes unresponsive to further dragging. This project is being developed using React JS.

<div className="container" > 
        <div  onDrop={this.allowDrop} onDragOver={this.dragOver}> 
        <video id="video" width="820" src="video.mp4"/>
        </div>
        <div className="overlay">
            <p draggable onDragStart={this.allowDrag} id="hey">hey there</p>
        </div>
</div>
allowDrag=(e)=>{
  e.dataTransfer.setData("text", e.target.id);
}

 allowDrop = (e)=>{
  e.preventDefault();
  var data = e.dataTransfer.getData("text");
  e.target.appendChild(document.getElementById(data));
 }

 dragOver = (e)=>{
  e.preventDefault();
 }

This is how my CSS is structured:

.container { position:relative; }
.container video {
    position:relative;
    z-index:-1;
}
.overlay {
    position:absolute;
    top:0;
    left:0;
    z-index:1;
}

Does anyone have any suggestions on how to fix this issue?

Answer №1

Implementing mouse pointer coordinates using state

class App extends React.Component {
  state = {
    xCoord: 0,
    yCoord: 0
  };
  
  handleDragStart = e => {
    e.dataTransfer.setData("text", e.target.id);
  };

  handleDrop = e => {
    e.preventDefault();
    // var data = e.dataTransfer.getData("text");
    // e.target.appendChild(document.getElementById(data));
  };

  handleDragOver = e => {
    e.preventDefault();
    this.setState({
      xCoord: e.clientX,
      yCoord: e.clientY
    });
  };

  render() {
    return (
      <div className="container">
        <div onDrop={this.handleDrop} onDragOver={this.handleDragOver}>
          <video
            id="video"
            width="820"
            src="https://ak1.picdn.net/shutterstock/videos/1048614721/preview/stock-footage-young-student-watching-lesson-online-and-studying-from-home-young-woman-taking-notes-while-looking.mp4"
          />
        </div>
        <div
          className="overlay"
          style={{ top: this.state.yCoord, left: this.state.xCoord }}
        >
          <p draggable onDragStart={this.handleDragStart} id="hey">
            hey there
          </p>
        </div>
      </div>
    );
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discover the art of utilizing two distinct binding strings, wherein upon the selection of either, the alternate binding string shall automatically be

Having to use two different bindingstrings is a requirement due to a tool used for creating PDFs. My objective is to have the corresponding bindingstring turn "Off" when a user clicks on either the Yes or No button, and have the clicked button turn to "Yes ...

What is the best way to change the default info color in bootstrap 5.0 to match the info color in bootstrap 3?

Back in the days of bootstrap 3, the sweet info color was #d9edf7. Whenever you used bg-info or alert-info, that trusty old background color was #d9edf7 like clockwork. I'm on a mission to make bootstrap 5.0 behave the same way with the info color. S ...

Is it possible to apply draggable functionality in the same way you attach events to an arbitrary node and a selector?

Here is a possible solution: function myFunction(){/*.....*/} $("body").on("click", "li.itemlist", function(){ alert("ping"); }); This code sets up a click event for all li.itemlist elements under the body element. However, is there a way to sim ...

Collaborating on components within nested React.js applications

I have a unique situation where I have two React.js apps, one nested inside the other. Both were bootstrapped using create-react-app. The project directory structure is as follows: . +-- admin | +-- node_modules | +-- public | +-- src | +-- p ...

Whenever I click on <a href="whatever.aspx"></a>, I would like the page to be displayed within the current page

This is the code I am working with: <asp:DataList ID="dlGallery" runat="server" RepeatLayout="Flow" Width="100%" CellPadding="4" ForeColor="#333333"> <AlternatingItemStyle BackColor="White" ForeColor="#284775" /> <FooterStyle BackColor ...

Click to close the collapsible panel - Ant Design Collapse

Is it possible to automatically close the panel after clicking on the button using the onClick function? I came across this helpful post, but I encountered some issues after implementing it in my code. Below is the code for my custom collapse component i ...

Resetting the quiz by utilizing the reset button

Hello everyone, I'm new to this platform called Stack Overflow. I need some help with my quiz reset button. It doesn't seem to be working as intended. According to my code, when the reset button is clicked at the end of the quiz, it should bring ...

In the world of programming, there exists a mysterious creature known as

I've been experimenting with different methods, but nothing seems to be working for me. What I am attempting to accomplish is <?php $php_var = a-thing; echo ' <script text/JavaScript> document.cookie = "arrayid"+&apos ...

The Three.js environment experiences lag and a decrease in performance

In the threejs scene, there is a sphere geometry and a plane geometry. The sphere is textured with an image, while the plane geometry is textured with 2D text. The plane geometry is also attached with a click event. When clicking on the plane geometry, ...

What is stopping me from sending data via POST - Express?

I'm encountering an issue with Express [POST request]: I have developed server-side and client-side code to send data to both the terminal and the browser.. Unfortunately, I am unable to view the result of the post function!! Please assist me, I feel ...

Is there a way to enable data-add-back-btn using jQuery script?

Currently, I am utilizing jQuery 1.9.1 and jQuery Mobile 1.3.1 to define multiple pages within my project setup: <div id="q1" data-role="page" data-add-back-btn="false" data-back-btn-text="Home"> <div data-role="header" data-position="fixed" ...

Comparing XDomainRequest and XMLHttpRequest for IE8 and IE9: A detailed analysis

I am feeling pretty lost when it comes to understanding the XMLHttpRequest and XDomainRequest renaissance, and I could really use some guidance. Here are my thoughts so far: It seems like the XDomainRequest in IE8 and IE9 is some sort of subclass of XMLH ...

Execute a function when a selection option is chosen consecutively two times

I have a dynamic dropdown menu that uses AJAX to load options. I need the function to be triggered not only when an option is changed, but also when the same option is clicked multiple times in a row. In other words, I want the function to run every time a ...

Is there a way to determine whether a mouse-down event took place on the scroll-bar or elsewhere within the element?

Looking for a solution with my HTML div acting as a canvas containing various objects. The issue lies in the fact that when attempting to draw a selection rectangle by dragging with the mouse, if scroll bars appear due to the large size of the canvas, scr ...

The React component fails to render on a Razor page

Looking to render a React component within a Razor page but without using a div? You can achieve this by utilizing ReactDOM.render, however my goal is to utilize it as a tag within the Razor page itself. For example, if I have a class named App, I would li ...

When working with ajax, you can easily refer to elements by using the $(this)

I am having trouble using the $(this) selector in the ajax success function to target the element with the class .selectclass that was double-clicked before. Any suggestions on how to make this work within the ajax success function? Here's the jQuery ...

Pass theme-specific properties to components using a Theme provider from an external library

I am in the process of structuring my react project by linking my custom components from @material-ui as an external dependency. Using rollup, I successfully built and published my external dependency on npm. I am able to import it into my project, and the ...

Utilizing a CSS stylesheet to style a specific div element within a webpage with bootstrap framework

Recently, I acquired an HTML5 template integrated with Bootstrap. As I started writing HTML code within the template, I noticed some unexpected outcomes, most likely due to the default styles of Bootstrap. Now, I am curious to learn how I can incorporate m ...

Exploring Portals in ThreeJs

Hey, has anyone successfully created a portal effect in ThreeJS? I'm interested in experimenting with impossible architectural designs for my VR thesis. Specifically, I'm attempting to construct a small square house where looking through the doo ...

What is the best way to recycle Vue and Vuetify code?

<script> export default { data () { return { my_alert: false, text: '', color: 'success' } }, methods: { openAlt (text, color) { this.text = text this.color = color this.my_ale ...