UNPKG

react-canvas-captcha

Version:
155 lines (154 loc) 8.7 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = require("react"); var _jsxRuntime = require("react/jsx-runtime"); function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } var randomColor = function randomColor() { var r = Math.floor(Math.random() * 256); var g = Math.floor(Math.random() * 256); var b = Math.floor(Math.random() * 256); return 'rgb(' + r + ',' + g + ',' + b + ')'; }; var createCaptcha = function createCaptcha(canvas, config) { var showNum = []; var canvasWidth = config === null || config === void 0 ? void 0 : config.boxWidth; var canvasHeight = config.boxHeight; var context = canvas.getContext('2d'); canvas.width = canvasWidth; canvas.height = canvasHeight; var sCode = 'A,a,B,b,C,c,D,d,E,e,F,f,G,g,H,h,I,i,J,j,K,k,L,l,M,m,N,n,O,o,P,p,Q,q,R,r,S,s,T,t,U,u,V,v,W,w,X,x,Y,y,Z,z,0,1,2,3,4,5,6,7,8,9'; if ((config === null || config === void 0 ? void 0 : config.caseType) === 'uppercase') { sCode = 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9'; } if ((config === null || config === void 0 ? void 0 : config.caseType) === 'lowercase') { sCode = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9'; } var saCode = sCode.split(','); var saCodeLen = saCode.length; for (var i = 0; i < (config === null || config === void 0 ? void 0 : config.numberOfChars); i++) { var sIndex = Math.floor(Math.random() * saCodeLen); var sDeg = Math.random() * 30 * Math.PI / 180; var cTxt = saCode[sIndex]; showNum[i] = cTxt.toLowerCase(); var x = 10 + i * 20; var y = 20 + Math.random() * 8; context.font = config === null || config === void 0 ? void 0 : config.font; context.translate(x, y); context.rotate(sDeg); context.fillStyle = randomColor(); context.fillText(cTxt, config === null || config === void 0 ? void 0 : config.textStartingX, config === null || config === void 0 ? void 0 : config.textStartingY); context.rotate(-sDeg); context.translate(-x, -y); } for (var _i = 0; _i <= 5; _i++) { context.strokeStyle = randomColor(); context.beginPath(); context.moveTo(Math.random() * canvasWidth, Math.random() * canvasHeight); context.lineTo(Math.random() * canvasWidth, Math.random() * canvasHeight); context.stroke(); } for (var _i2 = 0; _i2 < 30; _i2++) { context.strokeStyle = randomColor(); context.beginPath(); var _x = Math.random() * canvasWidth; var _y = Math.random() * canvasHeight; context.moveTo(_x, _y); context.lineTo(_x + 1, _y + 1); context.stroke(); } return showNum.join(''); }; var Captcha = function Captcha(_ref) { var boxWidth = _ref.boxWidth, boxHeight = _ref.boxHeight, refreshButton = _ref.refreshButton, captchaConfig = _ref.captchaConfig, setCode = _ref.setCode, caseType = _ref.caseType; var canvasRef = (0, _react.useRef)(); var _useState = (0, _react.useState)(''), _useState2 = _slicedToArray(_useState, 2), captchaCode = _useState2[0], setCaptchaCode = _useState2[1]; var onChangeCaptcha = function onChangeCaptcha() { var newCaptcha = createCaptcha(canvasRef.current, _objectSpread({ boxHeight: boxHeight, boxWidth: boxWidth, caseType: caseType }, captchaConfig)); setCaptchaCode(newCaptcha); }; (0, _react.useEffect)(function () { setCode(captchaCode); // eslint-disable-next-line react-hooks/exhaustive-deps }, [captchaCode]); (0, _react.useEffect)(function () { var newCaptcha = createCaptcha(canvasRef.current, _objectSpread({ boxHeight: boxHeight, boxWidth: boxWidth, caseType: caseType }, captchaConfig)); setCaptchaCode(newCaptcha); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", { id: "captchaContainer", style: { display: 'flex', alignItems: 'center' }, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("canvas", { id: "captcha", ref: canvasRef, width: boxWidth, height: boxHeight, style: { borderRadius: 4, backgroundColor: "#d1d399" } }), refreshButton && /*#__PURE__*/(0, _jsxRuntime.jsxs)("svg", { xmlns: "http://www.w3.org/2000/svg", width: "30", height: "30", fill: "currentColor", className: "bi bi-arrow-clockwise", viewBox: "0 0 16 16", onClick: onChangeCaptcha, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("path", { fillRule: "evenodd", d: "M8 3a5 5 0 1 0 4.546 2.914.5.5 0 0 1 .908-.417A6 6 0 1 1 8 2v1z" }), /*#__PURE__*/(0, _jsxRuntime.jsx)("path", { d: "M8 4.466V.534a.25.25 0 0 1 .41-.192l2.36 1.966c.12.1.12.284 0 .384L8.41 4.658A.25.25 0 0 1 8 4.466z" })] })] }); }; Captcha.defaultProps = { boxHeight: 50, boxWidth: 130, refreshButton: false, caseType: 'mix', captchaConfig: { numberOfChars: 4, font: 'bold 23px Arial', textStartingX: 15, textStartingY: 5 }, setCode: function setCode() {} }; var _default = /*#__PURE__*/(0, _react.memo)(Captcha); exports["default"] = _default;