裁剪图像 多个部分 jQuery areaSelect.js

  • 源码大小:122.41KB
  • 所需积分:1积分
  • 源码编号:19JP-3512
  • 浏览次数:627次
  • 最后更新:2023年06月21日
  • 所属栏目:其他
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

areaSelect.js是一个小型jQuery图像裁剪插件,允许您选择图像的多个区域并同时裁剪它们。具有实时预览支持,并能够将其输出为JSON以保存为目的。

如何使用它:

1.加载jquery.areaSelect.js查询在jQuery之后。

  1. <script src="/path/to/cdn/jquery.min.js"></script>
  2. <script src="/path/to/jquery.areaSelect.js"></script>

2.在文档中嵌入的图像上初始化插件。

  1. <img src="example.jpg" alt="Image Alt" />
  1. $('img').on('load',function () {
  2. $(this).areaSelect({
  3. // options here
  4. });
  5. });

3.指定预先选择的区域。

  1. $('img').on('load',function () {
  2. $(this).areaSelect({
  3. initAreas: [
  4. {"x": 280, "y": 93, "width": 50, "height": 50},
  5. {"x": 309, "y": 195, "width": 183, "height": 386},
  6. {"x": 298, "y": 5, "width": 45, "height": 55}
  7. ]
  8. });
  9. });

4.在页面上显示裁剪的图像。

  1. <div id="preview">
  2. </div>
  1. function showPreview(areas) {
  2. var $preview = $('#preview');
  3. $preview.empty();
  4. for (var index in areas) {
  5. var area = areas[index];
  6. var $img = $('<div/>').css({
  7. 'height': area.height,
  8. 'display': 'inline-block',
  9. 'width': area.width,
  10. 'margin': '10px',
  11. 'background-image': 'url("example.jpg")',
  12. 'background-position': -area.x + 'px ' + (-area.y + 'px')
  13. });
  14. $preview.append($img);
  15. }
  16. }
  17. showPreview($('img').areaSelect('get'));
  18. $('img').areaSelect('bindChangeEvent', function (event, data) {
  19. showPreview(data.areas);
  20. });

5.获取所选区域的数据。

  1. JSON.stringify($('img').areaSelect('get'))

6.自定义选择框的样式。

  1. $('img').on('load',function () {
  2. $(this).areaSelect({
  3. padding: 3,
  4. area: {strokeStyle: 'red', lineWidth: 2},
  5. point: {size: 3, fillStyle: 'black'}
  6. });
  7. });

7.自定义允许您删除当前选择的事件。默认值:“点击”。

  1. $('img').on('load',function () {
  2. $(this).areaSelect({
  3. deleteMethod: 'doubleClick',
  4. });
  5. });

预览截图