码迷,mamicode.com
首页 > 其他好文 > 详细

imgareaselect插件

时间:2015-04-02 13:03:40      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:

1、插件介绍

imgareaselect 是一个 允许用户使用简单、直观的点击、拖动界面图像选择矩形区域的jQuery插件。该插件可用于 web 应用程序中轻松实现图像裁剪功能,以及其他功能,如照片标记、 图像编辑功能,等等。
2、基本用法(简单实例)

首先先调用imgareaselect-default.css、jquery-1.7.1.min.js、imgareaselect.pack.js这三个文件(官网下载的)

官网:http://odyniec.net/projects/imgareaselect/

<!--html-->
<
div class="frame" style="margin: 0 0.3em; width: 500px; height: 467px;float:left;border:2px solid #dcdcdc;">   <img id="photo" src="../Images/img3.jpg" style="max-width:100%;max-height:100%;" /> </div> <div id="preview" style="width: 250px; height: 200px; overflow: hidden;border:2px solid #dcdcdc;">   <img src="../Images/img3.jpg" style="width: 250px; height: 200px;" /> </div>

 

function preview(img, selection) {
        if (!selection.width || !selection.height)
            return;

        var scaleX = 250 / selection.width;
        var scaleY = 200 / selection.height;
        var hei = $(‘#photo‘).height();
        var wid = $(‘#photo‘).width();
        $(‘#preview img‘).css({
            width: Math.round(scaleX * wid),
            height: Math.round(scaleY * hei),
            marginLeft: -Math.round(scaleX * selection.x1),
            marginTop: -Math.round(scaleY * selection.y1)
        });
    }

    $(function () {
        $(‘#photo‘).imgAreaSelect({
            //aspectRatio: ‘1:1‘, 
            handles: true,
            fadeSpeed: 200, onSelectChange: preview
        });
    });

如果在jQuery对象里有不止有一个img元素,那么这个插件会对里面的元素逐个应用此方法。其实这个方法不仅仅应用于img元素,它支持任何块级元素(比如有图像背景的div元素)

实例效果图:

技术分享

3、实例解析

效果图是通过onSelectChange()回调函数实现选择区域预览的效果。由于预览窗口是250x200px的,因此当截图窗口小于250px时,预览图会放大;当截图窗口大于250px时,预览图会缩小。

在preview()函数中,首先就定义了scaleX与scaleY,它们的内容就是250/selection.width(或height) ,也就是当selection.width小于250时,这个因子起放大作用,反之起缩小作用。

特别值得注意:回调函数中实际图的宽高(要根据实际情况调整!),回调函数中新图的宽高这些参数必须设置正确、否则会出现 选择偏差。(因为我自己的宽高是不定的,所以我是获取的)

 

imgareaselect插件

标签:

原文地址:http://www.cnblogs.com/cjqa/p/4386489.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!