gpt4 book ai didi

javascript - 多次使用时不显示蒙版图像

转载 作者:行者123 更新时间:2023-11-28 15:45:16 25 4
gpt4 key购买 nike

背景

我在网站上显示蒙版图片并告诉用户上传图片....

enter image description here

一旦用户上传图片,我就将用户上传的图片填充到蒙版图片中

enter image description here

它在 website 中工作正常& 在代码笔中:https://codepen.io/kidsdial/pen/wNqXEJ

要求:

现在我正在尝试上传两个蒙版图像,但这次蒙版图像没有像 codepen2 中那样显示:https://codepen.io/kidsdial/pen/OdjErW & 在 website2

html

<input type="file" id="fileupa" />
<input type="file" id="fileupb" />

<div class="minaimg masked-imga" ondragover="onDragOver(event)"ondragover="onDragOver(event)" >
<div draggable="true" ondragstart="onDragStart(event)" id="uploadedImg">
<div class="minaimga">
<!-- give an id for ease of selection -->
<img id="target_img" alt="">

<div></div>

</div>
</div>
</div>

<div class="minaimg masked-imgb" ondragover="onDragOver(event)"ondragover="onDragOver(event)" >
<div draggable="true" ondragstart="onDragStart(event)" id="uploadedImg">
<div class="minaimgb">
<!-- give an id for ease of selection -->
<img id="target_img" alt="">

<div></div>

</div>
</div>
</div>

CSS

.masked-imga {

-webkit-mask-image: url(/image/EQpM4.png);
mask-image: url(/image/EQpM4.png);
-webkit-mask-position: center center;
mask-position: center center;
-webkit-mask-repeat: no-repeat;
mask-repeat: no-repeat;

display: block;
width: 100%;
height: 100%;
}

.minaimga {
display: block;
background-color: black;
}

.masked-imgb {

-webkit-mask-image: url(/image/EQpM4.png);
mask-image: url(/image/EQpM4.png);
-webkit-mask-position: center center;
mask-position: center center;
-webkit-mask-repeat: no-repeat;
mask-repeat: no-repeat;

display: block;
width: 100%;
height: 100%;
}

.minaimgb {
display: block;
background-color: black;
}

脚本

fileupa.onchange = e => {
target_img.src = URL.createObjectURL(fileupa.files[0]);
}

fileupb.onchange = e => {
target_img.src = URL.createObjectURL(fileupb.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;
}

最佳答案

您的代码中的问题是您在 img 标签 target_img 上使用了相同的 idID始终是唯一的。

文件上传A

 <div class="minaimga">
<!-- give an id for ease of selection -->
<img id="target_imga" alt="">

<div></div>

</div>

文件上传B:

<div class="minaimgb">
<!-- give an id for ease of selection -->
<img id="target_imgb" alt="">

<div></div>

</div>

Working Demo

fileupa.onchange = e => {
target_imga.src = URL.createObjectURL(fileupa.files[0]);
}

fileupb.onchange = e => {
target_imgb.src = URL.createObjectURL(fileupb.files[0]);
}

你可以给.minaimgb,.minaimgb这两个类赋值height

 .minaimga,.minaimgb {
display: block;
background-color: black;
height:500px;
}

关于javascript - 多次使用时不显示蒙版图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54532724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com