fix: 改为通用

pull/199/head
youxia 2 years ago
parent 27704cd5f7
commit 87bb0e3b79

@ -85,16 +85,22 @@ function getCroppedImg(
const ctx = canvas.getContext('2d'); const ctx = canvas.getContext('2d');
if (!_isNil(ctx)) { if (!_isNil(ctx)) {
// 设置头像大小 // 设定最大尺寸,可以提出来作为参数传入
const size = 256; const maxSize = 256;
canvas.width = size; // 计算最大尺寸
canvas.height = size; const size = Math.min(crop.width, crop.height, maxSize);
// 计算缩放比例
const scale = size / Math.max(crop.width, crop.height);
canvas.width = scale * crop.width;
canvas.height = scale * crop.height;
// translate canvas context to a central location on image to allow rotating around the center. // translate canvas context to a central location on image to allow rotating around the center.
ctx.translate(size / 2, size / 2); ctx.translate(canvas.width / 2, canvas.width / 2);
ctx.rotate(getRadianAngle(rotation)); ctx.rotate(getRadianAngle(rotation));
ctx.translate(-size / 2, -size / 2); ctx.translate(-canvas.width / 2, -canvas.width / 2);
// draw rotated image and store data. // draw rotated image and store data.
ctx.drawImage( ctx.drawImage(
@ -105,8 +111,8 @@ function getCroppedImg(
crop.height, crop.height,
0, 0,
0, 0,
size, canvas.width,
size canvas.height
); );
} }

Loading…
Cancel
Save