使用拖动代码后图像上传没有发生
Image uploading not happening after using dragging code
在这个 Codepen1 : 中,我将一张图片上传到另一张图片中....
在这个 Codepen2 中,我可以将一张图片拖到另一张图片中....
现在我正在尝试结合这两个功能,将一张图片上传并拖动到另一张图片中,但在 codepen3
中没有上传
Html
<input type="file" onchange="load(this.files[0])"/><br>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="324" height="395">
<mask id="mask1">
<image style="background-color:green;" xlink:href="https://i.stack.imgur.com/L5daY.png" width="324" height="395"></image>
</mask>
<div class="minaimg masked-img" ondragover="onDragOver(event)" onwheel="scrollZoom(event)">
<div draggable="true" ondragstart="onDragStart(event)" id="uploadedImg">
<div class = "minaimg" >
<image id="masked-image" xlink:href="" mask="url(#mask1)"></image>
</div>
</div>
</div>
</svg>
脚本
function load(file) {
var img = new Image(),
imgURL = URL.createObjectURL(file);
img.onload = function() {
var width = img.naturalWidth,
height = img.naturalHeight,
maskedImage = document.getElementById('masked-image');
maskedImage.setAttribute('xlink:href', imgURL);
maskedImage.setAttribute('height', 395);
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;
}
URL.revokeObjectURL(imgURL);
};
img.src = imgURL;
}
如果您还有其他疑问,请告诉我....
提前致谢....
要显示用户磁盘中的图像,只需创建指向它的 URL,然后设置指向此 URL 的 标签的 src
。
你显然知道如何使用 URL.createObjectURL。
在你的可拖动版本中,你已经有了这样一个 元素,所以你只需要在输入的 onchange 事件中设置它的 src
即可:
fileup.onchange = e => {
target_img.src = URL.createObjectURL(fileup.files[0]);
}
///Below is OP's code\\
let prevX = 0, prevY = 0,translateX = 0, translateY = 0, scale = 1, zoomFactor = 0.1;
//const img = document.querySelector('#uploadedImg img');
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;
}
.masked-img {
background-color: green;
margin: 20px auto;
-webkit-mask-image: url(https://i.stack.imgur.com/EQpM4.png);
mask-image: url(https://i.stack.imgur.com/EQpM4.png);
-webkit-mask-position: center center;
mask-position: center center;
-webkit-mask-repeat: no-repeat;
mask-repeat: no-repeat;
position:relative;
bottom:60px;
left:40px;
}
.minaimg {
display: block;
}
<input type="file" id="fileup" />
<div class="minaimg masked-img" ondragover="onDragOver(event)" >
<div draggable="true" ondragstart="onDragStart(event)" id="uploadedImg">
<div class="minaimg">
<!-- give an id for ease of selection -->
<img id="target_img" src="https://i.stack.imgur.com/AV6yq.jpg" alt="">
<div></div>
</div>
</div>
</div>
在这个 Codepen1 : 中,我将一张图片上传到另一张图片中....
在这个 Codepen2 中,我可以将一张图片拖到另一张图片中....
现在我正在尝试结合这两个功能,将一张图片上传并拖动到另一张图片中,但在 codepen3
中没有上传Html
<input type="file" onchange="load(this.files[0])"/><br>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="324" height="395">
<mask id="mask1">
<image style="background-color:green;" xlink:href="https://i.stack.imgur.com/L5daY.png" width="324" height="395"></image>
</mask>
<div class="minaimg masked-img" ondragover="onDragOver(event)" onwheel="scrollZoom(event)">
<div draggable="true" ondragstart="onDragStart(event)" id="uploadedImg">
<div class = "minaimg" >
<image id="masked-image" xlink:href="" mask="url(#mask1)"></image>
</div>
</div>
</div>
</svg>
脚本
function load(file) {
var img = new Image(),
imgURL = URL.createObjectURL(file);
img.onload = function() {
var width = img.naturalWidth,
height = img.naturalHeight,
maskedImage = document.getElementById('masked-image');
maskedImage.setAttribute('xlink:href', imgURL);
maskedImage.setAttribute('height', 395);
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;
}
URL.revokeObjectURL(imgURL);
};
img.src = imgURL;
}
如果您还有其他疑问,请告诉我....
提前致谢....
要显示用户磁盘中的图像,只需创建指向它的 URL,然后设置指向此 URL 的 标签的 src
。
你显然知道如何使用 URL.createObjectURL。
在你的可拖动版本中,你已经有了这样一个 元素,所以你只需要在输入的 onchange 事件中设置它的 src
即可:
fileup.onchange = e => {
target_img.src = URL.createObjectURL(fileup.files[0]);
}
///Below is OP's code\\
let prevX = 0, prevY = 0,translateX = 0, translateY = 0, scale = 1, zoomFactor = 0.1;
//const img = document.querySelector('#uploadedImg img');
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;
}
.masked-img {
background-color: green;
margin: 20px auto;
-webkit-mask-image: url(https://i.stack.imgur.com/EQpM4.png);
mask-image: url(https://i.stack.imgur.com/EQpM4.png);
-webkit-mask-position: center center;
mask-position: center center;
-webkit-mask-repeat: no-repeat;
mask-repeat: no-repeat;
position:relative;
bottom:60px;
left:40px;
}
.minaimg {
display: block;
}
<input type="file" id="fileup" />
<div class="minaimg masked-img" ondragover="onDragOver(event)" >
<div draggable="true" ondragstart="onDragStart(event)" id="uploadedImg">
<div class="minaimg">
<!-- give an id for ease of selection -->
<img id="target_img" src="https://i.stack.imgur.com/AV6yq.jpg" alt="">
<div></div>
</div>
</div>
</div>