Skip to content

Commit c611a7f

Browse files
committed
add crop images
1 parent 3c78f2d commit c611a7f

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

site/client/components/doc/en/CropImage.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
url="http://101.198.151.190/api/upload.php"
1818
@imageuploaded="crpoServerImageUploaded"
1919
:max-file-size="10485760"
20+
:rotate="false"
2021
text="上传图片" >
2122
</vue-core-image-upload>
2223
</div>

site/src/lib/canvas-helper.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,35 +83,33 @@ export default {
8383
ctx.save();
8484
ctx.translate(canvasWidth / 2, canvasWidth / 2);
8585
ctx.rotate(degrees * (Math.PI / 180));
86-
let x = 0;
86+
let x = -canvasWidth / 2;
87+
let y = -canvasWidth / 2;
8788
degrees %= 360;
8889
if (degrees === 0) {
8990
return callback(src, w, h);
9091
}
91-
let y = 0;
9292
if ((degrees % 180) !== 0) {
93-
9493
if (degrees === -90 || degrees === 270) {
95-
x = -h;
94+
x = -w + canvasWidth / 2;
9695
} else {
97-
y = -w;
96+
y = canvasWidth/2 - h;
9897
}
9998
const c = w;
10099
w = h;
101100
h = c;
102-
cvs.width = w;
103-
cvs.height = h;
104101
} else {
105-
x = -w;
106-
y = -h;
102+
x = canvasWidth/2 - w;
103+
y = canvasWidth/2 - h;
107104
}
105+
108106
ctx.drawImage(image, x, y);
109-
ctx.restore();
107+
const cvs2 = this._getCanvas(w, h);
108+
const ctx2 = cvs2.getContext('2d');
109+
ctx2.drawImage(cvs, 0, 0, w, h, 0, 0, w, h);
110110
const mimeType = this._getImageType(image.src);
111111
const data = cvs.toDataURL(mimeType, 1);
112112
callback(data, w, h);
113-
cvs = null;
114-
ctx = null;
115113
});
116114
},
117115

0 commit comments

Comments
 (0)