npm install cropperjs --save
import Cropper from "cropperjs";
-------------------------html-----------------------------------------------------
------------------------------export default----------------------------------
data() {
return {
cropper: {},
filename: "",
initParam: {
fileType: 'recruit', // 其他上传参数
uploadURL: '', // 上传地址
scale: 1 // 相对手机屏幕放大的倍数: 4倍
},
IsCropper:false,
picDeg:0,//裁剪旋转角度
}
},
mounted() {
var size = 16/10
this.init(size);
},
methods:{
// 初始化裁剪插件
init(size) {
let cropperImg = this.$refs["cropperImg"];
this.cropper = new Cropper(cropperImg, {
aspectRatio: size,
cropBoxResizable: false,
center: false,
guides: false,
cropBoxMovable: false,
autoCropArea: 0.8,
rotatable:true,
dragMode: "move",
viewMode: 1
});
},
//图片旋转
rotateFun(){
this.picDeg+=90
this.cropper.rotateTo(this.picDeg);
},
// 上传触发
uploadChange(e) {
var num = e.target.attributes.num.value
this.imgnum = num
let file = e.target.files[0];
this.filename = file["name"];
let URL = window.URL || window.webkitURL;
this.cropper.replace(URL.createObjectURL(file));
this.$refs["layer"].style.display = "block";
},
// 取消上传
cancelHandle() {
this.picDeg=0
this.cropper.reset();
this.$refs["layer"].style.display = "none";
this.$refs["file"].value = "";
},
// 确定上传
confirmHandle() {
var that = this;
let cropBox = this.cropper.getCropBoxData();
let scale = this.initParam["scale"] || 1;
let cropCanvas = this.cropper.getCroppedCanvas({
width: cropBox.width * 2,
height: cropBox.height * 2
});
let imgData = cropCanvas.toDataURL();
let formData = new FormData();
formData.append("token", this.GLOBAL.appToken);
formData.append("version", this.GLOBAL.version);
formData.append("os", this.GLOBAL.os);
formData.append("file", imgData);
//console.log(imgData)
that.cancelHandle();
this.picDeg=0
},
}
-----------------style------------------------------------------
.file {
display: none;
}
.v-cropper-layer {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: #fff;
z-index: 99999;
display: none;
}
.layer-header {
position: fixed;
top: 0;
left: 0;
background: #1a2543;
width: 100%;
height: 1rem;
/* padding: 0 0.2rem; */
z-index: 99999;
box-sizing: border-box;
display: flex;
align-items:center;
}
.cancel,
.confirm,
.centerb {
line-height: 1rem;
font-size: 0.3rem;
background: inherit;
border: 0;
outline: 0;
color: #fff;
display:block;
width:100%;
}
.cancel{
text-align: left;
padding-left: .3rem;
}
.centerb{
text-align: center;
}
.confirm {
text-align: right;
padding-right: .3rem;
}
.cropper-img{
height: 4rem;
position: relative;
/* background: #290877; */
}
.cropper-mask{
height: 5rem;
position: absolute;
top: 0;
left: 0;
right: 0;
background-repeat: no-repeat;
background-position: center center;
/* background-size: 100%; */
}
.cropper-img img{
position: absolute;
width: 2.4rem;
height: 2.4rem;
border-radius: 50%;
top: 95%;
left: 50%;
margin-left: -1.2rem;
margin-top: -.7rem;
border:3px solid #fff;
}
.cropper-img button{
position: absolute;
width: 3rem;
height: 3rem;
border-radius: 50%;
top: 50%;
left: 50%;
margin-left: -1.5rem;
margin-top: -1.5rem;
opacity: 0;
}
.cropper-btn{
padding: .5rem 0;
margin-top: 1.5rem;
}
.cropper-btn button{
font-size: .3rem;
display: block;
width: 6rem;
height: .8rem;
border: 1px solid #eee;
margin: .4rem auto;
border-radius: 2px;
}
.cropper-btn button:nth-child(2){
background: #1f3e82;
border: 1px solid #1f3e82;
color: #fff;
}
.cropper-btn button:nth-child(2):active{
background: #3158ad;
border: 1px solid #3158ad;
}
.v-simple-cropper .cropper-wrap-box {
background-color: #000;
}