Skip to content

Commit 5fcb180

Browse files
committed
add .gitignore
1 parent e83e515 commit 5fcb180

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
npm-debug.log

lib/index.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
var React = require('react');
2+
var qr = require('qr.js');
3+
4+
function getBackingStorePixelRatio(ctx) {
5+
return (
6+
ctx.webkitBackingStorePixelRatio ||
7+
ctx.mozBackingStorePixelRatio ||
8+
ctx.msBackingStorePixelRatio ||
9+
ctx.oBackingStorePixelRatio ||
10+
ctx.backingStorePixelRatio ||
11+
1
12+
);
13+
}
14+
15+
var QRCode = React.createClass({
16+
propTypes: {
17+
value: React.PropTypes.string.isRequired,
18+
size: React.PropTypes.number,
19+
bgColor: React.PropTypes.string,
20+
fgColor: React.PropTypes.string,
21+
logo: React.PropTypes.string
22+
},
23+
24+
getDefaultProps: function() {
25+
return {
26+
size: 128,
27+
bgColor: '#FFFFFF',
28+
fgColor: '#000000'
29+
};
30+
},
31+
32+
shouldComponentUpdate: function(nextProps) {
33+
return Object.keys(QRCode.propTypes).some((k) => this.props[k] !== nextProps[k]);
34+
},
35+
36+
componentDidMount: function() {
37+
this.update();
38+
},
39+
40+
componentDidUpdate: function() {
41+
this.update();
42+
},
43+
44+
update: function() {
45+
var qrcode = qr(this.props.value);
46+
var canvas = this.refs.canvas.getDOMNode();
47+
48+
var ctx = canvas.getContext('2d');
49+
var cells = qrcode.modules;
50+
var tileW = this.props.size / cells.length;
51+
var tileH = this.props.size / cells.length;
52+
var scale = window.devicePixelRatio / getBackingStorePixelRatio(ctx);
53+
canvas.height = canvas.width = this.props.size * scale;
54+
ctx.scale(scale, scale);
55+
56+
cells.forEach(function(row, rdx) {
57+
row.forEach(function(cell, cdx) {
58+
ctx.fillStyle = cell ? this.props.fgColor : this.props.bgColor;
59+
var w = (Math.ceil((cdx + 1) * tileW) - Math.floor(cdx * tileW));
60+
var h = (Math.ceil((rdx + 1) * tileH) - Math.floor(rdx * tileH));
61+
ctx.fillRect(Math.round(cdx * tileW), Math.round(rdx * tileH), w, h);
62+
}, this);
63+
}, this);
64+
65+
if(this.props.logo) {
66+
var size = this.props.size;
67+
var image = document.createElement('img');
68+
image.src = this.props.logo;
69+
image.onload = function () {
70+
var dx = size/2 - size*0.1;
71+
var dwidth = size*0.2;
72+
ctx.drawImage(image, dx, dx, dwidth, dwidth);
73+
}
74+
}
75+
},
76+
77+
render: function() {
78+
return (
79+
<canvas
80+
style={{height: this.props.size, width: this.props.size}}
81+
height={this.props.size}
82+
width={this.props.size}
83+
ref="canvas"
84+
/>
85+
);
86+
}
87+
});
88+
89+
module.exports = QRCode;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "qrcode-react",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "React component to generate QRCode with logo",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)