React二维码组件

React二维码组件

先看效果
在这里插入图片描述

ImageCode.js

import React, { Component } from 'react';
import styles from './index.less'

export default class ImageCode extends Component{
  constructor(props){
    super(props)
    this.state = {
      ...this.initState(),
    }
  }
  initState = () => {
    return {
      data: this.getRandom(109,48,4),
      rotate: this.getRandom(75,-75,4),
      fz: this.getRandom(8,20,4),
      color: [
        this.getRandom(100,255,3),
        this.getRandom(100,255,4),
        this.getRandom(100,255,3),
        this.getRandom(100,255,3)
      ],
    }
  }

  getRandom(max, min, num) {
    const asciiNum = ~~(Math.random()*(max-min+1)+min)
    if(!Boolean(num)){
      return asciiNum
    }
    const arr = []
    for(let i = 0; i < num; i++){
      arr.push(this.getRandom(max, min))
    }
    return arr
  }

  canvas = () => {
    const { getRandom } = this
    const canvas = document.getElementById('bgi')
    let ctx = canvas.getContext('2d')
    canvas.height = canvas.height
    ctx.strokeStyle = `rgb(${this.getRandom(100,10,3).toString()})`
    for( let i = 0; i< 7; i++ ) {
      ctx.moveTo(getRandom(200,0),getRandom(200,0))
      ctx.stroke();
    }
  }
  componentDidMount() {
    this.refreshCode()
  }

  refreshCode = () => {
    this.setState({
      ...this.initState(),
    })
    this.canvas()
  }

  render() {
    const { rotate, fz, color } = this.state
    let abcArr = [];
    this.state.data.map((v, i) => {
      let randomStr = String.fromCharCode(v > 57 && v < 84 ? v + 7 : ( v < 57 ? v : v + 13 ));
      abcArr.push(randomStr);
    })
    // 传值给父组件
    this.props.getImageCode(abcArr.join(''));

    return (
      <div className={styles.vcodewrap} onClick={this.refreshCode}>
        <canvas className={styles.bgiview} id="bgi" width="200" height="200"></canvas>
        {abcArr.map((letterStr, i) =>
          <div
            key={i}
            className={styles.itemStr}
            style={{
              fontSize: `20px`,
              fontWeight: 800,
              color: `rgb(${color[i].toString()})`,
            }}
          >
            {letterStr}
          </div>
        )}
      </div>
    )
  }
}

ImageCode.less

.vcodewrap {
  width: 90px;
  overflow: hidden;
  font-size: 20px;
  height: 35px;
  text-align: center;
  position: relative;
  background: #c5026e;
  display: flex;
  padding: 8px;
  align-items: center;
  justify-content: center;
  cursor:pointer;
  -webkit-touch-callout: none; /* iOS Safari */
  -webkit-user-select: none; /* Chrome/Safari/Opera */
  -khtml-user-select: none; /* Konqueror */
  -moz-user-select: none; /* Firefox */
  -ms-user-select: none; /* Internet Explorer/Edge */
  user-select: none; /* Non-prefixed version, currently not supported by any browser */

  .itemStr {
    margin-left: 7px;
    //margin-left: 3px
  }
  .mask {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    background-color: rgba(0,0,0,0.95) ;
    opacity: 0.8;
    line-height: 25px;
    color: white;
    z-index: 1;
  }
  .bgiview {
    top: 0;
    left: 0;
    position: absolute;
    width: 100%;
    height: 100%;
  }
}

使用方法

在index.js中使用

import React, { Component } from 'react';
import ImageCode from '../ImageCode'
export default class Index extends Component {
	getImageCode = (value) => {
		console.log(value.toLowerCase()); // 二维码结果
	}
	render() {
		<div>
			<ImageCode getImageCode={this.getImageCode} />
		</div>
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值