-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdepth.html
More file actions
478 lines (416 loc) · 17.8 KB
/
depth.html
File metadata and controls
478 lines (416 loc) · 17.8 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Depth Map Video Noise Animation – Uniform Foreground Speed (Seamless & Recordable)</title>
<style>
body {
background: #222;
color: #eee;
font-family: sans-serif;
text-align: center;
}
canvas {
border: 1px solid #555;
margin-top: 20px;
/* When scaled, force nearest-neighbor interpolation */
image-rendering: pixelated;
}
.controls {
margin-top: 10px;
}
.controls label,
.controls input,
.controls button {
margin: 5px;
font-size: 1rem;
}
/* Hide the video element (it supplies depth-map frames only) */
video {
display: none;
}
</style>
</head>
<body>
<h1>Depth Map Video Noise Animation – Uniform Foreground Speed (Seamless & Recordable)</h1>
<p>
Upload a grayscale depth map video or image. Pixels classified as foreground (bright areas) move uniformly while background pixels remain static.
</p>
<div class="controls">
<div>
<label>Depth Source:</label>
<label><input type="radio" name="depthSource" value="video" checked> Video</label>
<label><input type="radio" name="depthSource" value="image"> Image</label>
</div>
<br>
<div id="videoInputContainer">
<label for="depthVideoInput">Depth Map Video:</label>
<input type="file" id="depthVideoInput" accept="video/*">
</div>
<div id="imageInputContainer" style="display: none;">
<label for="depthImageInput">Depth Map Image:</label>
<input type="file" id="depthImageInput" accept="image/*">
</div>
<br>
<label for="speedInput">Foreground Speed (px/sec):</label>
<input type="number" id="speedInput" value="1" min="1" max="300">
<br>
<label for="lowerThresholdInput">Foreground Threshold (Lower Bound):</label>
<input type="range" id="lowerThresholdInput" value="128" min="0" max="255" step="1">
<span id="lowerThresholdValue">128</span>
<br>
<label for="upperThresholdInput">Foreground Threshold (Upper Bound):</label>
<input type="range" id="upperThresholdInput" value="255" min="0" max="255" step="1">
<span id="upperThresholdValue">255</span>
<br>
<label for="edgeThresholdInput">Edge Detection Threshold:</label>
<input type="range" id="edgeThresholdInput" value="30" min="0" max="100" step="1">
<span id="edgeThresholdValue">30</span>
<br>
<label for="movementDirection">Movement Direction:</label>
<select id="movementDirection">
<option value="vertical">Vertical</option>
<option value="horizontal">Horizontal</option>
</select>
<br>
<label for="noiseDensityInput">Noise Density (%):</label>
<input type="number" id="noiseDensityInput" value="50" min="1" max="100">
<br>
<label for="speckleSizeInput">Speckle Size (px):</label>
<input type="number" id="speckleSizeInput" value="2" min="1" max="10">
<br>
<label for="recordDurationInput">Record Duration (seconds):</label>
<input type="number" id="recordDurationInput" value="5" min="1" max="60">
<button id="recordButton">Record Video</button>
<br>
<button id="pauseButton">Pause</button>
</div>
<canvas id="noiseCanvas" width="960" height="540"></canvas>
<!-- The video element is used only for extracting depth-map frames -->
<video id="depthVideo" loop muted></video>
<script>
// === Canvas Setup ===
const canvas = document.getElementById('noiseCanvas');
const ctx = canvas.getContext('2d');
// Disable smoothing on the visible canvas.
ctx.imageSmoothingEnabled = false;
const width = canvas.width;
const height = canvas.height;
// === Global Parameters ===
let foregroundSpeed = parseFloat(document.getElementById('speedInput').value) || 1;
document.getElementById('speedInput').addEventListener('input', (e) => {
foregroundSpeed = parseFloat(e.target.value) || 1;
});
// Animation state
let isPaused = false;
let animationFrameId = null;
// Pause/Resume button
const pauseButton = document.getElementById('pauseButton');
pauseButton.addEventListener('click', () => {
isPaused = !isPaused;
pauseButton.textContent = isPaused ? 'Resume' : 'Pause';
if (!isPaused && !animationFrameId) {
// Resume animation if it was paused
animationFrameId = requestAnimationFrame(animate);
}
});
// Toggle between video and image depth source
const depthSourceRadios = document.querySelectorAll('input[name="depthSource"]');
const videoInputContainer = document.getElementById('videoInputContainer');
const imageInputContainer = document.getElementById('imageInputContainer');
depthSourceRadios.forEach(radio => {
radio.addEventListener('change', () => {
if (radio.value === 'video') {
videoInputContainer.style.display = 'block';
imageInputContainer.style.display = 'none';
} else {
videoInputContainer.style.display = 'none';
imageInputContainer.style.display = 'block';
}
});
});
// Update foregroundThreshold from slider
const lowerThresholdInput = document.getElementById('lowerThresholdInput');
const lowerThresholdValueDisplay = document.getElementById('lowerThresholdValue');
const upperThresholdInput = document.getElementById('upperThresholdInput');
const upperThresholdValueDisplay = document.getElementById('upperThresholdValue');
const edgeThresholdInput = document.getElementById('edgeThresholdInput');
const edgeThresholdValueDisplay = document.getElementById('edgeThresholdValue');
let lowerThreshold = 128; // pixels with brightness >= lowerThreshold are considered foreground
let upperThreshold = 255; // pixels with brightness <= upperThreshold are considered foreground
let edgeThreshold = 30; // threshold for edge detection
lowerThresholdInput.addEventListener('input', (e) => {
lowerThreshold = parseInt(e.target.value);
lowerThresholdValueDisplay.textContent = lowerThreshold;
// Ensure lower threshold doesn't exceed upper threshold
if (lowerThreshold > upperThreshold) {
upperThreshold = lowerThreshold;
upperThresholdInput.value = upperThreshold;
upperThresholdValueDisplay.textContent = upperThreshold;
}
});
upperThresholdInput.addEventListener('input', (e) => {
upperThreshold = parseInt(e.target.value);
upperThresholdValueDisplay.textContent = upperThreshold;
// Ensure upper threshold isn't below lower threshold
if (upperThreshold < lowerThreshold) {
lowerThreshold = upperThreshold;
lowerThresholdInput.value = lowerThreshold;
lowerThresholdValueDisplay.textContent = lowerThreshold;
}
});
edgeThresholdInput.addEventListener('input', (e) => {
edgeThreshold = parseInt(e.target.value);
edgeThresholdValueDisplay.textContent = edgeThreshold;
});
// Movement direction
let movementDirection = 'vertical'; // Default to vertical movement
document.getElementById('movementDirection').addEventListener('change', (e) => {
movementDirection = e.target.value;
// Regenerate noise field when direction changes to ensure seamless tiling
generateNoiseField();
});
// === Generate a Static, Seamless Noise Field ===
const noiseField = new Uint8ClampedArray(width * height);
let speckleSize = parseInt(document.getElementById('speckleSizeInput').value) || 2; // size (in pixels) of each noise block
let noiseDensity = parseInt(document.getElementById('noiseDensityInput').value) || 50; // percentage of white pixels
// Event listeners for noise parameters
document.getElementById('speckleSizeInput').addEventListener('change', () => {
speckleSize = parseInt(document.getElementById('speckleSizeInput').value) || 2;
generateNoiseField();
});
document.getElementById('noiseDensityInput').addEventListener('change', () => {
noiseDensity = 100-parseInt(document.getElementById('noiseDensityInput').value) || 50;
generateNoiseField();
});
function generateNoiseField() {
// Fill the noise field in blocks.
for (let y = 0; y < height; y += speckleSize) {
for (let x = 0; x < width; x += speckleSize) {
// Randomly choose black or white based on noise density.
const value = (Math.random() * 100 < noiseDensity) ? 255 : 0;
for (let dy = 0; dy < speckleSize && (y + dy) < height; dy++) {
for (let dx = 0; dx < speckleSize && (x + dx) < width; dx++) {
noiseField[(y + dy) * width + (x + dx)] = value;
}
}
}
}
// Make the noise field seamlessly tile in the appropriate direction
if (movementDirection === 'vertical') {
// Copy the first 'speckleSize' rows to the very bottom rows.
for (let y = 0; y < speckleSize; y++) {
const destY = height - speckleSize + y;
if (destY < height) {
for (let x = 0; x < width; x++) {
noiseField[destY * width + x] = noiseField[y * width + x];
}
}
}
} else { // horizontal
// Copy the first 'speckleSize' columns to the very right columns.
for (let x = 0; x < speckleSize; x++) {
const destX = width - speckleSize + x;
if (destX < width) {
for (let y = 0; y < height; y++) {
noiseField[y * width + destX] = noiseField[y * width + x];
}
}
}
}
}
generateNoiseField();
// === Offscreen Canvas for Depth Video Frames ===
const depthCanvas = document.createElement('canvas');
depthCanvas.width = width;
depthCanvas.height = height;
const depthCtx = depthCanvas.getContext('2d');
// Disable smoothing on the offscreen canvas as well.
depthCtx.imageSmoothingEnabled = false;
// Stores the current depth frame's pixel data.
let depthData = null;
const defaultDepthValue = 0;
// Flag to track if we're using image or video for depth
let usingDepthImage = false;
let depthImage = new Image();
// === Video Setup ===
const depthVideo = document.getElementById('depthVideo');
document.getElementById('depthVideoInput').addEventListener('change', (e) => {
const file = e.target.files[0];
if (!file) return;
const url = URL.createObjectURL(file);
depthVideo.src = url;
depthVideo.play();
usingDepthImage = false;
// Select video radio button when video is uploaded
document.querySelector('input[name="depthSource"][value="video"]').checked = true;
videoInputContainer.style.display = 'block';
imageInputContainer.style.display = 'none';
});
// === Depth Image Setup ===
document.getElementById('depthImageInput').addEventListener('change', (e) => {
const file = e.target.files[0];
if (!file) return;
const url = URL.createObjectURL(file);
depthImage = new Image();
depthImage.onload = () => {
// Clear the canvas and draw the image
depthCtx.clearRect(0, 0, width, height);
depthCtx.drawImage(depthImage, 0, 0, width, height);
// Get the pixel data immediately after drawing
depthData = depthCtx.getImageData(0, 0, width, height).data;
// Set the flag to use image instead of video
usingDepthImage = true;
// Force a redraw
if (isPaused) {
// If paused, just draw one frame
const timestamp = performance.now();
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
}
animate(timestamp);
}
console.log("Depth image loaded, dimensions:", depthImage.width, "x", depthImage.height);
};
depthImage.src = url;
// Select image radio button when image is uploaded
document.querySelector('input[name="depthSource"][value="image"]').checked = true;
videoInputContainer.style.display = 'none';
imageInputContainer.style.display = 'block';
});
// === Animation Loop ===
// For each pixel, we check its depth value from the video.
// If the pixel is foreground (brightness >= threshold), it moves uniformly.
let startTime = null;
function animate(timestamp) {
if (isPaused) {
animationFrameId = null;
return;
}
animationFrameId = requestAnimationFrame(animate);
if (!startTime) startTime = timestamp;
const elapsedSeconds = (timestamp - startTime) / 1000;
// Update the depth frame if available.
if (!usingDepthImage && depthVideo.readyState >= 2) { // HAVE_CURRENT_DATA
depthCtx.drawImage(depthVideo, 0, 0, width, height);
depthData = depthCtx.getImageData(0, 0, width, height).data;
}
// Draw the noise field with the appropriate offset.
const imageData = ctx.createImageData(width, height);
const data = imageData.data;
// Calculate the offset based on time and speed.
// For vertical movement, we need the y-offset.
// For horizontal movement, we need the x-offset.
const pixelsPerSecond = foregroundSpeed;
const totalOffset = pixelsPerSecond * elapsedSeconds;
// Modulo by the canvas dimension to ensure we loop.
const offset = movementDirection === 'vertical'
? Math.floor(totalOffset) % height
: Math.floor(totalOffset) % width;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
// Get the depth value (0-255) for this pixel.
// We only care about the red channel (index 0) since it's grayscale.
const i = (y * width + x) * 4;
// Default depth value if no depth data is available
let depth = defaultDepthValue;
// Use depth data if available
if (depthData) {
depth = depthData[i];
// Check if this pixel is at an edge by comparing with neighboring pixels
let isEdge = false;
// Only check for edges if we have depth data and the pixel is within the foreground threshold
if (depth >= lowerThreshold && depth <= upperThreshold) {
// Check neighboring pixels (with bounds checking)
const neighbors = [];
// Add neighboring pixels' depth values to the array
if (x > 0) neighbors.push(depthData[i - 4]); // left
if (x < width - 1) neighbors.push(depthData[i + 4]); // right
if (y > 0) neighbors.push(depthData[(y - 1) * width * 4 + x * 4]); // top
if (y < height - 1) neighbors.push(depthData[(y + 1) * width * 4 + x * 4]); // bottom
// Check if any neighbor has a significantly different depth value
for (const neighborDepth of neighbors) {
if (Math.abs(depth - neighborDepth) > edgeThreshold) {
isEdge = true;
break;
}
}
}
// If this pixel is at an edge, treat it as background
if (isEdge) {
depth = 0; // Force to background
}
}
// Determine the offset based on direction.
let offsetX = x;
let offsetY = y;
// Apply the offset only to foreground pixels.
if (depth >= lowerThreshold && depth <= upperThreshold) {
if (movementDirection === 'vertical') {
offsetY = (y + offset) % height;
} else {
offsetX = (x + offset) % width;
}
}
// Sample the noise field at the offset position.
const sampleIndex = offsetY * width + offsetX;
const noiseValue = noiseField[sampleIndex];
// Write the noise value into the frame data.
data[i + 0] = noiseValue;
data[i + 1] = noiseValue;
data[i + 2] = noiseValue;
data[i + 3] = 255;
}
}
ctx.putImageData(imageData, 0, 0);
}
// Start the animation
animationFrameId = requestAnimationFrame(animate);
// === Recording Functionality ===
let isRecording = false;
let mediaRecorder = null;
let recordedChunks = [];
const recordButton = document.getElementById('recordButton');
const recordDurationInput = document.getElementById('recordDurationInput');
recordButton.addEventListener('click', () => {
if (isRecording) return; // Prevent re-triggering if already recording
const durationSeconds = parseFloat(recordDurationInput.value) || 5;
const durationMs = durationSeconds * 1000;
// Capture the canvas stream (using 60 fps).
const stream = canvas.captureStream(60);
recordedChunks = [];
const options = { mimeType: 'video/webm; codecs=vp9' };
try {
mediaRecorder = new MediaRecorder(stream, options);
} catch (err) {
// Fallback if VP9 is not supported.
mediaRecorder = new MediaRecorder(stream);
}
mediaRecorder.ondataavailable = (e) => {
if (e.data.size > 0) {
recordedChunks.push(e.data);
}
};
mediaRecorder.onstop = () => {
const blob = new Blob(recordedChunks, { type: 'video/webm' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'recorded-video.webm';
a.click();
URL.revokeObjectURL(url);
};
mediaRecorder.start();
isRecording = true;
recordButton.textContent = 'Recording...';
// Stop recording after the specified duration.
setTimeout(() => {
mediaRecorder.stop();
isRecording = false;
recordButton.textContent = 'Record Video';
}, durationMs);
});
</script>
</body>
</html>