-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
167 lines (150 loc) · 4.59 KB
/
index.html
File metadata and controls
167 lines (150 loc) · 4.59 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Visual Impairment Simulator</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; background: black; overflow: hidden; }
#container { display: flex; height: 100vh; width: 100vw; }
canvas { object-fit: cover; }
#leftView, #rightView {
width: 50vw;
height: 100vh;
}
.single #rightView {
display: none;
}
.single #leftView {
width: 100vw;
}
#controls {
position: fixed;
top: 10px;
left: 50%;
transform: translateX(-50%);
z-index: 10;
background: white;
padding: 10px;
border-radius: 8px;
font-size: 14px;
display: flex;
gap: 10px;
flex-wrap: wrap;
justify-content: center;
}
select {
font-size: 14px;
}
</style>
</head>
<body>
<div id="controls">
<select id="filterSelector">
<option value="normal">Normal</option>
<option value="cataract">Cataract</option>
<option value="glaucoma">Glaucoma</option>
<option value="macular">Macular Degeneration</option>
</select>
<select id="cameraSelector">
<option value="environment">Back Camera</option>
<option value="user">Front Camera</option>
</select>
<select id="viewModeSelector">
<option value="split">Google Cardboard (Split View)</option>
<option value="single">Normal View (Single Screen)</option>
</select>
</div>
<div id="container" class="split">
<canvas id="leftView"></canvas>
<canvas id="rightView"></canvas>
</div>
<script>
const leftCanvas = document.getElementById('leftView');
const rightCanvas = document.getElementById('rightView');
const leftCtx = leftCanvas.getContext('2d');
const rightCtx = rightCanvas.getContext('2d');
const filterSelector = document.getElementById('filterSelector');
const cameraSelector = document.getElementById('cameraSelector');
const viewModeSelector = document.getElementById('viewModeSelector');
const container = document.getElementById('container');
let filterType = 'normal';
let facingMode = 'environment';
let stream = null;
const video = document.createElement('video');
video.autoplay = true;
video.playsInline = true;
filterSelector.addEventListener('change', (e) => {
filterType = e.target.value;
});
cameraSelector.addEventListener('change', (e) => {
facingMode = e.target.value;
startCamera();
});
viewModeSelector.addEventListener('change', (e) => {
container.className = e.target.value; // 'split' or 'single'
});
function startCamera() {
stopCamera();
navigator.mediaDevices.getUserMedia({
video: { facingMode: { exact: facingMode } }
}).then(s => {
stream = s;
video.srcObject = stream;
}).catch(err => {
alert("Error accessing camera: " + err.message);
});
}
function stopCamera() {
if (stream) {
stream.getTracks().forEach(track => track.stop());
stream = null;
}
}
function applyFilter(ctx, canvas) {
if (filterType === 'cataract') {
ctx.globalAlpha = 0.6;
ctx.fillStyle = 'rgba(255,255,255,0.3)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.filter = 'blur(5px) contrast(70%)';
} else if (filterType === 'glaucoma') {
ctx.save();
ctx.beginPath();
ctx.arc(canvas.width / 2, canvas.height / 2, canvas.width / 4, 0, 2 * Math.PI);
ctx.clip();
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
ctx.restore();
ctx.fillStyle = 'rgba(0,0,0,0.9)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.globalCompositeOperation = 'destination-in';
ctx.beginPath();
ctx.arc(canvas.width / 2, canvas.height / 2, canvas.width / 4, 0, 2 * Math.PI);
ctx.fill();
ctx.globalCompositeOperation = 'source-over';
} else if (filterType === 'macular') {
ctx.beginPath();
ctx.fillStyle = 'rgba(0,0,0,0.7)';
ctx.arc(canvas.width / 2, canvas.height / 2, canvas.width / 6, 0, 2 * Math.PI);
ctx.fill();
}
ctx.filter = 'none';
ctx.globalAlpha = 1;
}
function draw(ctx, canvas) {
if (video.readyState < 2) return;
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
applyFilter(ctx, canvas);
}
function loop() {
draw(leftCtx, leftCanvas);
if (container.className === 'split') draw(rightCtx, rightCanvas);
requestAnimationFrame(loop);
}
startCamera();
loop();
</script>
</body>
</html>