import { TextField } from "@mui/material"; import PropTypes from "prop-types"; import React, { Component } from "react"; // import "./captcha.css"; function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; } class Captcha extends Component { state = { solution: getRandomInt(111111, 999999), input: "", }; componentDidMount = () => { this.drawCaptcha(); // this.props.captchaSolution(this.state.solution.toString()); }; drawCaptcha = () => { const { solution } = this.state; const { width, height } = this.canvas; const ctx = this.canvas.getContext("2d"); ctx.clearRect(0, 0, width, height); ctx.font = "40px serif"; ctx.textAlign = "center"; ctx.textBaseline = "middle"; ctx.fillText(solution, width / 2, height / 2 + 3); ctx.strokeStyle = "#000000"; ctx.beginPath(); ctx.moveTo(getRandomInt(5, 20), getRandomInt(5, 20)); ctx.lineTo(width - getRandomInt(5, 20), height - getRandomInt(5, 20)); ctx.stroke(); ctx.moveTo(getRandomInt(5, 20), height - getRandomInt(5, 20)); ctx.lineTo(width - getRandomInt(5, 20), getRandomInt(5, 20)); ctx.stroke(); ctx.closePath(); this.canvas.style.backgroundColor = "transparent"; }; refresh = () => { this.setState( { solution: getRandomInt(111111, 999999), input: "", }, this.drawCaptcha ); }; playAudio = () => { const { solution } = this.state; let audio = new SpeechSynthesisUtterance( solution.toString().split("").join(" ") ); audio.rate = 0.25; window.speechSynthesis.speak(audio); }; handleChange = (e) => { const { onChange } = this.props; const { solution } = this.state; this.setState({ input: e.target.value }); onChange(e.target.value === solution.toString()); }; render() { const { input } = this.state; return (
(this.canvas = el)} width={200} height={50} className="rnc-canvas" data-testid="captcha-canvas" />
); } } Captcha.defaultProps = { placeholder: "کد امنیتی را وارد کنید...", }; Captcha.propTypes = { onChange: PropTypes.func.isRequired, placeholder: PropTypes.string, }; export default Captcha;