Background :
if i use single image , i can able to drag the uploaded image inside another image : https://codepen.io/AlenToma/pen/WPLmbw
video link1
Issue :
But if i use two images, only uploading is working, dragging is not working : https://codepen.io/kidsdial/pen/JxxpzM
video link2
Console Error :
Uncaught TypeError: canvas.getContext is not a function
let prevX = 0,
prevY = 0,
translateX = 0,
translateY = 0,
scale = 1,
zoomFactor = 0.1,
draggable = false,
ctx;
fileupa.onchange = e => {
target_imga.src = URL.createObjectURL(fileupa.files[0]);
var maskedImageUrl ="https://i.stack.imgur.com/jnStF.png";
maskedImage = new Image();
maskedImage.src = maskedImageUrl;
ctx = canvas.getContext("2d");
maskedImage.onload = function() {
console.log("loaded")
canvas.width = maskedImage.width;
canvas.height = maskedImage.height;
ctx.drawImage(maskedImage, 0, 0, maskedImage.width, maskedImage.height);
}
}
fileupb.onchange = e => {
target_imgb.src = URL.createObjectURL(fileupb.files[0]);
var maskedImageUrl ="https://i.stack.imgur.com/jnStF.png";
maskedImage = new Image();
maskedImage.src = maskedImageUrl;
ctx = canvas.getContext("2d");
maskedImage.onload = function() {
console.log("loaded")
canvas.width = maskedImage.width;
canvas.height = maskedImage.height;
ctx.drawImage(maskedImage, 0, 0, maskedImage.width, maskedImage.height);
}
}
minaimgContainer.onmousedown = function(event) {
var x = event.offsetX + (translateX)
var y = event.offsetY + (translateY)
var pixelData = canvas.getContext('2d').getImageData(x, y, 1, 1).data;
draggable = pixelData[0] > 1;
}
function onDragStart(evt) {
if (evt.dataTransfer && evt.dataTransfer.setDragImage) {
// evt.dataTransfer.setDragImage(evt.target.nextElementSibling, 0, 0);
}
prevX = evt.clientX;
prevY = evt.clientY;
}
function onDragOver(evt) {
if (draggable) {
translateX += evt.clientX - prevX;
translateY += evt.clientY - prevY;
prevX = evt.clientX;
prevY = evt.clientY;
updateStyle();
}
}
function updateStyle() {
let transform = "translate(" + translateX + "px, " + translateY + "px) scale(" + scale + ")";
var img = document.querySelector('#uploadedImg img');
img.style.transform = transform;
}
.container {
border: 1px solid #DDDDDD;
width: 1212px;
height: 1212px;
position:relative;
background:red;
}
.customa {
border: 1px solid #ccc;
display: inline-block;
padding: 6px 12px;
cursor: pointer;
position: relative;
top: 100px;
z-index: 1;
left: 280px;
}
.customb{
border: 1px solid #ccc;
display: inline-block;
padding: 6px 12px;
cursor: pointer;
position: relative;
top: 100px;
z-index: 1;
left: 580px;
}
#canvas{
display: block;
position: absolute;
top: 0;
z-index: -1;
}
.masked-imga
{
width: 416px;
height: 388px;
-webkit-mask-image: url(https://i.stack.imgur.com/jnStF.png);
mask-image: url(https://i.stack.imgur.com/jnStF.png);
-webkit-mask-position: center center;
mask-position: center center;
-webkit-mask-repeat: no-repeat;
mask-repeat: no-repeat;
position: absolute;
top: 10px;
left: 173px;
background-color: white;
overflow:hidden;
}
.masked-imgb
{
width: 416px;
height: 388px;
-webkit-mask-image: url(https://i.stack.imgur.com/jnStF.png);
mask-image: url(https://i.stack.imgur.com/jnStF.png);
-webkit-mask-position: center center;
mask-position: center center;
-webkit-mask-repeat: no-repeat;
mask-repeat: no-repeat;
position: absolute;
top: 10px;
left: 673px;
background-color: white;
overflow:hidden;
}
.minaimga
{
display: block;
background-color: white;
height: 278px;
}
.minaimgb
{
display: block;
background-color: white;
height: 278px;
}
<div class="container">
<label for="fileupa" class="customa">
Upload Image
</label>
<input id="fileupa" type="file" style="display:none;">
<div class="minaimg masked-imga" id="minaimgContainer" ondragover="onDragOver(event)"ondragover="onDragOver(event)" >
<div draggable="true" ondragstart="onDragStart(event)" id="uploadedImg">
<img id="target_imga" alt="">
<canvas id="canvas" width="416" height="388"></canvas>
</div>
</div>
<label for="fileupb" class="customb">
Upload Image
</label>
<input id="fileupb" type="file" style="display:none;">
<div class="minaimg masked-imgb" id="minaimgContainer" ondragover="onDragOver(event)"ondragover="onDragOver(event)" >
<div draggable="true" ondragstart="onDragStart(event)" id="uploadedImg">
<img id="target_imgb" alt="">
<canvas id="canvas" width="416" height="388"></canvas>
</div>
</div>
</div>
Thanks in advance.....