react-captcha-generator
Version:
react captcha generator
99 lines (79 loc) • 5.21 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var ReactCaptchaGenerator = function (_Component) {
_inherits(ReactCaptchaGenerator, _Component);
function ReactCaptchaGenerator(props) {
_classCallCheck(this, ReactCaptchaGenerator);
var _this = _possibleConstructorReturn(this, (ReactCaptchaGenerator.__proto__ || Object.getPrototypeOf(ReactCaptchaGenerator)).call(this, props));
_this.state = {
height: 100,
width: 100,
textColor: false,
fontFamily: 'Verdana',
fontSize: '30',
paddingLeft: '20',
paddingTop: '60',
lenght: '5',
background: 'none'
};
_this.setData = _this.setData.bind(_this);
return _this;
}
_createClass(ReactCaptchaGenerator, [{
key: 'componentWillMount',
value: function componentWillMount() {
this.setData();
}
// componentDidUpdate() {
// this.setData()
// }
// componentDidUpdate(previousProps, previousState) {
// if (previousProps.data !== this.props.data) {
// this.setData()
// }
// }
}, {
key: 'setData',
value: function setData() {
this.setState({
height: this.props.height ? this.props.height : 100,
width: this.props.width ? this.props.width : 100,
textColor: this.props.textColor ? this.props.textColor : false,
fontFamily: this.props.fontFamily ? this.props.fontFamily : 'Verdana',
fontSize: this.props.fontSize ? this.props.fontSize : '30',
paddingLeft: this.props.paddingLeft ? this.props.paddingLeft : '20',
paddingTop: this.props.paddingTop ? this.props.paddingTop : '60',
lenght: this.props.lenght ? this.props.lenght : '5',
background: this.props.background ? this.props.background : 'none'
});
this.text = [];
this.originText = [];
this.possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < this.state.lenght; i++) {
var char = this.possible.charAt(Math.floor(Math.random() * this.possible.length));
this.text.push('<text \n font-family="' + this.state.fontFamily + '" \n font-size="' + this.state.fontSize + '" \n x="' + this.state.paddingLeft * i + '" \n y="' + this.state.paddingTop + '"\n fill="' + (this.props.textColor ? this.props.textColor : "#" + ((1 << 24) * Math.random() | 0).toString(16)) + '"\n transform="rotate(' + (Math.random() * (5 - 0) + 0) + ')"\n >' + char + '</text>');
this.originText.push(char);
}
this.props.result(this.originText.join(''));
}
}, {
key: 'render',
value: function render() {
return _react2.default.createElement('img', {
style: { background: this.state.background },
src: 'data:image/svg+xml;base64,' + btoa('<svg ' + 'xmlns="http://www.w3.org/2000/svg" ' + 'height="' + this.state.height + '" ' + 'width="' + this.state.width + '">' + this.text.join() + '</svg>'),
alt: '' });
}
}]);
return ReactCaptchaGenerator;
}(_react.Component);
exports.default = ReactCaptchaGenerator;