如何在Imgareaselect中选择图像根据像素进行选择
How we can select image in Imgareaselect according pixels
我是jQuery的新手。我想根据145x190、140X180等像素来选择图像。我可以按比例来做,但不能按像素或尺寸来做。请帮帮我。我的代码是:
function preview(img, selection) {
var scaleX = 100 / (selection.width || 1);
var scaleY = 100 / (selection.height || 1);
$('#ferret + div > img').css({
width: Math.round(scaleX * 400) + 'px',
height: Math.round(scaleY * 300) + 'px',
marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
marginTop: '-' + Math.round(scaleY * selection.y1) + 'px',
});
}
$(document).ready(function () {
$('<div> <img src="http://jotform.org/demo/jquery-image-area-select-plugin/images/sweet-dogs.jpg" style="position: relative;"> </div>')
.css({
float: 'right',
position: 'relative',
overflow: 'hidden',
width: '100px',
height: '100px'
})
.insertAfter($('#ferret'));
$('#ferret').imgAreaSelect({ aspectRatio: '1:1', onSelectChange: preview ,minWidth: 100,minHeight: 100});
});
希望你能理解我的问题。。。
抱歉太草率了,有点累。
如果您使用下面的代码,它与我上面的示例相同,只是您可以通过文本框动态更改宽度和高度的值:
HTML
<div class="container">
<div>
<input id="thumbWidth" placeholder="width" value="200" />
<input id="thumbHeight" placeholder="height" value="200" />
</div>
<p>
<img id="ferret" src="http://odyniec.net/projects/imgareaselect/ferret.jpg" alt="It's coming right for us!" title="It's coming right for us!" style="float: left; margin-right: 10px;" />
</p>
</div>
JAVASCRIPT
$(document).ready(function () {
var defaultVal = 100;
var thumbWidth = $('#thumbWidth').val() | defaultVal,
thumbHeight = $('#thumbHeight').val() | defaultVal;
$('#thumbWidth').on("change keyup", function () {
thumbWidth = this.value | defaultVal;
reLoad('cancel');
});
$('#thumbHeight').on("change keyup", function () {
thumbHeight = this.value | defaultVal;
reLoad('cancel');
});
function preview(img, selection) {
var scaleX = thumbWidth / (selection.width || 1);
var scaleY = thumbHeight / (selection.height || 1);
$('#ferret + div > img').css({
width: Math.round(scaleX * $("#ferret").width()) + 'px',
height: Math.round(scaleY * $("#ferret").height()) + 'px',
marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
});
}
var reLoad = function (cancel) {
$('.premove').remove();
$('<div class="premove"><img src="http://odyniec.net/projects/imgareaselect/ferret.jpg" style="position: relative;" /><div>')
.css({
float: 'left',
position: 'relative',
overflow: 'hidden',
width: thumbWidth + 'px',
height: thumbHeight + 'px'
})
.insertAfter($('#ferret'));
var fer = $('#ferret').imgAreaSelect({
aspectRatio: thumbWidth + ':' + thumbHeight,
onSelectChange: preview,
instance: true
});
if (cancel) fer.cancelSelection();
}
reLoad();
});
如果您想从图像中进行选择以创建自定义预览图像,请尝试我的fiddle。只需进入javascript部分并更改thumbWidth
和thumbHeight
的值。基本上,它会将预览图像调整为您想要的尺寸,并将该比例用作aspectRatio
。
Java脚本代码
var thumbWidth = 200, thumbHeight = 200;
function preview(img, selection) {
var scaleX = thumbWidth / (selection.width || 1);
var scaleY = thumbHeight / (selection.height || 1);
$('#ferret + div > img').css({
width: Math.round(scaleX * $("#ferret").width()) + 'px',
height: Math.round(scaleY * $("#ferret").height()) + 'px',
marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
});
}
$(document).ready(function () {
$('<div><img src="http://odyniec.net/projects/imgareaselect/ferret.jpg" style="position: relative;" /><div>')
.css({
float: 'left',
position: 'relative',
overflow: 'hidden',
width: thumbWidth + 'px',
height: thumbHeight + 'px'
})
.insertAfter($('#ferret'));
$('#ferret').imgAreaSelect({aspectRatio: thumbWidth+':'+thumbHeight, onSelectChange: preview });
});
相关文章:
- 如何通过溢出来判断元素被切断了多少像素:隐藏在父级上
- 点击facebook像素跟踪注册(JS/JQUERY代码)
- 点击(右键点击)使用传单地图库获取图像覆盖的像素坐标
- 如何使线条的边缘像素保持半透明
- 画布:逐像素绘制图像并请求动画帧计时
- 如何's css标记"-webkit最小设备像素比”;在Javascript中实现
- javascript mootools平滑滚动'x'像素数量
- 逐像素碰撞检测弹球
- 获取图像的平均外部像素颜色
- 如何根据分辨率添加像素
- 仅当窗口宽度>940像素,无论是通过页面加载还是调整大小
- 使用画布在秒内逐像素显示图片
- JS/CSS:如何在向下滚动超过1200像素(高度)后更改z索引值
- 获取以屏幕像素为单位的旋转SVG元素的边界
- 如何将像素添加到元素的当前位置
- 如何在Imgareaselect中选择图像根据像素进行选择
- 从FF和谷歌浏览器的像素坐标中选择
- 在鼠标选择时捕获像素曲面
- 允许用户在网页上显示的图像中动态选择像素框
- 使用asp.net MVC在图片上选择一个或多个像素