提交 a8b1baa4 编写于 作者: DCloud_JSON's avatar DCloud_JSON

修复微信小程序端裁剪图片问题

上级 827afca9
/**
* h5网络地址转base64
*/
export function pathToBase64(path) {
// #ifdef H5
return new Promise((resolve, reject) => {
const _fileReader = (blob) => {
const fileReader = new FileReader();
fileReader.onload = (e) => {
resolve(e.target.result);
};
fileReader.readAsDataURL(blob);
fileReader.onerror = (error) => {
console.error('blobToBase64 error:', JSON.stringify(error))
reject(new Error('blobToBase64 error'));
};
}
if (/^(http|\/\/)/.test(path) && typeof FileReader === 'function') {
window.URL = window.URL || window.webkitURL;
const xhr = new XMLHttpRequest();
xhr.open("get", path, true);
xhr.timeout = 2000;
xhr.responseType = "blob";
xhr.onload = function() {
if (this.status == 200) {
_fileReader(this.response)
}
}
xhr.send();
}
})
// #endif
}
/** /**
* 判断手指触摸位置 * 判断手指触摸位置
*/ */
export function determineDirection(cutX, cutY, clipWidth, clipHeight, currentX, currentY) { export function determineDirection(clipX, clipY, clipWidth, clipHeight, currentX, currentY) {
const EXPAND_SIZE = 24; /*
* (右下>>1 右上>>2 左上>>3 左下>>4)
let leftx1 = cutX - EXPAND_SIZE; */
let leftx2 = cutX + EXPAND_SIZE;
let topy1 = cutY - EXPAND_SIZE;
let topy2 = cutY + EXPAND_SIZE;
let rightx1 = cutX + clipWidth - EXPAND_SIZE;
let rightx2 = cutX + clipWidth + EXPAND_SIZE;
let bottomy1 = cutY + clipHeight - EXPAND_SIZE;
let bottomy2 = cutY + clipHeight + EXPAND_SIZE;
let corner; let corner;
const isRight = currentX > rightx1 && currentX < rightx2; /**
const isLeft = currentX > leftx1 && currentX < leftx2; * 思路:(利用直角坐标系)
const isBottom = currentY > bottomy1 && currentY < bottomy2; * 1.找出裁剪框中心点
const isTop = currentY > topy1 && currentY < topy2; * 2.如点击坐标在上方点与左方点区域内,则点击为左上角
if (isRight && isBottom) { * 3.如点击坐标在下方点与右方点区域内,则点击为右下角
corner = 1; * 4.其他角同理
} else if (isRight && isTop) { */
corner = 2; const mainPoint = [clipX + clipWidth / 2, clipY + clipHeight / 2]; // 中心点
} else if (isLeft && isTop) { const currentPoint = [currentX, currentY]; // 触摸点
corner = 3;
} else if (isLeft && isBottom) { if (currentPoint[0] <= mainPoint[0] && currentPoint[1] <= mainPoint[1]) {
corner = 4; corner = 3; // 左上
} else if (currentPoint[0] >= mainPoint[0] && currentPoint[1] <= mainPoint[1]) {
corner = 2; // 右上
} else if (currentPoint[0] <= mainPoint[0] && currentPoint[1] >= mainPoint[1]) {
corner = 4; // 左下
} else if (currentPoint[0] >= mainPoint[0] && currentPoint[1] >= mainPoint[1]) {
corner = 1; // 右下
} }
return corner; return corner;
} }
...@@ -71,31 +33,32 @@ export function determineDirection(cutX, cutY, clipWidth, clipHeight, currentX, ...@@ -71,31 +33,32 @@ export function determineDirection(cutX, cutY, clipWidth, clipHeight, currentX,
* 图片边缘检测检测时,计算图片偏移量 * 图片边缘检测检测时,计算图片偏移量
*/ */
export function calcImageOffset(data, scale) { export function calcImageOffset(data, scale) {
let { let left = data.imageLeft;
imageLeft: left, let top = data.imageTop;
imageTop: top,
imageWidth,
imageHeight,
cutX,
clipWidth,
cutY,
clipHeight
} = data
scale = scale || data.scale; scale = scale || data.scale;
let imageWidth = data.imageWidth;
let imageHeight = data.imageHeight;
if ((data.angle / 90) % 2) { if ((data.angle / 90) % 2) {
imageWidth = mageHeight; imageWidth = data.imageHeight;
imageHeight = mageWidth; imageHeight = data.imageWidth;
} }
const {
clipX,
clipWidth,
clipY,
clipHeight
} = data;
// 当前图片宽度/高度 // 当前图片宽度/高度
const currentImageSize = (size) => (size * scale) / 2; const currentImageSize = (size) => (size * scale) / 2;
const currentImageWidth = currentImageSize(imageWidth); const currentImageWidth = currentImageSize(imageWidth);
const currentImageHeight = currentImageSize(imageHeight); const currentImageHeight = currentImageSize(imageHeight);
left = cutX + currentImageWidth >= left ? left : cutX + currentImageWidth; left = clipX + currentImageWidth >= left ? left : clipX + currentImageWidth;
left = cutX + clipWidth - currentImageWidth <= left ? left : cutX + clipWidth - currentImageWidth; left = clipX + clipWidth - currentImageWidth <= left ? left : clipX + clipWidth - currentImageWidth;
top = cutY + currentImageHeight >= top ? top : cutY + currentImageHeight; top = clipY + currentImageHeight >= top ? top : clipY + currentImageHeight;
top = cutY + clipHeight - currentImageHeight <= top ? top : cutY + clipHeight - currentImageHeight; top = clipY + clipHeight - currentImageHeight <= top ? top : clipY + clipHeight - currentImageHeight;
return { return {
left, left,
top, top,
...@@ -138,15 +101,15 @@ export function calcImageSize(width, height, data) { ...@@ -138,15 +101,15 @@ export function calcImageSize(width, height, data) {
clipWidth, clipWidth,
clipHeight, clipHeight,
sysinfo, sysinfo,
width: oWidth, width: originWidth,
height: oHeight height: originHeight
} = data } = data
if (imageWidth && imageHeight) { if (imageWidth && imageHeight) {
if (imageWidth / imageHeight > (clipWidth || oWidth) / (clipWidth || oHeight)) { if (imageWidth / imageHeight > (clipWidth || originWidth) / (clipWidth || originHeight)) {
imageHeight = clipHeight || oHeight; imageHeight = clipHeight || originHeight;
imageWidth = (width / height) * imageHeight; imageWidth = (width / height) * imageHeight;
} else { } else {
imageWidth = clipWidth || oWidth; imageWidth = clipWidth || originWidth;
imageHeight = (height / width) * imageWidth; imageHeight = (height / width) * imageWidth;
} }
} else { } else {
...@@ -174,15 +137,13 @@ export function clipTouchMoveOfCalculate(data, event) { ...@@ -174,15 +137,13 @@ export function clipTouchMoveOfCalculate(data, event) {
const clientX = event.touches[0].clientX; const clientX = event.touches[0].clientX;
const clientY = event.touches[0].clientY; const clientY = event.touches[0].clientY;
const { let {
clipWidth, clipWidth,
clipHeight, clipHeight,
cutY: oldCutY, clipY: oldClipY,
cutX: oldCutX, clipX: oldClipX,
cutstart, clipStart,
isLockRatio isLockRatio,
} = data;
let {
maxWidth, maxWidth,
minWidth, minWidth,
maxHeight, maxHeight,
...@@ -195,8 +156,11 @@ export function clipTouchMoveOfCalculate(data, event) { ...@@ -195,8 +156,11 @@ export function clipTouchMoveOfCalculate(data, event) {
let width = clipWidth, let width = clipWidth,
height = clipHeight, height = clipHeight,
cutY = oldCutY, clipY = oldClipY,
cutX = oldCutX, clipX = oldClipX,
// 获取裁剪框实际宽度/高度
// 如果大于最大值则使用最大值
// 如果小于最小值则使用最小值
sizecorrect = () => { sizecorrect = () => {
width = width <= maxWidth ? (width >= minWidth ? width : minWidth) : maxWidth; width = width <= maxWidth ? (width >= minWidth ? width : minWidth) : maxWidth;
height = height <= maxHeight ? (height >= minHeight ? height : minHeight) : maxHeight; height = height <= maxHeight ? (height >= minHeight ? height : minHeight) : maxHeight;
...@@ -209,51 +173,51 @@ export function clipTouchMoveOfCalculate(data, event) { ...@@ -209,51 +173,51 @@ export function clipTouchMoveOfCalculate(data, event) {
return true; return true;
} }
}; };
if (cutstart.corner) { //if (clipStart.corner) {
height = cutstart.height + (cutstart.corner > 1 && cutstart.corner < 4 ? 1 : -1) * (cutstart.y - clientY); height = clipStart.height + (clipStart.corner > 1 && clipStart.corner < 4 ? 1 : -1) * (clipStart.y - clientY);
} //}
switch (cutstart.corner) { switch (clipStart.corner) {
case 1: case 1:
width = cutstart.width - cutstart.x + clientX; width = clipStart.width - clipStart.x + clientX;
if (isLockRatio) { if (isLockRatio) {
height = width / (clipWidth / clipHeight); height = width / (clipWidth / clipHeight);
} }
if (!sizeinspect()) return; if (!sizeinspect()) return;
break; break;
case 2: case 2:
width = cutstart.width - cutstart.x + clientX; width = clipStart.width - clipStart.x + clientX;
if (isLockRatio) { if (isLockRatio) {
height = width / (clipWidth / clipHeight); height = width / (clipWidth / clipHeight);
} }
if (!sizeinspect()) { if (!sizeinspect()) {
return; return;
} else { } else {
cutY = cutstart.cutY - (height - cutstart.height); clipY = clipStart.clipY - (height - clipStart.height);
} }
break; break;
case 3: case 3:
width = cutstart.width + cutstart.x - clientX; width = clipStart.width + clipStart.x - clientX;
if (isLockRatio) { if (isLockRatio) {
height = width / (clipWidth / clipHeight); height = width / (clipWidth / clipHeight);
} }
if (!sizeinspect()) { if (!sizeinspect()) {
return; return;
} else { } else {
cutY = cutstart.cutY - (height - cutstart.height); clipY = clipStart.clipY - (height - clipStart.height);
cutX = cutstart.cutX - (width - cutstart.width); clipX = clipStart.clipX - (width - clipStart.width);
} }
break; break;
case 4: case 4:
width = cutstart.width + cutstart.x - clientX; width = clipStart.width + clipStart.x - clientX;
if (isLockRatio) { if (isLockRatio) {
height = width / (clipWidth / clipHeight); height = width / (clipWidth / clipHeight);
} }
if (!sizeinspect()) { if (!sizeinspect()) {
return; return;
} else { } else {
cutX = cutstart.cutX - (width - cutstart.width); clipX = clipStart.clipX - (width - clipStart.width);
} }
break; break;
default: default:
...@@ -262,8 +226,8 @@ export function clipTouchMoveOfCalculate(data, event) { ...@@ -262,8 +226,8 @@ export function clipTouchMoveOfCalculate(data, event) {
return { return {
width, width,
height, height,
cutX, clipX,
cutY clipY
}; };
} }
......
...@@ -11,20 +11,14 @@ export default { ...@@ -11,20 +11,14 @@ export default {
data() {return {path: '',options:{"width":600,"height":600}}}, data() {return {path: '',options:{"width":600,"height":600}}},
onLoad({path,options}) { onLoad({path,options}) {
this.path = path this.path = path
console.log('path-path-path-path',path);
if(options){ if(options){
this.options = JSON.parse(options) this.options = JSON.parse(options)
} }
}, },
methods:{ methods:{
successFn(e){ successFn(e){
// uni.getImageInfo({
// src:e.url,
// complete: (e) => {
// console.log(e);
// }
// })
this.uploadImgToUnicloud(e.url,(url)=>{ this.uploadImgToUnicloud(e.url,(url)=>{
//console.log(url);
this.getOpenerEventChannel().emit('uploadAvatarAfter', {url}) this.getOpenerEventChannel().emit('uploadAvatarAfter', {url})
uni.navigateBack() uni.navigateBack()
}) })
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册