UNPKG

fix-captcha

Version:

A customizable Captcha component for React.

97 lines (95 loc) 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateCaptchaText = exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _io = require("react-icons/io"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } //App.js const generateRandomChar = (min, max) => String.fromCharCode(Math.floor(Math.random() * (max - min + 1) + min)); const generateCaptchaText = length => { let captcha = ''; for (let i = 0; i < length / 3; i++) { captcha += generateRandomChar(65, 90); captcha += generateRandomChar(97, 122); captcha += generateRandomChar(48, 57); } return captcha.split('').sort(() => Math.random() - 0.5).join(''); }; exports.generateCaptchaText = generateCaptchaText; function Captcha(_ref) { let { refreshIconColor, refreshIconSize, placeholder, styles, length, textColor1, textColor2, initialSpace, fontSize, fontFamily, canvasContainerStyle, buttonStyle, canvasStyle, canvasHeight, canvasWidth, inputStyle } = _ref; const [captchaText, setCaptchaText] = (0, _react.useState)(''); const [userInput, setUserInput] = (0, _react.useState)(''); const canvasRef = (0, _react.useRef)(null); (0, _react.useEffect)(() => { const canvas = canvasRef.current; const ctx = canvas.getContext('2d'); initializeCaptcha(ctx); }, []); const drawCaptchaOnCanvas = (ctx, captcha) => { ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); const textColors = [textColor1, textColor2]; const letterSpace = 150 / captcha.length; for (let i = 0; i < captcha.length; i++) { const xInitialSpace = initialSpace; ctx.font = fontSize + " " + fontFamily; ctx.fillStyle = textColors[Math.floor(Math.random() * 2)]; ctx.fillText(captcha[i], xInitialSpace + i * letterSpace, // Randomize Y position slightly Math.floor(Math.random() * 16 + 25), 100); } }; const initializeCaptcha = ctx => { setUserInput(''); const newCaptcha = generateCaptchaText(length); setCaptchaText(newCaptcha); drawCaptchaOnCanvas(ctx, newCaptcha); }; const handleUserInputChange = e => { setUserInput(e.target.value); }; return /*#__PURE__*/_react.default.createElement("div", { style: styles }, /*#__PURE__*/_react.default.createElement("div", { style: canvasContainerStyle }, /*#__PURE__*/_react.default.createElement("canvas", { ref: canvasRef, width: canvasWidth, height: canvasHeight, style: canvasStyle }), /*#__PURE__*/_react.default.createElement("button", { type: "reset", style: buttonStyle, onClick: () => initializeCaptcha(canvasRef.current.getContext('2d')) }, /*#__PURE__*/_react.default.createElement(_io.IoMdRefresh, { color: refreshIconColor, size: refreshIconSize }))), /*#__PURE__*/_react.default.createElement("input", { type: "text", style: inputStyle, placeholder: placeholder, value: userInput, onChange: handleUserInputChange })); } var _default = exports.default = Captcha;