基于Html5 canvas实现裁剪图片和马赛克功能及又拍云上传图片 功能

2020-04-21 07:50:07易采站长站整理

this.paintRectClear(e);
}
// 裁剪
if (this.isDrop) {
var targetDiv = $("#canvas-mainBox"),
offsetArr = targetDiv.offset();
var chaX = e.pageX - this.movePrev[0],
chaY = e.pageY - this.movePrev[1],
ox = parseFloat(targetDiv.css("left")),
oy = parseFloat(targetDiv.css("top"));
targetDiv.css({
left: ox + chaX + "px",
top: oy + chaY + "px"
});
this.movePrev = [e.pageX, e.pageY];
}
},
resizeDiv(e) {
e.preventDefault();
e.stopPropagation();
// 获取需要改变尺寸元素到页面的距离
var targetDiv = $("#canvas-mainBox"),
offsetArr = targetDiv.offset();
var eleSWidth = targetDiv.width(),
eleSHeight = targetDiv.height(),
ox = parseFloat(targetDiv.css("left")),
oy = parseFloat(targetDiv.css("top"));
// 获取鼠标位置,和元素初始offset进行对比,
var chaX = e.pageX - offsetArr.left,
chaY = e.pageY - offsetArr.top;
switch (this.resizeFX) {
case 0:
//如果移动距离接近宽度或高度,则不进行改变
if (chaX >= eleSWidth - 10 || chaY >= eleSHeight - 10) {
return;
}
// 获得位置差(m-e),先设置宽度和高度,再设置位置
// 原始宽高+((m-e)*-1),原始位置+(m-e)
targetDiv.css({
width: eleSWidth + chaX * -1 + "px",
height: eleSHeight + chaY * -1 + "px",
left: ox + chaX + "px",
top: oy + chaY + "px"
});
break;
case 1:
//如果移动距离接近宽度或高度,则不进行改变
if (chaY >= eleSHeight - 10) {
return;
}
// 获得位置差(m-e),先设置宽度和高度,再设置位置
// 原始宽高+((m-e)*-1),原始位置+(m-e)
targetDiv.css({
height: eleSHeight + chaY * -1 + "px",
top: oy + chaY + "px"
});
break;
case 2:
//如果移动距离接近宽度或高度,则不进行改变
if (chaX <= 10 || chaY >= eleSHeight - 10) {
return;
}
// 获得位置差(m-e),先设置宽度和高度,设置位置
// 原始高+((m-e)*-1),原始宽+((m-e)),原始位置+(m-e)
targetDiv.css({
width: chaX + "px",
height: eleSHeight + chaY * -1 + "px",
top: oy + chaY + "px"
});
break;
case 3:
//如果移动距离接近宽度或高度,则不进行改变
if (chaX <= 10) {
return;
}
// 获得位置差(m-e),先设置宽度和高度,再设置位置
// 原始宽高+((m-e)*-1),原始位置+(m-e)
targetDiv.css({
width: chaX + "px"
});
break;
case 4:
//如果移动距离接近宽度或高度,则不进行改变
if (chaX <= 10 || chaY <= 10) {
return;
}
// 获得位置差(m-e),先设置宽度和高度,再设置位置