jquery+cropper剪切、旋转、缩放图片

news/2024/7/10 23:39:42 标签: jquery, cropper

cropper是一款使用简单且功能强大的图片剪裁jQuery插件。该图片剪裁插件支持图片放大缩小,支持图片旋转,支持触摸屏设备,支持canvas,并且支持跨浏览器使用。

效果

demo源码地址
在这里插入图片描述

cropper_7">安装cropper

cnpm i cropper --save-dev

在这里插入图片描述

cropper_14">使用cropper

使用前,我们需要先了解一下cropper 文档,以及cropper的源码

cropper_17">cropper参数源码

在这里插入图片描述

常用参数说明

名称类型及默认值说明
viewMode类型:String,默认值0。定义裁剪器的视图模式
dragMode类型:String,默认值crop。定义裁剪器的拖动模式
initialAspectRatio类型:Number,默认值NaN。定义裁剪框的初始纵横比
aspectRatio类型:Number,默认值NaN。例如 2/1,3/1,2/3 …定义裁剪框的纵横比
preview类型:String,默认值 ’ '。用于预览容器的选择器
background类型:Boolean,默认值true。是否在容器上显示网格背景
autoCropArea类型:Number,默认值0.8。定义自动剪裁区域的大小
zoomOnWheel类型:Boolean,默认值true。是否允许鼠标 缩放图片
cropBoxResizable类型:Boolean,默认值true。是否允许改变剪裁框的大小
cropBoxMovable类型:Boolean,默认值true。是是否可以移动裁剪框
rotatable类型:Boolean,默认值true。是否允许旋转图片

CDN引入

<script src="https://www.jq22.com/jquery/jquery-1.7.1.js"></script>
<script type="text/javascript" src="assets/js/cropper.js"></script>
<link rel="stylesheet" type="text/css" href="assets/css/cropper.css">

核心html代码

 <div class="popup-wrap">
    <div  class="s-upload-container">
      
      <div class="head">
        <span>上传图片</span>
        <input type="button" class="btn" id="closeBtn" value="关闭" />
      </div>
      <div class="s-upload-main">
        <!-- 上传图片 -->
        <div class="top-box">
          <span class="title">图片上传</span>
          <div class="btn file-box">
            选择文件
            <input type="file" accept="image/jpg,image/jpeg,image/png" name="file" id="file" class="btn" onchange="selectImg(this)" multiple="multiple">
          </div>
          <div class="right">预览区域</div>
        </div>
        <div class="s-upload-content">
          <!-- 裁剪区域 -->
          <div class="left-box">
            <img src="" id="imageId" alt="" class="image"> 
          </div>
          <!-- 预览区域 -->
          <div class="right-box">
            <div class="previewBox"></div>
          </div>
        </div>
        <div class="action bottom-box">
          <!-- 旋转 -->
            <div class="left">
              <input type="button" id="btnLeft"  value="向左旋转" class="btn" onclick="rotateFn('1')">
              <input type="button" id="btnRight" value="向右旋转" class="btn" onclick="rotateFn('2')">
            </div>
            <div class="right">
              <!-- 放大、缩小、还原 -->
              <input type="button" id="btnLarge" class="btn" value="放大">
              <input type="button" id="btnSmall" class="btn" value="缩小" >
              <input type="button" id="btnScale" class="btn" value="换向">
              <input type="button" id="btnInit" class="btn" value="还原">
              <!-- 保存 -->
              <input type="button" id="btnSubmit" class="btn submit" value="保存修改" >
            </div>
        </div>
      </div>	
    </div>
  </div>

事件方法

img

裁剪区域的img,必须要一个可见的块级标签包裹。

初始化

// cut_size 为裁剪的限制尺寸
function init (cut_size){
	var sizeArr = [0,0];
	var aspectRatio = null
	if(cut_size){
		sizeArr = cut_size.split('*');
		let w = sizeArr[0]*1
		var h = sizeArr[1]*1

		if(w > h){
			aspectRatio = (w/h)  / 1
		} else {
			aspectRatio = 1 / (h/w)
		}
	}


	$('#imageId').cropper({
		viewMode: 1,
		dragMode: 'none',
		initialAspectRatio: 1,
		preview: '.previewBox',
		// 是否在容器上显示网格背景
		background: true,
		// 定义自动剪裁区域的大小
		// autoCropArea: 1,
		// 是否允许鼠标 缩放图片
		zoomOnWheel: false,
		// 是否允许改变剪裁框的大小
		cropBoxResizable:true,
		// 是否可以移动裁剪框
		cropBoxMovable:true,
		// 是否允许旋转图片
		rotatable: true,
		aspectRatio:aspectRatio
	})
}

