-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexWithWebcam.html
121 lines (101 loc) · 3.73 KB
/
indexWithWebcam.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Live Colored ASCII Art Converter</title>
<style>
* {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
#ascii-art {
font-family: monospace;
white-space: pre;
font-size: 8px;
}
.ascii-char {
display: inline-block;
width: 8px;
height: 8px;
line-height: 8px;
}
button,
input[type="file"] {
border: none;
padding: 10px;
cursor: pointer;
font-weight: bold;
box-shadow: 0 0 10px rgb(201, 201, 201) inset;
border-radius: 10px;
padding: 10px 50px;
}
button:hover {
background-color: rgb(0, 102, 255);
color: white;
box-shadow: none;
transition-duration: 200ms;
}
</style>
</head>
<body>
<h1>Live webcam ascii viewer</h1>
<label>Background Color: <input type="checkbox" id="backgroundColor"></label><br>
<label>Frame Delay: <input type="range" id="frameDelay" min="50" max="1000" value="300" onchange="frameDelayViewer.innerText=frameDelay.value"><span id="frameDelayViewer">300</span></label>
<p>
<button id="startButton" onclick="startCamera()">Start</button></p>
<div id="ascii-art"></div>
<a href="http://safakamali.ir" target="_blank">
<h2>Designed By Mohammad Safa Kamali: safakamali.ir</h2>
</a>
<script>
const asciiArtDiv = document.getElementById('ascii-art');
const isOnBackgroundColor = document.getElementById('backgroundColor');
const frameDelay = document.getElementById('frameDelay');
const frameDelayViewer = document.getElementById('frameDelayViewer');
async function startCamera() {
try {
const stream = await navigator.mediaDevices.getUserMedia({
video: true
});
const video = document.createElement('video');
video.srcObject = stream;
video.play();
video.addEventListener('play', function () {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = 50;
canvas.height = 50;
setInterval(function () {
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
const asciiArt = convertToColoredAscii(imageData, canvas.width);
asciiArtDiv.innerHTML = asciiArt;
}, frameDelay.value); // Update every 100 milliseconds
});
} catch (error) {
console.error('Error accessing the camera:', error);
}
}
function convertToColoredAscii(imageData, width) {
const asciiChars = ['@', '#', 'S', '%', '?', '*', '+', ';', ':', ',', '.'];
let asciiArt = '';
let elementStyle = '';
if (isOnBackgroundColor.checked) {
elementStyle = 'background-color: {color};';
}
for (let i = 0; i < imageData.length; i += 4) {
const avgBrightness = (imageData[i] + imageData[i + 1] + imageData[i + 2]) / 3;
const asciiIndex = Math.round((avgBrightness / 255) * (asciiChars.length - 1));
const color = `rgb(${imageData[i]}, ${imageData[i + 1]}, ${imageData[i + 2]})`;
const span =
`<span class="ascii-char" style="${elementStyle.replace('{color}', color)}">${asciiChars[asciiIndex]}</span>`;
asciiArt += span;
if ((i / 4) % width === 0 && i !== 0) {
asciiArt += '<br>';
}
}
return asciiArt;
}
</script>
</body>
</html>