xXxAirSnifferxXx
xXxAirSnifferxXx

Reputation: 55

drag and drop image, display that image on canvas

i want to do a drag and drop image and then display that image on canvas and i don't really know how to do this. but here is what i tried:

var canvas = document.getElementById("canvas2");
var c = canvas.getContext("2d");
//console.log(canvas)


canvas.ondrop = function(e){
    e.preventDefault();
    c.drawImage(e.dataTransfer.files[0], 0, 0, c.canvas.width, c.canvas.height);
}

canvas.ondragover = function(e){
    console.log("test")
    return false;
}
#canvas2{
    position:relative;
    z-index:100;
    margin-top: 100px;
    border:1px solid #000000;
}
<html lang="en">
<head>
    <!-- <meta http-equiv="refresh" content="5" >  -->
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="./style.css">
</head>
<body>
  <!-- <label for="GridCheck">Grid:</label>  -->
  <!-- <input type="checkbox" id="GridCheck" onclick="GridToggle('canvas1')"> -->
  <div class="canvasContainer">
    <canvas id = "canvas2" class="canvas"></canvas>
  </div>
  <script src="main.js"></script>
</body>
</html>

i know that the problem is "c.drawImage(e.dataTransfer.files[0], 0, 0, c.canvas.width, c.canvas.height);" part. do i need to actually upload image to access it display it on canvas?? im confused...

Upvotes: 2

Views: 371

Answers (1)

A A
A A

Reputation: 33

You can do this, though it might be a bit different from what you said I think you can edit it enough to make it like what you want:

Code

const fill = document.querySelector('.fill');
const empties = document.querySelectorAll('.empty');


fill.addEventListener('dragstart', dragStart);
fill.addEventListener('dragend', dragEnd);


for (const empty of empties) {
  empty.addEventListener('dragover', dragOver);
  empty.addEventListener('dragenter', dragEnter);
  empty.addEventListener('dragleave', dragLeave);
  empty.addEventListener('drop', dragDrop);
}



function dragStart() {
  this.className += ' hold';
  setTimeout(() => (this.className = 'invisible'), 0);
}

function dragEnd() {
  this.className = 'fill';
}

function dragOver(e) {
  e.preventDefault();
}

function dragEnter(e) {
  e.preventDefault();
  this.className += ' hovered';
}

function dragLeave() {
  this.className = 'empty';
}

function dragDrop() {
  this.className = 'empty';
  this.append(fill);
}
body {
  background: #1a911a;
}

.fill {
  background-image: url('https://source.unsplash.com/random/150x150');
  position: relative;
  height: 150px;
  width: 150px;
  top: 5px;
  left: 5px;
  cursor: pointer;
}

.hold {
  border: solid 5px #ccc;
}

.empty {
  display: grid;
  height: 160px;
  width: 160px;
  margin: 10px;
  border: solid 3px red;
  background: white;
  grid-row: 1/4;
  grid-column: 3/4;
}

.hovered {
  background: #f4f4f4;
  border-style: dashed;
}
<!DOCTYPE HTML>
<html lang="en">

<head>
  <script src="(Filename).js"></script>
  <title>Drag and Drop</title>
</head>

<body>
  <div class="empty">
    <div class="fill" draggable="true"> </div>
  </div>
  <div class="empty"></div>
</body>

</html>

Upvotes: 2

Related Questions