@@ -137,9 +137,9 @@ export default class Vcode extends React.PureComponent<Props, State> {
137
137
"#008888" ,
138
138
] ,
139
139
lineHeightMin : 1 , // 线的粗细最小值
140
- lineHeightMax : 1 , // 线的粗细最大值
141
- lineWidthMin : 20 , // 线的长度最小值
142
- lineWidthMax : 60 , // 线的长度最大值
140
+ lineHeightMax : 2 , // 线的粗细最大值
141
+ lineWidthMin : 40 , // 线的长度最小值
142
+ lineWidthMax : 100 , // 线的长度最大值
143
143
} ;
144
144
if ( this . props . options ) {
145
145
return Object . assign ( { } , a , this . props . options ) ;
@@ -188,9 +188,15 @@ export default class Vcode extends React.PureComponent<Props, State> {
188
188
* 随机生成一个Code的CSS样式
189
189
* @param uW 每个字符所占的宽度
190
190
* @param i 当前字符的下标
191
+ * @param maxW 最大偏移值
191
192
* @return CSS字符串
192
193
*/
193
- codeCss ( uW : number , i : number ) : string {
194
+ codeCss ( uW : number , i : number , maxW : number ) : string {
195
+ const transStr = `rotate(${ this . randint (
196
+ - 15 ,
197
+ 15 ,
198
+ true
199
+ ) } deg) translateY(${ this . randint ( - 55 , - 45 , true ) } %)`;
194
200
return [
195
201
`font-size:${ this . randint (
196
202
this . state . options . fontSizeMin ,
@@ -202,13 +208,12 @@ export default class Vcode extends React.PureComponent<Props, State> {
202
208
]
203
209
} `,
204
210
"position: absolute" ,
205
- `left:${ this . randint ( uW * i , uW * i + uW - uW / 2 ) } px` ,
211
+ `left:${ Math . max (
212
+ Math . min ( this . randint ( uW * i , uW * i + uW / 2 , true ) , maxW ) ,
213
+ uW / 4
214
+ ) } px`,
206
215
"top:50%" ,
207
- `transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
208
- `-o-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
209
- `-ms-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
210
- `-moz-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
211
- `-webkit-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
216
+ `transform:${ transStr } ;-o-transform:${ transStr } ;-ms-transform:${ transStr } ;-moz-transform:${ transStr } ;-webkit-transform:${ transStr } ` ,
212
217
`font-family:${
213
218
this . state . options . fonts [
214
219
this . randint ( 0 , this . state . options . fonts . length - 1 )
@@ -224,6 +229,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
224
229
* @return CSS字符串
225
230
*/
226
231
lineCss ( ) : string {
232
+ const transStr = `rotate(${ this . randint ( - 30 , 30 ) } deg)` ;
227
233
return [
228
234
"position: absolute" ,
229
235
`opacity:${ this . randint ( 3 , 8 ) / 10 } ` ,
@@ -245,17 +251,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
245
251
this . state . width
246
252
) } px`,
247
253
`top:${ this . randint ( 0 , this . state . height ) } px` ,
248
- `transform:rotate(${ this . randint ( - 30 , 30 ) } deg)` ,
249
- `-o-transform:rotate(${ this . randint ( - 30 , 30 ) } deg)` ,
250
- `-ms-transform:rotate(${ this . randint ( - 30 , 30 ) } deg)` ,
251
- `-moz-transform:rotate(${ this . randint ( - 30 , 30 ) } deg)` ,
252
- `-webkit-transform:rotate(${ this . randint ( - 30 , 30 ) } deg)` ,
253
- `font-family:${
254
- this . state . options . fonts [
255
- this . randint ( 0 , this . state . options . fonts . length - 1 )
256
- ]
257
- } `,
258
- `font-weight:${ this . randint ( 400 , 900 ) } ` ,
254
+ `transform:${ transStr } ;-o-transform:${ transStr } ;-ms-transform:${ transStr } ;-moz-transform:${ transStr } ;-webkit-transform:${ transStr } ` ,
259
255
] . join ( ";" ) ;
260
256
}
261
257
@@ -290,11 +286,12 @@ export default class Vcode extends React.PureComponent<Props, State> {
290
286
291
287
// 不是图片而是普通字符串, 如果value存在说明是用户自定义的字符串
292
288
const length = value ? value . length : this . state . len ; // 字符的长度
289
+ const uW : number = this . state . width / length ; // 每个字符能够占据的范围宽度
290
+ const maxW = this . state . width - uW / 4 ; // 最大可偏移距离
293
291
294
- const uW : number = this . state . width / length / 1.01 ; // 每个字符占的宽度
295
292
for ( let i = 0 ; i < length ; i ++ ) {
296
293
const dom = document . createElement ( "span" ) ;
297
- dom . style . cssText = this . codeCss ( uW , i ) ;
294
+ dom . style . cssText = this . codeCss ( uW , i , maxW ) ;
298
295
const temp = value
299
296
? value [ i ]
300
297
: this . state . options . codes [
@@ -316,10 +313,10 @@ export default class Vcode extends React.PureComponent<Props, State> {
316
313
}
317
314
318
315
/** 生成范围随机数 **/
319
- randint ( n : number , m : number ) : number {
316
+ randint ( n : number , m : number , t ?: boolean ) : number {
320
317
const c = m - n + 1 ;
321
318
const num = Math . random ( ) * c + n ;
322
- return Math . floor ( num ) ;
319
+ return t ? num : Math . floor ( num ) ;
323
320
}
324
321
325
322
render ( ) {
0 commit comments