Javascript 如何使用用户选择缩放图像
我有一张300x300的图片:Javascript 如何使用用户选择缩放图像,javascript,html,css,Javascript,Html,Css,我有一张300x300的图片: <style> #profileImage, #profilePP { max-width: 300px; max-height: 300px; min-width: 50px; min-height: 50px; } #profileBox { width:300px; height:300px; overflow:hi
<style>
#profileImage, #profilePP {
max-width: 300px;
max-height: 300px;
min-width: 50px;
min-height: 50px;
}
#profileBox {
width:300px;
height:300px;
overflow:hidden;
}
</style>
<!-- html extract -->
<div id="profileBox">
<img src="check.png'" id="profileImage">
</div>
好的,我想我用下面的方法解决了这个问题,还请记住,如果用户选择减少得太少,最终将x或y值推到零,则可能会出现溢出。解决这个问题的方法是确保最小选择在溢出之前被封顶
var constHeight=parseInt($("#profileBox").css("width"));
var proportion=constHeight/ (selection.y2 - selection.y1);
var maxHeight=constHeight* proportion;
var maxWidth=constHeight* proportion;
var moveX=selection.x1*proportion;
var moveY=selection.y1*proportion;
$("#profileImage").attr("src",$("#profilePP").attr("src"));
$("#profileImage").css("margin-top","-"+moveY+"px");
$("#profileImage").css("margin-left","-"+moveX+"px");
$("#profileImage").css("max-height",maxHeight+"px");
$("#profileImage").css("max-width",maxWidth+"px");
好的,我想我用下面的方法解决了这个问题,还请记住,如果用户选择减少得太少,最终将x或y值推到零,则可能会出现溢出。解决这个问题的方法是确保最小选择在溢出之前被封顶
var constHeight=parseInt($("#profileBox").css("width"));
var proportion=constHeight/ (selection.y2 - selection.y1);
var maxHeight=constHeight* proportion;
var maxWidth=constHeight* proportion;
var moveX=selection.x1*proportion;
var moveY=selection.y1*proportion;
$("#profileImage").attr("src",$("#profilePP").attr("src"));
$("#profileImage").css("margin-top","-"+moveY+"px");
$("#profileImage").css("margin-left","-"+moveX+"px");
$("#profileImage").css("max-height",maxHeight+"px");
$("#profileImage").css("max-width",maxWidth+"px");
到目前为止你试过什么?帮助我们帮助你如果可以的话,还可以设置一个JSFIDLE,这样我们就可以看到页面的样子了!如果您知道尺寸,则图像的宽度为100%且容器的高度和宽度为固定值,则图像将扩展到
div的宽度,并且图像将上下缩放。很难导出代码,因为其中包含大量内容中交织的服务器代码。不过,我基本上是在尝试与下面的实例相同,但是它可以是一个宽度和高度可变的用户定义图像,而不是一个设置图像…我有点困惑。您是在询问上传后编辑图像的代码,还是在上传后如何显示图像?在用户做出选择后。我想知道如何使用正确的选择填充300x300图像,并正确缩放,以仅显示用户选择的图像部分。请注意,如果用户使框变小,则300 x 300图像只需反映图像的该部分。如上面的链接示例所示,到目前为止您尝试了什么?帮助我们帮助你如果可以的话,还可以设置一个JSFIDLE,这样我们就可以看到页面的样子了!如果您知道尺寸,则图像的宽度为100%
且容器的高度和宽度为固定值,则图像将扩展到div的宽度,并且图像将上下缩放。很难导出代码,因为其中包含大量内容中交织的服务器代码。不过,我基本上是在尝试与下面的实例相同,但是它可以是一个宽度和高度可变的用户定义图像,而不是一个设置图像…我有点困惑。您是在询问上传后编辑图像的代码,还是在上传后如何显示图像?在用户做出选择后。我想知道如何使用正确的选择填充300x300图像,并正确缩放,以仅显示用户选择的图像部分。请注意,如果用户使框变小,则300 x 300图像只需反映图像的该部分。如上面的链接示例所示