-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcap.html
72 lines (57 loc) · 2.46 KB
/
cap.html
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
<!DOCTYPE html>
<html lang="pt">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gerar Imagem com Texto Aleatório</title>
</head>
<body>
<div id="c"></div>
<br>
<span id="result"></span>
<br>
<!--button onclick="gerarImagem()">Gerar Texto Aleatório</button-->
<script>
function gerarTextoAleatorio(tamanho) {
const caracteres = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let texto = '';
for (let i = 0; i < tamanho; i++) {
texto += caracteres.charAt(Math.floor(Math.random() * caracteres.length));
}
return texto;
}
function gerarImagem() {
document.querySelectorAll('#canvasimg').forEach(e => e.remove());
const mainbody = document.getElementById('c'); //or target div
const divbtn = document.createElement('canvas');
divbtn.setAttribute('id','canvas');
divbtn.setAttribute('width','400');
divbtn.setAttribute('height','100');
mainbody.appendChild(divbtn);
const canvas = document.getElementById('canvas');
const contexto = canvas.getContext('2d');
const textoAleatorio = gerarTextoAleatorio(10); // Gera um texto aleatório com 10 caracteres
document.getElementById('result').innerHTML = textoAleatorio;
// Limpar o canvas
contexto.clearRect(0, 0, canvas.width, canvas.height);
contexto.fillStyle = "white";
contexto.fillRect(0, 0, canvas.width, canvas.height);
// Definir o estilo da fonte e cor
contexto.font = '30px Arial';
contexto.fillStyle = 'black';
// Desenhar o texto no canvas
contexto.fillText(textoAleatorio, 50, 50);
const img = canvas.toDataURL('image/jpeg');
const canvasimg = document.createElement('img');
canvasimg.setAttribute('id','canvasimg');
canvasimg.setAttribute('width','400');
canvasimg.setAttribute('height','100');
canvasimg.src = img;
document.querySelectorAll('#canvas').forEach(e => e.remove());
mainbody.appendChild(canvasimg);
}
// Gera uma imagem ao carregar a página
gerarImagem();
</script>
</body>
</html>