Skip to content

Commit d1a90ce

Browse files
committed
fix rotate function
1 parent 20909d9 commit d1a90ce

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

site/src/crop.vue

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</div>
1919
</div>
2020
<resize-bar v-if="resize" ref="resizeBar" @resize="resizeImage"></resize-bar>
21-
<rotate-bar @rotate="roateImage"></rotate-bar>
21+
<rotate-bar @rotate="rotateImage"></rotate-bar>
2222
</div>
2323
</div>
2424
</template>
@@ -131,6 +131,7 @@ import drag from './lib/drag';
131131
import resize from './lib/resize';
132132
import GIF_LOADING_SRC from './lib/loading-gif';
133133
import helper from './lib/helper';
134+
import canvasHelper from './lib/canvas-helper';
134135
import ResizeBar from './resize-bar.vue';
135136
import RotateBar from './rotate-bar';
136137
// set cropbox size in image
@@ -220,9 +221,13 @@ export default {
220221
this.imgChangeRatio = this.width / this.natrualWidth;
221222
},
222223
223-
rotateImage(src) {
224-
console.log(src);
225-
this.src = src;
224+
rotateImage(degress) {
225+
console.log(degress);
226+
const data = canvasHelper.rotate(this.src, degress, (data, w, h) => {
227+
console.log(data);
228+
this.setImage(data, w, h);
229+
});
230+
//this.src = src;
226231
},
227232
228233
setLayout(w, h) {

site/src/lib/canvas-helper.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,27 @@ export default {
7070
}
7171
},
7272

73-
rotate(src, degress, callback) {
73+
rotate(src, degrees, callback) {
7474
this._loadImage(src, (image) => {
7575
let w = image.naturalWidth;
7676
let h = image.naturalHeight;
7777
const canvasWidth = Math.max(w, h);
7878
const cvs = this._getCanvas(canvasWidth, canvasWidth);
79-
const ctx = cvs.getContext('2d').drawImage(image, (canvasWidth - w) / 2 , (canvasWidth - h) / 2);
79+
const targetCvs = this._getCanvas(h, w);
80+
const ctx = cvs.getContext('2d');
81+
ctx.drawImage(image, (canvasWidth - w) / 2, (canvasWidth - h) / 2);
8082
ctx.clearRect(0, 0, canvasWidth, canvasWidth);
8183
ctx.save();
8284
ctx.translate(canvasWidth / 2, canvasWidth / 2);
83-
ctx.rotate( degrees * (Math.PI / 180));
84-
ctx.drawImage(image, -image.width/2, -image.width/2);
85+
ctx.rotate(degrees * (Math.PI / 180));
86+
cvs.width = h;
87+
cvs.height = w;
88+
ctx.drawImage(image, -((image.width - image.height) / 2), -((image.width - image.height) / 2));
8589
ctx.restore();
8690
const mimeType = this._getImageType(image.src);
87-
const data = cvs.toDataURL(mimeType, options.compress/100);
88-
callback(data);
91+
const data = cvs.toDataURL(mimeType, 1);
92+
callback(data, h, w);
8993
});
90-
9194
},
9295

9396
_loadImage(data, callback) {

site/src/rotate-bar.vue

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
</style>
3535

3636
<script>
37-
import canvasHelper from './lib/canvas-helper';
38-
3937
export default {
4038
data() {
4139
return {
@@ -45,13 +43,12 @@ export default {
4543
4644
methods: {
4745
rotateRight() {
48-
this.rotateDegree = (this.rotateDegree % 360) + 90;
46+
this.$emit('rotate', 90);
4947
},
5048
5149
rotateLeft() {
52-
this.rotateDegree = (this.rotateDegree % 360) + 90;
50+
this.$emit('rotate', -90);
5351
},
54-
5552
}
5653
};
5754

0 commit comments

Comments
 (0)