ImageAreaSelect:预先选择最大的缩略图
ImageAreaSelect: preselect biggest thumbnail possible respecting aspectRatio
我是这样尝试的:
$('#thumbnail').imgAreaSelect({
x1: 5,
y1: 5,
x2: $('#thumbnail').width(),
y2: $('#thumbnail').width()*0.66,
aspectRatio: '1:0.66'
});
但是一些裁剪后的图像会远离溢出…
这似乎对我尝试的大多数图像分辨率进行了预选…
var thwidth = $('#thumbnail').width();
var thheight = $('#thumbnail').height();
aspectRatio = 0.66;
$('#thumbnail').imgAreaSelect({
x1: 5,
y1: 5,
x2: thwidth - 80,
y2: (thwidth - 80) * aspectRatio,
aspectRatio: '1:0.66'
});
但是它不会选择最大可能;而且在我看来有点脏……
我如何选择最大的(居中,如果可能的话)宽度/高度坐标尊重长宽比?(在本例中:1:0.66)
试试这个代码
var selWidth = 500;
var photo = $('#photo'),
photoWidth = parseInt($('#photo').width()),
maxWidth = Math.min(selWidth, photoWidth)
aspectRatio = 0.66,
maxHeight = maxWidth * aspectRatio,
yTop = parseInt(photo.height()) / 2 - maxHeight / 2;
$('img#photo').imgAreaSelect({
x1: photoWidth / 2 - maxWidth / 2,
y1: yTop,
x2: (photoWidth / 2 - maxWidth / 2) + maxWidth,
y2: yTop + maxHeight
});
这段代码创建了一个居中的选择区域,具有给定的长宽比和最大宽度。如果max width超过图像的宽度,则使用图像的宽度作为max width。请注意,它的工作与jquery 1.8.3,我认为这是因为imageareaselect插件不兼容最新的jquery版本(我不确定虽然)。
更新我已经改进了代码,包括高度溢出和aspectRatio> 1的情况。我希望这在所有情况下都有效:)
var selWidth = 350;
var photo = $('#photo'),
photoWidth = parseInt($('#photo').width()),
photoHeight = parseInt($('#photo').height()),
maxWidth = Math.min(selWidth, photoWidth),
aspectRatio = 0.66,
maxHeight = maxWidth * aspectRatio;
if (maxHeight > photoHeight) {
maxHeight = photoHeight;
maxWidth = maxHeight * ( 1 / aspectRatio);
}
var yTop = photoHeight / 2 - maxHeight / 2;
$('img#photo').imgAreaSelect({
x1: photoWidth / 2 - maxWidth / 2,
y1: yTop,
x2: (photoWidth / 2 - maxWidth / 2) + maxWidth,
y2: yTop + maxHeight
});
我准备了这个小提琴。我花的时间比jay长一点,但我做了一个视觉演示。
计算完拇指的位置和宽度后,你可以称之为简单的。(使用变量名)
$('#thumbnail').imgAreaSelect({
x1: thumbX,
y1: thumbY,
x2: thumbX+thumbW,
y2: thumbY+thumbH,
});
相关文章:
- 下拉选择可自动更改第二个下拉选择
- 如何使用jQuery选择下拉列表的值
- jquery点击函数select&取消选择
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 按照选项卡索引的顺序循环一个jQuery选择
- 在动态创建的元素上获取对特定选择器的引用
- AngularJS-在JSON选择器中使用变量名
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 获取选择框的状态
- d3基于用户选择动态更新节点
- 提交后保留下拉选择的值
- JQuery对动态创建的对象进行选择
- 不应选择缩略图优先图像
- 生成由文件输入在特定时间选择的视频文件的缩略图/快照
- 显示从文件选择器中选择的图像的图像缩略图
- MVC - 在“选择缩略图”上,显示数据库中的数据
- 修复bx_slider自动播放后选择缩略图
- ImageAreaSelect:预先选择最大的缩略图
- 如何在喜欢按钮上选择特定的缩略图照片
- 选择时弹出的缩略图(javascript)