Jcrop(github) 是一個裁切圖片介面的 jQuery plugin.

安裝
可以直接下載檔案或透過 bower 安裝

1bower install Jcrop

使用範例
HTML

1<script src="/bower_components/jquery/dist/jquery.min.js"></script>
2<script src="/bower_components/jcrop/js/jquery.Jcrop.min.js"></script>
3<img class="crop-img" src="test.jpg">

javascript

1$(function () {
2    $('.crop-img').Jcrop({
3        onChange: showCoords,
4        onSelect: showCoords,
5        onRelease: clearCoords,
6        aspectRatio: 1
7    });
8 
9    var showCoords = function(c) {
10        // 圖片縮放比例()
11        var narrow_ratio = 1;      
12 
13        // 座標
14        var crop_x1 = c.x * narrow_ratio;
15        var crop_y1 = c.y * narrow_ratio;
16        var crop_x2 = c.x2 * narrow_ratio;
17        var crop_y2 = c.y2 * narrow_ratio;
18        var crop_w = c.w * narrow_ratio;
19        var crop_h = c.h * narrow_ratio;
20 
21        // 確認不會超出原圖大小
22        crop_w = (crop_w > src_w) ? src_w : crop_w;
23        crop_h = (crop_h > src_h) ? src_h : crop_h;
24 
25        if (crop_w !== crop_h && crop_h > crop_w) {
26            crop_h = crop_w;
27        } else if (crop_w !== crop_h && crop_w > crop_h) {
28            crop_w = crop_h;
29        }
30 
31        // Do Something
32    };
33 
34    var clearCoords = function() {
35        // Clear
36    };
37});
Categories: jQuery