1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| class Canvas { constructor() { this.curCanvas(); } curCanvas() { let myCanvas = document.querySelector("#canvas"); let ctx = myCanvas.getContext("2d"); let canvasH = myCanvas.offsetHeight; let canvasW = myCanvas.offsetWidth; ctx.clearRect(0, 0, canvasW, canvasH); ctx.fillStyle = "rgba(0,0,0,0.7)"; ctx.fillRect(0, 0, canvasW, canvasH); for (let i = 0; i < 200; i++) { this.producePoint(ctx, canvasW, canvasH); } let str = ""; let arr = this.allCharacter(); for (let i = 0; i < 4; i++) { str += arr[this.randomValue(0, 61)]; }
for (let i = 0; i < str.length; i++) { let colorR = this.randomValue(0, 256); let colorG = this.randomValue(0, 256); let colorB = this.randomValue(0, 256); let deg = this.randomValue(-30, 30); let x = this.randomValue(20, 30); let y = this.randomValue(20,30); ctx.font = "3rem sans-serif"; ctx.fillStyle = `rgb(${colorR},${colorG},${colorB})`; ctx.translate(x + 50 * i, y); ctx.rotate((Math.PI / 180) * deg); ctx.textBaseline ="top" ctx.fillText(str[i], 0, 0); ctx.rotate((Math.PI / 180) * -deg); ctx.translate(-(x + 50 * i), -y); } }
randomValue(min, max) { return Math.floor(Math.random() * (max - min)) + min; }
allCharacter() { let arr = []; for (let i = 48; i < 58; i++) { arr.push(String.fromCharCode(i)); } for (let j = 65; j < 123; j++) { if (j >= 91 && j <= 96) { continue; } arr.push(String.fromCharCode(j)); } return arr; }
producePoint(ctx, canvasW, canvasH) { ctx.beginPath(); let x = this.randomValue(5, canvasW - 5); let y = this.randomValue(5, canvasH - 5); let r = this.randomValue(2, 4); let colorR = this.randomValue(0, 256); let colorG = this.randomValue(0, 256); let colorB = this.randomValue(0, 256); ctx.arc(x, y, r, 0, Math.PI * 2); ctx.fillStyle = `rgb(${colorR},${colorG},${colorB})`; ctx.fill(); ctx.closePath(); } } new Canvas();
|