1
1
<template >
2
2
<div class =" g-crop-image-principal" >
3
3
<div class =" image-wrap" :style =" { width:width + 'px',height: height + 'px' }" >
4
- <img ref =" crop-image" :src =" src" :style =" { width:width + 'px ',height: height + 'px' }" >
4
+ <img ref =" crop-image" :src =" src" :style =" { width:'100% ',height: '100%', }" >
5
5
</div >
6
- <div class =" image-mask" >
6
+ <div class =" image-mask" v-if = " !hideCrop " >
7
7
<div class =" mask top" :style =" { top:0, height: cropCSS.top + 'px', left: 0, width: '100%'}" ></div >
8
8
<div class =" mask bottom" :style =" { bottom:0, top: (cropCSS.top + cropCSS.height) + 'px', left: 0, width: '100%'}" ></div >
9
9
<div class =" mask left" :style =" {top: cropCSS.top + 'px', height: cropCSS.height + 'px', left:0, width: cropCSS.left + 'px'}" ></div >
10
10
<div class =" mask right" :style =" {top: cropCSS.top + 'px', height: cropCSS.height + 'px', left: (cropCSS.left + cropCSS.width) + 'px', right: 0}" ></div >
11
11
</div >
12
- <div class =" crop-box" v-on:touchstart.self =" drag" v-on:mousedown.self =" drag" :style =" {top: cropCSS.top + 'px', left: cropCSS.left + 'px', height: cropCSS.height + 'px', width: cropCSS.width + 'px'}" >
12
+ <div class =" crop-box" v-if = " !hideCrop " v- on:touchstart.self =" drag" v-on:mousedown.self =" drag" :style =" {top: cropCSS.top + 'px', left: cropCSS.left + 'px', height: cropCSS.height + 'px', width: cropCSS.width + 'px'}" >
13
13
<div class =" reference-line v" ></div >
14
14
<div class =" reference-line h" ></div >
15
15
<a class =" g-resize" v-on:touchstart.self =" resize" v-on:mousedown.self =" resize" ></a >
30
30
background-color : rgba (255 ,255 ,255 ,.6 );
31
31
}
32
32
.crop-box {
33
+ box-sizing : border-box ;
33
34
position : absolute ;
34
35
background : none ;
35
36
cursor : move ;
71
72
}
72
73
.crop-box .g-resize {
73
74
display : inline-block ;
74
- z-index : 90 ;
75
+ z-index : 1910 ;
75
76
position : absolute ;
76
77
bottom : -8px ;
77
78
right : -8px ;
@@ -100,6 +101,18 @@ export default {
100
101
ratio: {
101
102
type: String ,
102
103
default: ' 1:1'
104
+ },
105
+ minWidth: {
106
+ type: Number ,
107
+ default: 50 ,
108
+ },
109
+ minHeight: {
110
+ type: Number ,
111
+ default: 50 ,
112
+ },
113
+ hideCrop: {
114
+ type: [String , Boolean ],
115
+ default: false ,
103
116
}
104
117
},
105
118
@@ -120,40 +133,70 @@ export default {
120
133
if (this .ratio .indexOf (' :' ) > 0 ) {
121
134
this .ratioW = this .ratio .split (' :' )[0 ];
122
135
this .ratioH = this .ratio .split (' :' )[1 ];
123
- this .ratioVal = this .ratioW / this .ratioH ;
136
+ this .ratioVal = this .ratioW / this .ratioH ;
124
137
} else {
125
138
this .ratioW = 1 ;
126
139
this .ratioH = 1 ;
127
140
this .ratioVal = this .ratio ;
128
141
}
129
142
this .setLayout (w, h);
130
143
this .setCropBox ();
144
+ this .natrualWidth = w;
145
+ this .natrualHeight = h;
146
+ return this .imgChangeRatio ;
131
147
},
148
+
149
+ resizeImage (progress ) {
150
+ const w = this .natrualWidth * this .imgChangeRatio * progress;
151
+ const h = this .natrualHeight * this .imgChangeRatio * progress;
152
+ if (w <= this .minWidth || h < this .minHeight ) {
153
+ return ;
154
+ }
155
+ this ._setStyle (w, h, w/ h);
156
+ this .setCropBox ();
157
+ },
158
+
132
159
setLayout (w , h ) {
133
160
let H = window .innerHeight - 80 ,
134
161
W = window .innerWidth - 60 ,
135
162
width = w,
136
- height = h;
163
+ height = h,
164
+ marginLeft = 0 ;
165
+
137
166
// caculate the image ratio
138
167
let R = width / height;
139
168
let Rs = W / H ;
140
- let $container = this .$el ;
141
169
if (R > Rs) {
142
170
width = W ;
143
171
height = W / R ;
144
- // I don't hope to use a state to change the container stye
145
- $container .style .cssText = ' width:' + W + ' px;height:' + W / R + ' px;margin-top:' + (H - W / R ) / 2 + ' px' ;
172
+ marginLeft = (H - W / R ) / 2 ;
146
173
} else {
147
174
width = H * R ,
148
175
height = H ;
149
- $container . style . cssText = ' width: ' + H * R + ' px;height: ' + H + ' px;margin-left: ' + (W - H * R ) / 2 + ' px; ' ;
176
+ marginLeft = (W - H * R ) / 2 ;
150
177
}
178
+ this .marginLeft = marginLeft;
179
+ this .marginTop = 0 ;
151
180
this .imgChangeRatio = width / w;
152
- this .width = width;
153
- this .height = height;
181
+ this ._setStyle (width, height, R , true );
182
+ },
183
+
184
+ _setStyle (w , h , r , isInit ) {
185
+ const $container = this .$el ;
186
+ if (! isInit) {
187
+ this .marginLeft = this .marginLeft + (this .width - w) / 2 ;
188
+ this .marginTop = this .marginTop + (this .height - h) / 2 ;
189
+ }
190
+ $container .style .cssText = ' width:' + w + ' px;height:' + h + ' px;margin-left:'
191
+ + this .marginLeft + ' px;' + ' margin-top:' + this .marginTop + ' px' ;
192
+ this .width = w;
193
+ this .height = h;
154
194
},
155
195
156
196
setCropBox () {
197
+ if (this .hideCrop ) {
198
+ return ;
199
+ }
157
200
let $selectCropBox = this .__find (' .crop-box' );
158
201
let $wrap = this .$el ;
159
202
let imageWidth = this .width ,
@@ -181,6 +224,15 @@ export default {
181
224
182
225
getCropData () {
183
226
// keep compatible with old api
227
+ if (this .hideCrop ) {
228
+ return {
229
+ imgChangeRatio: this .imgChangeRatio ,
230
+ toCropImgX: 0 ,
231
+ toCropImgY: 0 ,
232
+ toCropImgW: this .natrualWidth ,
233
+ toCropImgH: this .natrualHeight ,
234
+ };
235
+ }
184
236
return {
185
237
toCropImgX: this .cropCSS .left / this .imgChangeRatio ,
186
238
toCropImgY: this .cropCSS .top / this .imgChangeRatio ,
0 commit comments