I'm creating a demo page for an image stitching tool, where the user's can use images shown on the webpage instead of uploading their own.
I want the user to be able to drag the image from the img tag to the input within the form.
Here is the html that I have so far:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Stitcher</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<script src="static/js/index.js" defer></script>
</head>
<body>
<div class="content">
<nav>
<h1>Image Stitcher</h1>
<a class="link" href="{{url_for('home')}}">home</a>
<a class="link right" href="{{url_for('about')}}">about</a>
</nav>
<br />
<br />
<div class="row">
<div class="form-area">
<form action="http://127.0.0.1:5000" method="POST" enctype="multipart/form-data">
<label for="left_image">Upload the left image:</label>
<input type="file" id="left_image" name="left_image" accept="image/png, image/jpeg" />
<br />
<label for="right_image">Upload the right image:</label>
<input type="file" id="right_image" name="right_image" accept="image/png, image/jpeg" />
<br />
<button class="submit" type="submit">Start Stitching</button>
</form>
</div>
<div class="image-row">
<div class="demo-image">
<p> drag me to the <strong>left image</strong></p>
<img id="image-to-drag" src="/static/left_art.png" alt="Left Image">
</div>
<div class="demo-image">
<p> drag me to the <strong>right image</strong></p>
<img src="/static/right_art.png" alt="Right Image">
</div>
</div>
</div>
</div>
<br />
<br />
</body>
</html>
I'm also open to answers where the ui fakes this behaviour, and uploads some base64 encoding of the image instead.
I have tried using the drag event, but the DragEvent.dataTransfer.files comes up empty when I drag the image.
// from index.js
function drag(event) {
console.log('DRAG')
console.log(event)
}
// and change the img elements to this
<img ondragstart="drag(event)" draggable="true" src="/static/right_art.png" alt="Right Image">
If the image is already on the web server (or another web server) you can just grab the URL of the image and add that to the form.