107 lines
3.2 KiB
JavaScript
Raw Normal View History

'use strict';
2015-11-20 23:48:49 +08:00
2016-05-05 23:35:57 +08:00
var React = require('react');
2015-11-20 23:48:49 +08:00
var Canvas = require('./Canvas.js');
var qr = require('qr.js');
2015-11-20 23:48:49 +08:00
var {
2016-05-05 23:35:57 +08:00
View,
} = require('react-native');
2015-11-20 23:48:49 +08:00
function renderCanvas(canvas) {
var ctx = canvas.getContext('2d');
var size = this.size;
var fgColor = this.fgColor;
var bgColor = this.bgColor;
canvas.width = size;
canvas.height = size;
2016-03-11 14:57:17 +08:00
canvas.style.left = (window.innerWidth - size) / 2 + 'px';
if(window.innerHeight > size) canvas.style.top = (window.innerHeight - size) / 2 + 'px';
ctx.fillRect(0, 0, size, size);
var cells = this.cells;
var cellWidth = this.size / cells.length;
var cellHeight = this.size / cells.length;
2016-05-26 10:55:35 +08:00
var nRoundedWidth = Math.round(cellWidth);
var nRoundedHeight = Math.round(cellHeight);
cells.forEach(function(row, rowIndex) {
row.forEach(function(column, columnIndex) {
2016-05-26 10:53:21 +08:00
var nLeft = columnIndex * cellWidth;
var nTop = rowIndex * cellHeight;
ctx.fillStyle = ctx.strokeStyle = column ? bgColor : fgColor;
ctx.lineWidth = 1;
ctx.fillRect(nLeft, nTop, cellWidth, cellHeight);
ctx.strokeRect(
Math.floor(nLeft) + 0.5,
Math.floor(nTop) + 0.5,
nRoundedWidth,
nRoundedHeight
);
ctx.strokeRect(
Math.ceil(nLeft) - 0.5,
Math.ceil(nTop) - 0.5,
nRoundedWidth,
nRoundedHeight
);
});
});
}
var QRCode = React.createClass({
PropTypes: {
value: React.PropTypes.string,
size: React.PropTypes.number,
bgColor: React.PropTypes.string,
fgColor: React.PropTypes.string,
2015-11-20 23:48:49 +08:00
},
getDefaultProps: function() {
return {
value: 'https://github.com/cssivision',
fgColor: 'white',
bgColor: 'black',
2015-11-21 21:20:49 +08:00
size: 128,
2015-11-20 23:48:49 +08:00
}
},
2015-11-29 16:45:33 +08:00
utf16to8: function(str) {
var out, i, len, c;
out = "";
len = str.length;
2016-03-11 14:57:17 +08:00
for (i = 0; i < len; i++) {
2015-11-29 16:45:33 +08:00
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
out += str.charAt(i);
} else if (c > 0x07FF) {
out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
}
return out;
},
2015-11-20 23:48:49 +08:00
render: function() {
var size = this.props.size;
2015-11-29 16:45:33 +08:00
var value = this.utf16to8(this.props.value);
2015-11-20 23:48:49 +08:00
return (
<View>
<Canvas
context={{
size: size,
value: this.props.value,
bgColor: this.props.bgColor,
fgColor: this.props.fgColor,
2015-11-29 16:45:33 +08:00
cells: qr(value).modules,
}}
render={renderCanvas}
2015-11-21 21:20:49 +08:00
style={{height: size, width: size}}
2015-11-20 23:48:49 +08:00
/>
</View>
);
}
});
2015-11-21 17:15:46 +08:00
module.exports = QRCode;