cropper初始化_Cropper

本文档详细介绍了如何使用Cropper.js库进行图片裁剪的初始化、设置裁切比例、移动、缩放、旋转、翻转、启用、禁用、重置、清除等操作,并提供了上传图片、替换图片、获取裁剪数据以及提交裁剪图片到后台的完整流程。通过实例代码展示,帮助开发者快速掌握Cropper.js的使用。
摘要由CSDN通过智能技术生成

//main.js

$(function() {'use strict';var $image = $('#image');var $download = $('#download');var $dataX = $('#dataX');var $dataY = $('#dataY');var $dataHeight = $('#dataHeight');var $dataWidth = $('#dataWidth');var $dataRotate = $('#dataRotate');var $dataScaleX = $('#dataScaleX');var $dataScaleY = $('#dataScaleY');var $dataWH = $('#dataWH');var options ={

aspectRatio:4 / 3,//设置裁切框的宽高比。默认情况下,裁剪框是自由比例。

preview: '.img-preview',

crop:function(e) {

$dataX.html(Math.round(e.x));

$dataY.html(Math.round(e.y));

$dataHeight.html(Math.round(e.height));

$dataWidth.html(Math.round(e.width));

$dataRotate.html(e.rotate);

$dataScaleX.html(e.scaleX);

$dataScaleY.html(e.scaleY);var _$dataWH =reductionTo(Math.round(e.width), Math.round(e.height));

$dataWH.html(_$dataWH[0] + '/' + _$dataWH[1]);

}

};//初始化函数

$image.cropper(options);

$image.cropper({

built:function() {

}

});//修改裁剪比例函数

$('#ratio_container .btn').click(function(event) {

event.stopPropagation();var dataRatio = $(this).attr('data-ratio');

$image.cropper('destroy').cropper({'aspectRatio': dataRatio});

});//移动函数

$('#move_container .btn').click(function(event) {

event.stopPropagation();var dataMovex = parseInt($(this).attr('data-movex'));var dataMovey = parseInt($(this).attr('data-movey'));

$image.cropper('move', dataMovex, dataMovey)

});//移动函数

$('#move_container .btn').click(function(event) {

event.stopPropagation();var dataMovex = parseInt($(this).attr('data-movex'));var dataMovey = parseInt($(this).attr('data-movey'));

$image.cropper('move', dataMovex, dataMovey)

});//Keyboard

$(document.body).on('keydown', function(e) {if (!$image.data('cropper') || this.scrollTop > 300) {return;

}switch(e.which) {case 37:

e.preventDefault();

$image.cropper('move', -1, 0);break;case 38:

e.preventDefault();

$image.cropper('move', 0, -1);break;case 39:

e.preventDefault();

$image.cropper('move', 1, 0);break;case 40:

e.preventDefault();

$image.cropper('move', 0, 1);break;

}

});//放大缩小

$('#zoom_container .btn').click(function(event) {

event.stopPropagation();var dataZoom = $(this).attr('data-zoom');

$image.cropper('zoom', dataZoom);

});//旋转

$('#rotate_container .btn').click(function(event) {

event.stopPropagation();var dataRotate = $(this).attr('data-rotate');

$image.cropper('rotate', dataRotate);

});//翻转

var scalexVal = 1;var scaleyVal = 1;

$('#scale_container .btn').click(function(event) {

event.stopPropagation();var dataScale = $(this).attr('data-scale');if (dataScale == 'x') {

scalexVal= -scalexVal;

$image.cropper('scaleX', scalexVal);

}else if (dataScale == 'y') {

scaleyVal= -scaleyVal;

$image.cropper('scaleY', scaleyVal);

}

});//enable():使cropper可用。

$('#enable').click(function(event) {

event.stopPropagation();

$image.cropper('enable');

});//disable():冻结cropper。

$('#disable').click(function(event) {

event.stopPropagation();

$image.cropper('disable');

});//reset():重置剪裁区域的图片到初始状态。

$('#reset1').click(function(event) {

event.stopPropagation();

$image.cropper('crop');

$image.cropper('destroy').cropper({'preview': '.img-preview'});

});

$('#reset2').click(function(event) {

event.stopPropagation();

$image.cropper('reset');

$image.cropper('destroy').cropper({'preview': '.img-preview'});

});//clear():清空剪裁区域。

$('#clear').click(function(event) {

event.stopPropagation();

$image.cropper('clear');

});//destroy():销毁剪裁函数。

$('#destroy').click(function(event) {

event.stopPropagation();

$image.cropper('destroy');

});//上传图片

$('#upload').change(function(event) {var files = this.files;if (files &&files.length) {var file = files[0];if (/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(file.name)) {var uploadedImageURL =window.URL.createObjectURL(file);

$image.cropper('destroy').attr('src', uploadedImageURL).cropper(options);

$download.attr('download', uploadedImageURL);

$('#upload').val('');

}else{

alert('请选择正确的图片格式!');

}

}

});//修改图片地址

var imgUrl = 'images/picture.jpg';

$('#replace').click(function(event) {

event.stopPropagation();

imgUrl= (imgUrl == 'images/picture_new.jpg' ? 'images/picture.jpg' : 'images/picture_new.jpg');

$image.cropper('replace', imgUrl);

$download.attr('download', imgUrl);

});//输出裁剪好的图片

$('#getCroppedCanvas').click(function(event) {

event.stopPropagation();var imgurl = $image.cropper("getCroppedCanvas");

$("#canvas").html(imgurl);

$download.attr('href', imgurl.toDataURL());

$('.fixed-canvas').removeClass('hiddle');

});//点击取消或者下载之后

$('#modal_canvas_btn .btn').click(function(event) {

$('.fixed-canvas').addClass('hiddle');

});//获取数据

$('#getData').click(function(event) {

event.stopPropagation();var getData = $image.cropper("getData")

console.log(getData);

});//提交裁剪好的图片到后台

$('#submit').click(function(event) {var imgData = $image.cropper("getCroppedCanvas").toDataURL();//console.log(imgData);

$.ajax({

url:'',

dataType:'json',

type:"POST",

data: {"image": imgData},

success:function() {

console.log('Upload success');

},

error:function() {

console.log('Upload error');

}

});

});

});functiondestory() {

$image.cropper('destroy').cropper(options);

}//m,n为正整数的分子和分母

functionreductionTo(m, n) {var arr =[];if (!isInteger(m) || !isInteger(n)) {//console.log('m和n必须为整数');

arr[0] = 0;

arr[1] = 0;returnarr;

}else if (m <= 0 || n <= 0) {//console.log('m和n必须大于0');

arr[0] = 0;

arr[1] = 0;returnarr;

}var a =m;var b =n;

(a>= b) ? (a = m, b = n) : (a = n, b =m);if (m != 1 && n != 1) {for (var i = b; i >= 2; i--) {if (m % i == 0 && n % i == 0) {

m= m /i;

n= n /i;

}

}

}

arr[0] =m;

arr[1] =n;returnarr;

}//判断一个数是否为整数

functionisInteger(obj) {return obj % 1 === 0}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值