Skip to content

Commit c68aaa8

Browse files
committed
1.0.11 做了些许优化
1 parent 974043c commit c68aaa8

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

example/src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Test extends React.Component {
1111
input2: "", // 第2个input的值
1212
vcode2: "-1", // 第2个vcode的值
1313
code: "",
14-
width: 200,
14+
width: 150,
1515
};
1616
}
1717

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-vcode",
3-
"version": "1.0.10",
3+
"version": "1.0.11",
44
"description": "a react verification code component",
55
"main": "dist/index.js",
66
"files": [

src/index.tsx

+22-25
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ export default class Vcode extends React.PureComponent<Props, State> {
137137
"#008888",
138138
],
139139
lineHeightMin: 1, // 线的粗细最小值
140-
lineHeightMax: 1, // 线的粗细最大值
141-
lineWidthMin: 20, // 线的长度最小值
142-
lineWidthMax: 60, // 线的长度最大值
140+
lineHeightMax: 2, // 线的粗细最大值
141+
lineWidthMin: 40, // 线的长度最小值
142+
lineWidthMax: 100, // 线的长度最大值
143143
};
144144
if (this.props.options) {
145145
return Object.assign({}, a, this.props.options);
@@ -188,9 +188,15 @@ export default class Vcode extends React.PureComponent<Props, State> {
188188
* 随机生成一个Code的CSS样式
189189
* @param uW 每个字符所占的宽度
190190
* @param i 当前字符的下标
191+
* @param maxW 最大偏移值
191192
* @return CSS字符串
192193
*/
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)}%)`;
194200
return [
195201
`font-size:${this.randint(
196202
this.state.options.fontSizeMin,
@@ -202,13 +208,12 @@ export default class Vcode extends React.PureComponent<Props, State> {
202208
]
203209
}`,
204210
"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`,
206215
"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}`,
212217
`font-family:${
213218
this.state.options.fonts[
214219
this.randint(0, this.state.options.fonts.length - 1)
@@ -224,6 +229,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
224229
* @return CSS字符串
225230
*/
226231
lineCss(): string {
232+
const transStr = `rotate(${this.randint(-30, 30)}deg)`;
227233
return [
228234
"position: absolute",
229235
`opacity:${this.randint(3, 8) / 10}`,
@@ -245,17 +251,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
245251
this.state.width
246252
)}px`,
247253
`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}`,
259255
].join(";");
260256
}
261257

@@ -290,11 +286,12 @@ export default class Vcode extends React.PureComponent<Props, State> {
290286

291287
// 不是图片而是普通字符串, 如果value存在说明是用户自定义的字符串
292288
const length = value ? value.length : this.state.len; // 字符的长度
289+
const uW: number = this.state.width / length; // 每个字符能够占据的范围宽度
290+
const maxW = this.state.width - uW / 4; // 最大可偏移距离
293291

294-
const uW: number = this.state.width / length / 1.01; // 每个字符占的宽度
295292
for (let i = 0; i < length; i++) {
296293
const dom = document.createElement("span");
297-
dom.style.cssText = this.codeCss(uW, i);
294+
dom.style.cssText = this.codeCss(uW, i, maxW);
298295
const temp = value
299296
? value[i]
300297
: this.state.options.codes[
@@ -316,10 +313,10 @@ export default class Vcode extends React.PureComponent<Props, State> {
316313
}
317314

318315
/** 生成范围随机数 **/
319-
randint(n: number, m: number): number {
316+
randint(n: number, m: number, t?: boolean): number {
320317
const c = m - n + 1;
321318
const num = Math.random() * c + n;
322-
return Math.floor(num);
319+
return t ? num : Math.floor(num);
323320
}
324321

325322
render() {

0 commit comments

Comments
 (0)