@@ -17,7 +17,7 @@ export default {
17
17
const reader = new FileReader ( ) ;
18
18
const self = this ;
19
19
reader . onload = function ( event ) {
20
- let image = new Image ( ) ;
20
+ const image = new Image ( ) ;
21
21
image . src = event . target . result ;
22
22
image . onload = function ( ) {
23
23
const mimeType = self . _getImageType ( src . type ) ;
@@ -37,7 +37,7 @@ export default {
37
37
return ( typeof num === 'number' ) ;
38
38
} ;
39
39
// check crop options
40
- if ( checkNumber ( options . toCropImgX ) && checkNumber ( options . toCropImgY ) && options . toCropImgW > 0 && options . toCropImgH > 0 ) {
40
+ if ( checkNumber ( options . toCropImgX ) && checkNumber ( options . toCropImgY ) && options . toCropImgW > 0 && options . toCropImgH > 0 ) {
41
41
let w = options . toCropImgW ;
42
42
let h = options . toCropImgH ;
43
43
if ( options . maxWidth && options . maxWidth < w ) {
@@ -65,7 +65,7 @@ export default {
65
65
const cvs = this . _getCanvas ( w , h ) ;
66
66
const ctx = cvs . getContext ( '2d' ) . drawImage ( image , 0 , 0 , options . toCropImgW , options . toCropImgH , 0 , 0 , w , h ) ;
67
67
const mimeType = this . _getImageType ( image . src ) ;
68
- const data = cvs . toDataURL ( mimeType , options . compress / 100 ) ;
68
+ const data = cvs . toDataURL ( mimeType , options . compress / 100 ) ;
69
69
callback ( data ) ;
70
70
}
71
71
} ,
@@ -75,21 +75,34 @@ export default {
75
75
let w = image . naturalWidth ;
76
76
let h = image . naturalHeight ;
77
77
const canvasWidth = Math . max ( w , h ) ;
78
- const cvs = this . _getCanvas ( canvasWidth , canvasWidth ) ;
79
- const targetCvs = this . _getCanvas ( h , w ) ;
80
- const ctx = cvs . getContext ( '2d' ) ;
81
- ctx . drawImage ( image , ( canvasWidth - w ) / 2 , ( canvasWidth - h ) / 2 ) ;
82
- ctx . clearRect ( 0 , 0 , canvasWidth , canvasWidth ) ;
78
+ let cvs = this . _getCanvas ( canvasWidth , canvasWidth ) ;
79
+ let ctx = cvs . getContext ( '2d' ) ;
83
80
ctx . save ( ) ;
84
81
ctx . translate ( canvasWidth / 2 , canvasWidth / 2 ) ;
85
82
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 ) ) ;
83
+ const x = - canvasWidth / 2 ;
84
+ degrees = degrees % 360 ;
85
+ if ( degrees === 0 ) {
86
+ return callback ( src , w , h ) ;
87
+ }
88
+ let y = ( ( canvasWidth - h ) - canvasWidth / 2 )
89
+ if ( ( degrees % 180 ) !== 0 ) {
90
+ const c = w ;
91
+ w = h ;
92
+ h = c ;
93
+ if ( degrees === - 90 || degrees === 270 ) {
94
+ y = - canvasWidth / 2 ;
95
+ }
96
+ }
97
+ ctx . drawImage ( image , x , y ) ;
89
98
ctx . restore ( ) ;
99
+ //cvs.width = h;
100
+ //cvs.height = w;
90
101
const mimeType = this . _getImageType ( image . src ) ;
91
102
const data = cvs . toDataURL ( mimeType , 1 ) ;
92
- callback ( data , h , w ) ;
103
+ callback ( data , w , h ) ;
104
+ cvs = null ;
105
+ ctx = null ;
93
106
} ) ;
94
107
} ,
95
108
@@ -98,10 +111,10 @@ export default {
98
111
image . src = data ;
99
112
image . onload = function ( ) {
100
113
callback ( image ) ;
101
- }
102
- image . onerror = function ( ) {
114
+ } ;
115
+ image . onerror = function ( ) {
103
116
console . log ( 'Error: image error!' ) ;
104
- }
117
+ } ;
105
118
} ,
106
119
107
120
_getCanvas ( width , height ) {
0 commit comments