-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
312 lines (273 loc) · 12.1 KB
/
index.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<!DOCTYPE html>
<html lang="en">
<!-- Mirror Mirror a single page javascript web camera app by deftio -->
<!-- See github.com/deftio/MirrorMirror for more information -->
<head>
<meta charset="UTF-8">
<title>Mirror Mirror...</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body,
html {
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
display: flex;
flex-direction: column;
align-items: center;
background: black;
}
.controls {
width: 100%;
text-align: center;
z-index: 10;
position: fixed;
top: 0;
padding: 20px 0;
background: rgba(0, 0, 0, 0.8);
}
.title {
color: white;
font-size: 24px;
}
#canvas-container {
width: 100%;
height: calc(100% - 70px);
display: flex;
justify-content: center;
align-items: center;
margin-top: 70px;
overflow: hidden;
}
canvas {
max-width: 100%;
max-height: 100%;
}
</style>
</head>
<body>
<div class="controls">
<div class="title">Mirror Mirror</div>
<div class="btn-group" role="group">
<button id="btnToggle" class="btn btn-success">Turn On</button>
<button id="btnEffect" class="btn btn-warning">Infinity Effect</button>
<button id="btnEdge" class="btn btn-dark">Sobel Edge Detect</button>
<button id="btnCanny" class="btn btn-light">Canny Edge Detect</button>
<button id="btnFlip" class="btn btn-primary">Flip Horizontal</button>
<button id="btnRotate" class="btn btn-secondary">Rotate</button>
<button id="btnSave" class="btn btn-info">Save Picture</button>
</div>
</div>
<div id="canvas-container">
<canvas id="canvas"></canvas>
</div>
<script>
document.addEventListener('DOMContentLoaded', function () {
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
let videoStream = null;
let video = document.createElement('video');
let flipHorizontal = false;
let infinityEffect = false;
let rotationAngle = 0;
let edgeDetectionEnabled = false;
let cannyEdgeDetectionEnabled = false;
const btnToggle = document.getElementById('btnToggle');
const btnFlip = document.getElementById('btnFlip');
const btnEffect = document.getElementById('btnEffect');
const btnRotate = document.getElementById('btnRotate');
const btnSave = document.getElementById('btnSave');
document.getElementById('btnEdge').addEventListener('click', function () {
edgeDetectionEnabled = !edgeDetectionEnabled;
if (edgeDetectionEnabled) {
cannyEdgeDetectionEnabled = false; // Disable Canny if Edge Detection is enabled
}
});
document.getElementById('btnCanny').addEventListener('click', function () {
cannyEdgeDetectionEnabled = !cannyEdgeDetectionEnabled;
if (cannyEdgeDetectionEnabled) {
edgeDetectionEnabled = false; // Disable Edge Detection if Canny is enabled
}
});
btnToggle.addEventListener('click', function () {
if (videoStream) {
videoStream.getTracks().forEach(track => track.stop());
videoStream = null;
btnToggle.textContent = 'Turn On';
btnToggle.classList.replace('btn-danger', 'btn-success');
} else {
navigator.mediaDevices.getUserMedia({ video: true })
.then(stream => {
videoStream = stream;
video.srcObject = stream;
video.play();
btnToggle.textContent = 'Turn Off';
btnToggle.classList.replace('btn-success', 'btn-danger');
draw();
})
.catch(error => {
console.error('Error accessing the camera: ', error);
});
}
});
btnFlip.addEventListener('click', function () {
flipHorizontal = !flipHorizontal;
});
btnEffect.addEventListener('click', function () {
infinityEffect = !infinityEffect;
});
btnRotate.addEventListener('click', function () {
rotationAngle = (rotationAngle + 90) % 360;
canvas.style.transform = `rotate(${rotationAngle}deg)`;
});
btnSave.addEventListener('click', function () {
if (canvas) {
const link = document.createElement('a');
link.download = 'infinity-mirror.png';
link.href = canvas.toDataURL('image/png');
link.click();
}
});
function draw() {
if (!videoStream) return;
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
context.clearRect(0, 0, canvas.width, canvas.height);
if (flipHorizontal) {
context.save();
context.scale(-1, 1);
context.drawImage(video, -canvas.width, 0, canvas.width, canvas.height);
context.restore();
} else {
context.drawImage(video, 0, 0, canvas.width, canvas.height);
}
if (infinityEffect) {
applyInfinityEffect(context, canvas.width, canvas.height);
}
if (edgeDetectionEnabled) {
applyEdgeDetection(context, canvas.width, canvas.height);
}
if (cannyEdgeDetectionEnabled) {
applyCannyEdgeDetection(context, canvas.width, canvas.height);
}
requestAnimationFrame(draw);
}
function applyInfinityEffect(context, width, height) {
const numberOfReflections = 5;
let scaleFactor = 0.5;
let alphaFactor = 0.9;
for (let i = 0; i < numberOfReflections; i++) {
context.globalAlpha = Math.pow(alphaFactor, i + 1);
const newWidth = width * Math.pow(scaleFactor, i + 1);
const newHeight = height * Math.pow(scaleFactor, i + 1);
const dx = (width - newWidth) / 2;
const dy = (height - newHeight) / 2;
context.drawImage(canvas, 0, 0, width, height, dx, dy, newWidth, newHeight);
}
context.globalAlpha = 1.0;
}
function applyEdgeDetection(context, width, height) {
const imageData = context.getImageData(0, 0, width, height);
const grayScaled = grayscale(imageData.data, width, height);
const edgeData = sobelFilter(grayScaled, width, height);
context.putImageData(new ImageData(edgeData, width, height), 0, 0);
}
function applyCannyEdgeDetection(context, width, height) {
const imageData = context.getImageData(0, 0, width, height);
const grayScaled = grayscale(imageData.data, width, height);
const blurred = gaussianBlur(grayScaled, width, height);
const edgeData = cannyFilter(blurred, width, height);
context.putImageData(new ImageData(edgeData, width, height), 0, 0);
}
function grayscale(data, width, height) {
const result = new Uint8ClampedArray(width * height * 4);
for (let i = 0; i < data.length; i += 4) {
const avg = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2];
result[i] = avg; // red
result[i + 1] = avg; // green
result[i + 2] = avg; // blue
result[i + 3] = 255; // alpha
}
return result;
}
function gaussianBlur(data, width, height) {
const kernel = [
[1, 2, 1],
[2, 4, 2],
[1, 2, 1]
];
const kernelWeight = 16;
const blurredData = new Uint8ClampedArray(width * height * 4);
for (let y = 1; y < height - 1; y++) {
for (let x = 1; x < width - 1; x++) {
let sum = 0;
let sumAlpha = 0;
for (let cy = -1; cy <= 1; cy++) {
for (let cx = -1; cx <= 1; cx++) {
const pixel = ((y + cy) * width + (x + cx)) * 4;
const weight = kernel[cy + 1][cx + 1];
sum += data[pixel] * weight;
sumAlpha += data[pixel + 3];
}
}
const pixelIndex = (y * width + x) * 4;
blurredData[pixelIndex] = sum / kernelWeight;
blurredData[pixelIndex + 1] = sum / kernelWeight;
blurredData[pixelIndex + 2] = sum / kernelWeight;
blurredData[pixelIndex + 3] = sumAlpha / kernelWeight;
}
}
return blurredData;
}
function cannyFilter(data, width, height) {
// Simplified Canny edge detection
const sobelData = sobelFilter(data, width, height);
const result = new Uint8ClampedArray(width * height * 4);
for (let i = 0; i < sobelData.length; i += 4) {
const magnitude = sobelData[i];
if (magnitude > 50) {
result[i] = result[i + 1] = result[i + 2] = 255; // white
result[i + 3] = 255; // alpha
} else {
result[i] = result[i + 1] = result[i + 2] = 0; // black
result[i + 3] = 255; // alpha
}
}
return result;
}
function sobelFilter(data, width, height) {
const kernelX = [
[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]
];
const kernelY = [
[-1, -2, -1],
[0, 0, 0],
[1, 2, 1]
];
const sobelData = new Uint8ClampedArray(width * height * 4);
for (let y = 1; y < height - 1; y++) {
for (let x = 1; x < width - 1; x++) {
let px = (y * width + x) * 4;
let gx = 0;
let gy = 0;
for (let cy = -1; cy <= 1; cy++) {
for (let cx = -1; cx <= 1; cx++) {
const cpx = ((y + cy) * width + (x + cx)) * 4;
gx += data[cpx] * kernelX[cy + 1][cx + 1];
gy += data[cpx] * kernelY[cy + 1][cx + 1];
}
}
const magnitude = Math.sqrt(gx * gx + gy * gy);
sobelData[px] = sobelData[px + 1] = sobelData[px + 2] = magnitude;
sobelData[px + 3] = 255; // alpha
}
}
return sobelData;
}
});
</script>
</body>
</html>