-
Notifications
You must be signed in to change notification settings - Fork 627
/
index.js
76 lines (67 loc) 路 1.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {createElement, Component} from 'rax';
import Canvas from 'rax-canvas';
import StyleSheet from 'universal-stylesheet';
import qr from 'qr.js';
class QRCode extends Component {
constructor(props) {
super(props);
const {style = {}} = props;
const {width = 300, height = 300} = style;
this.width = width;
this.height = height;
this.canvas = null;
}
componentDidMount() {
const {data = '', options = {}} = this.props;
if (data === '') {
return;
}
this.drawCode(data, options);
}
componentWillReceiveProps(nextProps) {
const {data, options} = nextProps;
if (data !== this.props.data) {
this.drawCode(data, options);
}
}
canvasRef = (ref) => {
this.canvas = ref;
}
drawCode = (data, options) => {
if (this.canvas === null) {
return;
}
const codeData = qr(data, options);
const {fillColor = '#000000', blankColor = '#ffffff'} = options;
const cells = codeData.modules;
const tileWidth = this.width / cells.length;
const tileHeight = this.height / cells.length;
const ctx = this.canvas.getContext();
for (let r = 0; r < cells.length; ++r) {
const row = cells[r];
for (let c = 0; c < row.length; ++c) {
ctx.fillStyle = row[c] ? fillColor : blankColor;
const w = Math.ceil((c + 1) * tileWidth) - Math.floor(c * tileWidth);
const h = Math.ceil((r + 1) * tileHeight) - Math.floor(r * tileHeight);
ctx.fillRect(Math.round(c * tileWidth), Math.round(r * tileHeight), w, h);
}
}
}
render() {
const {style} = this.props;
return (
<Canvas
style={[styles.qrCode, style]}
ref={this.canvasRef}
/>
);
}
}
const styles = StyleSheet.create({
qrCode: {
width: 300,
height: 300
}
});
QRCode.ErrorCorrectLevel = qr.ErrorCorrectLevel;
export default QRCode;