用户可以通过拖动到蒙版图像外部来拖动图像

时间:2019-02-13 07:50:46

标签: javascript html css html5 css3

背景

我允许用户在蒙版图像内上传图像,一旦用户上传,他就可以将上传的图像拖动到蒙版图像内。...很好。 ...

问题

但是连同内部,用户可以通过拖动外部蒙版图像来拖动上载的图像。...但是我想停止此操作。...

这里是video link

https://codepen.io/kidsdial/pen/qgybgL

fileupa.onchange = e => {
 target_imga.src = URL.createObjectURL(fileupa.files[0]);   
}
let prevX = 0, prevY = 0,translateX = 0, translateY = 0, scale = 1, zoomFactor = 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) {
  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+")"; 

if(document.querySelector('#uploadedImg img'))
document.querySelector('#uploadedImg img').style.transform = transform;
}
.container {
  border: 1px solid #DDDDDD;
  width: 612px;
  height: 612px;
  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;
}
.masked-imga {
  -webkit-mask-image: url(https://i.stack.imgur.com/ChbtV.png);
  mask-image: url(http://139.59.24.243/ecom1/site/test/images/heart2.png);
  -webkit-mask-position: center center;
  mask-position: center center;
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
  width: 416px;
  height: 388px;
  position: absolute;
  top: 10px;
  left: 173px; 
  background-color: white;
}
.minaimga {
  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"   ondragover="onDragOver(event)"ondragover="onDragOver(event)" >
  <div draggable="true" ondragstart="onDragStart(event)" id="uploadedImg">
    <div class="minaimga">         
      <img id="target_imga"  alt="">
      <div></div>
    </div>
  </div>
</div>
</div>

如果您需要有关此问题的其他信息,请告诉我。.

请帮助我找到解决方案.....

预先感谢。...

0 个答案:

没有答案