效果图:
.ts
mounted() {
this.createCode();
}
private rand() {
let str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let arr = str.split("");
let ranNum = Math.floor(Math.random() * 36);
let randomCode = arr[ranNum];
return randomCode;
}
private drawline(canvas, context) {
context.beginPath();
context.moveTo(
Math.floor(Math.random() * (this as any).canvasWidth),
Math.floor(Math.random() * (this as any).canvasHeight)
);
context.lineTo(
Math.floor(Math.random() * (this as any).canvasWidth),
Math.floor(Math.random() * (this as any).canvasHeight)
);
context.lineWidth = 1;
context.strokeStyle = "#275DB3"; // 干扰线条的颜色
context.stroke();
}