Created
June 16, 2019 06:25
-
-
Save NetanelBasal/6399164c02d4cdebea15356f9f8b88be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({...}) | |
export class CaptchaComponent implements OnInit, ControlValueAccessor { | |
@ViewChild('canvas', { static: true }) canvas: ElementRef<HTMLCanvasElement>; | |
answer: number; | |
ngOnInit() { this.createCaptcha(); } | |
... | |
createCaptcha() { | |
const ctx = this.canvas.nativeElement.getContext("2d"); | |
const [numOne, numTwo] = [random(), random()]; | |
this.answer = numOne + numTwo; | |
ctx.font = "30px Arial"; | |
ctx.fillText(`${numOne} + ${numTwo} = `, 10, 35); | |
} | |
change(value: string) { | |
this.onChange(value); | |
this.onTouched(); | |
} | |
} | |
function random() { | |
return Math.floor(Math.random() * 10) + 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment