@@ -17,7 +17,7 @@ export default {
1717 const reader = new FileReader ( ) ;
1818 const self = this ;
1919 reader . onload = function ( event ) {
20- let image = new Image ( ) ;
20+ const image = new Image ( ) ;
2121 image . src = event . target . result ;
2222 image . onload = function ( ) {
2323 const mimeType = self . _getImageType ( src . type ) ;
@@ -37,7 +37,10 @@ export default {
3737 return ( typeof num === 'number' ) ;
3838 } ;
3939 // check crop options
40- if ( checkNumber ( options . toCropImgX ) && checkNumber ( options . toCropImgY ) && options . toCropImgW > 0 && options . toCropImgH > 0 ) {
40+ if ( checkNumber ( options . toCropImgX ) &&
41+ checkNumber ( options . toCropImgY ) &&
42+ options . toCropImgW > 0 &&
43+ options . toCropImgH > 0 ) {
4144 let w = options . toCropImgW ;
4245 let h = options . toCropImgH ;
4346 if ( options . maxWidth && options . maxWidth < w ) {
@@ -59,26 +62,66 @@ export default {
5962 const checkNumber = function ( num ) {
6063 return ( typeof num === 'number' ) ;
6164 } ;
62- if ( checkNumber ( options . toCropImgX ) && checkNumber ( options . toCropImgY ) && options . toCropImgW > 0 && options . toCropImgH > 0 ) {
63- let w = options . toCropImgW * options . imgChangeRatio ;
64- let h = options . toCropImgH * options . imgChangeRatio ;
65+ if ( checkNumber ( options . toCropImgX ) && checkNumber ( options . toCropImgY ) && options . toCropImgW > 0 && options . toCropImgH > 0 ) {
66+ const w = options . toCropImgW * options . imgChangeRatio ;
67+ const h = options . toCropImgH * options . imgChangeRatio ;
6568 const cvs = this . _getCanvas ( w , h ) ;
66- const ctx = cvs . getContext ( '2d' ) . drawImage ( image , 0 , 0 , options . toCropImgW , options . toCropImgH , 0 , 0 , w , h ) ;
69+ const ctx = cvs . getContext ( '2d' ) . drawImage ( image , 0 , 0 , options . toCropImgW , options . toCropImgH , 0 , 0 , w , h ) ;
6770 const mimeType = this . _getImageType ( image . src ) ;
68- const data = cvs . toDataURL ( mimeType , options . compress / 100 ) ;
71+ const data = cvs . toDataURL ( mimeType , options . compress / 100 ) ;
6972 callback ( data ) ;
7073 }
7174 } ,
7275
76+ rotate ( src , degrees , callback ) {
77+ this . _loadImage ( src , ( image ) => {
78+ let w = image . naturalWidth ;
79+ let h = image . naturalHeight ;
80+ const canvasWidth = Math . max ( w , h ) ;
81+ const cvs = this . _getCanvas ( canvasWidth , canvasWidth ) ;
82+ const ctx = cvs . getContext ( '2d' ) ;
83+ ctx . save ( ) ;
84+ ctx . translate ( canvasWidth / 2 , canvasWidth / 2 ) ;
85+ ctx . rotate ( degrees * ( Math . PI / 180 ) ) ;
86+ let x = - canvasWidth / 2 ;
87+ let y = - canvasWidth / 2 ;
88+ degrees %= 360 ;
89+ if ( degrees === 0 ) {
90+ return callback ( src , w , h ) ;
91+ }
92+ if ( ( degrees % 180 ) !== 0 ) {
93+ if ( degrees === - 90 || degrees === 270 ) {
94+ x = - w + canvasWidth / 2 ;
95+ } else {
96+ y = canvasWidth / 2 - h ;
97+ }
98+ const c = w ;
99+ w = h ;
100+ h = c ;
101+ } else {
102+ x = canvasWidth / 2 - w ;
103+ y = canvasWidth / 2 - h ;
104+ }
105+
106+ ctx . drawImage ( image , x , y ) ;
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 ) ;
110+ const mimeType = this . _getImageType ( image . src ) ;
111+ const data = cvs . toDataURL ( mimeType , 1 ) ;
112+ callback ( data , w , h ) ;
113+ } ) ;
114+ } ,
115+
73116 _loadImage ( data , callback ) {
74117 const image = new Image ( ) ;
75118 image . src = data ;
76119 image . onload = function ( ) {
77120 callback ( image ) ;
78- }
79- image . onerror = function ( ) {
121+ } ;
122+ image . onerror = function ( ) {
80123 console . log ( 'Error: image error!' ) ;
81- }
124+ } ;
82125 } ,
83126
84127 _getCanvas ( width , height ) {
0 commit comments