Skip to content

Instantly share code, notes, and snippets.

@NetanelBasal
Created June 16, 2019 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NetanelBasal/6399164c02d4cdebea15356f9f8b88be to your computer and use it in GitHub Desktop.
Save NetanelBasal/6399164c02d4cdebea15356f9f8b88be to your computer and use it in GitHub Desktop.
@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