rotate 旋转图像

// 角度
var deg = 0;
//向左旋转
$("#btnLeft").on("click",function () {
	rotateFn(1)
});
//向右旋转
$("#btnRight").on("click",function () {
	rotateFn(2)
});
// 旋转
function rotateFn (type){
	if(type ==1){
		deg += 90
	} else {
		deg -= 90
	}

	$('#imageId').cropper("rotate", deg);
}

zoom 放大、缩小图像

// 放大
$("#btnLarge").on("click",function () {
	$('#imageId').cropper("zoom",0.1);
});

// 缩小
$("#btnSmall").on("click",function () {
	$('#imageId').cropper("zoom",-0.1);
});

scaleX 翻转图像

	var flagX = true;
	$("#btnScale").on("click",function () {
		if(flagX){
			$('#imageId').cropper("scaleX", -1);
			flagX = false;
		}else{
			$('#imageId').cropper("scaleX", 1);
			flagX = true;
		}
		flagX != flagX;
	});

reset 还原裁剪区域

$("#btnInit").on("click",function () {
	$('#imageId').cropper("reset");
});

cropper_196">destroy 销毁cropper

$('#imageId').cropper("destroy");

http://www.niftyadmin.cn/n/1558074.html

相关文章

view围绕圆心自转

创建一个image UIImageView *imgView [[UIImageView alloc] initWithFrame:CGRectMake(40, 50, 200, 200)];imgView.image [UIImage imageNamed:"image"];[self.view addSubview:imgView]; 创建一个基础动画CABasicAnimation *animation [CABasicAnimation new]; …

vue + xlsx 动态生成Excel,合并单元格

根据数据动态生成一个Excel导出文件&#xff0c;这个公共相对简单一些&#xff0c;引入 xlsx就可以了。 但是根据需求&#xff0c;动态合并单元格&#xff0c;貌似难度不小&#xff0c;这里需要对 xlsx的属性和方法有一定的了解才可以&#xff0c;那么一起学习一下。 安装 xl…

四:RF框架appium工具之xpath定位

XPATH定位方法具体的学会&#xff0c;还是在今年1月份&#xff0c;以前运用的都不熟练。 这个定位神器是一定要掌握的&#xff0c;不然有你抓狂的时候。 第一要掌握它的书写格式&#xff0c;这个好上手。 第二要掌握它的具体用法。这个就要多练习了&#xff0c;没有别的方法和途…

hihoCoder1097 (Prim算法)

#1097 : 最小生成树一Prim算法 时间限制:10000ms单点时限:1000ms内存限制:256MB描述 最近&#xff0c;小Hi很喜欢玩的一款游戏模拟城市开放出了新Mod&#xff0c;在这个Mod中&#xff0c;玩家可以拥有不止一个城市了&#xff01; 但是&#xff0c;问题也接踵而来——小Hi现在手…

vue.config.js build打包UglifyJsPlugin清除控制台打印console.log,报错出现`warnings` is not a supported option

前端在开发过程中&#xff0c;为了调试方便经常会用到console.log&#xff0c;开发、测试环境有打印可以&#xff0c;但是生产环境最好不要有&#xff0c;毕竟不友好、也不太美观。 uglifyjs-webpack-plugin 上线到生产环境&#xff0c;我们希望把打印功能全部注销掉&#xf…

php连接sql server 2008失败报错This extension requires the Microsoft SQL Server 2012...

2019独角兽企业重金招聘Python工程师标准>>> php连接sql server 2008失败 This extension requires the Microsoft SQL Server 2012 今天在使用php链接sql server 2008的时候&#xff0c;报错&#xff0c;具体报错如下&#xff1a; This extension requires the Mi…

百度地图--3D棱柱效果---省边界数据抽取---Boundary、Point、Prism

3D棱柱效果 上代码 var map new BMapGL.Map("allmap");var point new BMapGL.Point(116.404, 39.925);map.centerAndZoom(point, 10);map.setTilt(50);map.enableScrollWheelZoom();var bd new BMapGL.Boundary();bd.get(北京市, function (rs) {var count rs.bo…

SpringBoot + Spring Batch

2019独角兽企业重金招聘Python工程师标准>>> 前言&#xff1a; 日常开发中&#xff0c;业务系统会有定期扫描大量数据&#xff0c;并对数据进行处理后入库、或着短信通知用户。比如用户在云平台上购买了服务&#xff0c;在服务到期前、到期后短信通知用户&#xff0…