-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
475 lines (398 loc) · 16.2 KB
/
script.js
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
class DottedEffect {
constructor() {
// Canvas and context
this.canvas = document.getElementById('outputCanvas');
this.ctx = this.canvas.getContext('2d');
this.fileInput = document.getElementById('fileInput');
// Container elements
this.dropZone = document.querySelector('.drop-zone');
this.dropMessage = document.querySelector('.drop-message');
// Settings panel elements
this.settingsPanel = document.querySelector('.settings-panel');
this.settingsToggle = document.querySelector('.settings-toggle');
this.dotSizeInput = document.getElementById('dotSize');
this.spacingInput = document.getElementById('spacing');
this.thresholdInput = document.getElementById('threshold');
// Button elements
this.downloadBtn = document.getElementById('downloadBtn');
this.downloadVideoBtn = document.getElementById('downloadVideo');
this.clearBtn = document.getElementById('clearMedia');
this.cancelBtn = document.getElementById('cancelProcessing');
// Media state
this.currentMedia = null;
this.isRecording = false;
this.mediaRecorder = null;
this.recordedChunks = [];
// Hide buttons initially
this.downloadBtn.style.display = 'none';
this.downloadVideoBtn.style.display = 'none';
this.cancelBtn.style.display = 'none';
this.clearBtn.style.display = 'none';
// Add resize observer
this.resizeObserver = new ResizeObserver(() => {
if (this.currentMedia) {
this.updateCanvasSize();
}
});
this.resizeObserver.observe(this.dropZone);
this.setupEventListeners();
window.addEventListener('resize', () => this.updateCanvasSize());
this.showDropMessage();
}
setupEventListeners() {
// Settings panel toggle
this.settingsToggle.addEventListener('click', () => {
this.settingsPanel.classList.toggle('retracted');
});
// Slider event listeners
this.dotSizeInput.addEventListener('input', () => this.applyEffect());
this.spacingInput.addEventListener('input', () => this.applyEffect());
this.thresholdInput.addEventListener('input', () => this.applyEffect());
// File input event listeners
this.fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (!file) return;
if (file.type.startsWith('image/')) {
this.handleImageUpload(file);
} else if (file.type.startsWith('video/')) {
this.handleVideoUpload(file);
}
});
// Drop zone event listeners
this.dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
e.stopPropagation();
this.dropZone.classList.add('dragover');
});
this.dropZone.addEventListener('dragleave', (e) => {
e.preventDefault();
e.stopPropagation();
this.dropZone.classList.remove('dragover');
});
this.dropZone.addEventListener('drop', (e) => {
e.preventDefault();
e.stopPropagation();
this.dropZone.classList.remove('dragover');
const file = e.dataTransfer.files[0];
if (!file) return;
if (file.type.startsWith('image/')) {
this.handleImageUpload(file);
} else if (file.type.startsWith('video/')) {
this.handleVideoUpload(file);
}
});
// Click on drop zone to trigger file input
this.dropZone.addEventListener('click', (e) => {
if (!this.currentMedia && this.dropMessage.style.display === 'block') {
this.fileInput.click();
}
});
// Button event listeners
this.downloadBtn.addEventListener('click', () => this.downloadResult());
this.downloadVideoBtn.addEventListener('click', () => this.processVideo());
this.cancelBtn.addEventListener('click', () => this.cancelProcessing());
this.clearBtn.addEventListener('click', () => this.clearMedia());
}
handleFileSelect(event) {
const file = event.target.files[0];
if (!file) return;
if (file.type.startsWith('image/')) {
this.handleImageUpload(file);
} else if (file.type.startsWith('video/')) {
this.handleVideoUpload(file);
}
}
handleImageUpload(file) {
const reader = new FileReader();
reader.onload = (e) => {
const img = new Image();
img.onload = () => {
this.currentMedia = {
type: 'image',
element: img,
width: img.width,
height: img.height
};
this.hideDropMessage();
this.downloadBtn.style.display = 'block';
this.downloadVideoBtn.style.display = 'none';
this.clearBtn.style.display = 'block';
this.cancelBtn.style.display = 'none';
this.updateCanvasSize();
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
handleVideoUpload(file) {
const url = URL.createObjectURL(file);
this.video = document.createElement('video');
this.video.src = url;
this.video.style.display = 'none';
this.video.loop = true;
this.video.muted = true;
this.video.onloadedmetadata = () => {
this.currentMedia = {
type: 'video',
element: this.video,
width: this.video.videoWidth,
height: this.video.videoHeight
};
this.hideDropMessage();
this.downloadBtn.style.display = 'none';
this.downloadVideoBtn.style.display = 'block';
this.clearBtn.style.display = 'block';
this.cancelBtn.style.display = 'none';
this.updateCanvasSize();
this.startVideoPlayback();
};
}
startVideoPlayback() {
if (!this.currentMedia || this.currentMedia.type !== 'video') return;
const video = this.currentMedia.element;
video.play();
const animate = () => {
if (!this.currentMedia || this.currentMedia.type !== 'video') return;
this.applyEffect();
this.animationFrame = requestAnimationFrame(animate);
};
animate();
}
applyEffect() {
if (!this.currentMedia) return;
const dotSize = parseInt(this.dotSizeInput.value);
const spacing = parseInt(this.spacingInput.value);
const threshold = parseInt(this.thresholdInput.value);
const sourceElement = this.currentMedia.element;
this.processImage(sourceElement, this.canvas.width, this.canvas.height, dotSize, spacing, threshold);
}
processImage(source, width, height, dotSize = 5, spacing = 8, threshold = 128, ctx = this.ctx) {
// Create a temporary canvas for processing
const tempCanvas = document.createElement('canvas');
const tempCtx = tempCanvas.getContext('2d');
tempCanvas.width = width;
tempCanvas.height = height;
// Draw the source image/video frame
tempCtx.drawImage(source, 0, 0, width, height);
const imageData = tempCtx.getImageData(0, 0, width, height);
const data = imageData.data;
// Clear the target canvas
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = 'white';
// Process pixels
for (let y = 0; y < height; y += spacing) {
for (let x = 0; x < width; x += spacing) {
const i = (Math.floor(y) * width + Math.floor(x)) * 4;
const brightness = (data[i] + data[i + 1] + data[i + 2]) / 3;
if (brightness > threshold) {
const radius = dotSize * (brightness / 255);
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.fill();
}
}
}
}
downloadResult() {
// Create a full resolution version
const originalWidth = this.currentMedia.width;
const originalHeight = this.currentMedia.height;
const hiddenCanvas = document.createElement('canvas');
hiddenCanvas.width = originalWidth;
hiddenCanvas.height = originalHeight;
const hiddenCtx = hiddenCanvas.getContext('2d');
const dotSize = parseInt(this.dotSizeInput.value);
const spacing = parseInt(this.spacingInput.value);
const threshold = parseInt(this.thresholdInput.value);
this.processImage(
this.currentMedia.element,
originalWidth,
originalHeight,
dotSize,
spacing,
threshold,
hiddenCtx
);
// Download at full resolution
const link = document.createElement('a');
link.download = 'dotted-effect.png';
link.href = hiddenCanvas.toDataURL('image/png');
link.click();
}
processVideo() {
if (this.isRecording || !this.currentMedia || this.currentMedia.type !== 'video') return;
// Pause the looping preview while processing
cancelAnimationFrame(this.animationFrame);
this.currentMedia.element.pause();
this.isRecording = true;
this.downloadVideoBtn.classList.add('processing');
this.downloadVideoBtn.disabled = true;
this.cancelBtn.style.display = 'inline-block';
// Start dots animation
let dots = 0;
const updateDots = () => {
if (!this.isRecording) return;
dots = (dots + 1) % 4;
this.downloadVideoBtn.textContent = 'Processing' + '.'.repeat(dots);
setTimeout(updateDots, 500);
};
updateDots();
// Set hidden canvas to original video dimensions
const originalWidth = this.currentMedia.width;
const originalHeight = this.currentMedia.height;
const hiddenCanvas = document.createElement('canvas');
hiddenCanvas.width = originalWidth;
hiddenCanvas.height = originalHeight;
const hiddenCtx = hiddenCanvas.getContext('2d');
// Use basic WebM format with higher bitrate
const stream = hiddenCanvas.captureStream();
try {
this.mediaRecorder = new MediaRecorder(stream, {
mimeType: 'video/webm',
videoBitsPerSecond: 8000000
});
} catch (e) {
console.error('MediaRecorder error:', e);
return;
}
this.recordedChunks = [];
this.mediaRecorder.ondataavailable = (e) => {
if (e.data.size > 0) {
this.recordedChunks.push(e.data);
}
};
this.mediaRecorder.onstop = () => {
if (this.recordedChunks.length > 0) {
const blob = new Blob(this.recordedChunks, { type: 'video/webm' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'dotted-effect.webm';
a.click();
URL.revokeObjectURL(url);
}
this.finishProcessing();
// Resume the looping preview
this.startVideoPlayback();
};
// Set up video for processing
const video = this.currentMedia.element;
video.currentTime = 0;
video.playbackRate = 1;
video.loop = false; // Disable looping during processing
const dotSize = parseInt(this.dotSizeInput.value);
const spacing = parseInt(this.spacingInput.value);
const threshold = parseInt(this.thresholdInput.value);
// Start recording immediately
this.mediaRecorder.start();
const processFrame = () => {
if (!this.isRecording) {
video.pause();
this.mediaRecorder.stop();
return;
}
// Process the current frame
this.processImage(
video,
originalWidth,
originalHeight,
dotSize,
spacing,
threshold,
hiddenCtx
);
// Check if we've reached the end
if (video.currentTime >= video.duration - 0.1) {
video.pause();
this.mediaRecorder.stop();
} else {
requestAnimationFrame(processFrame);
}
};
// Start processing
video.play();
processFrame();
}
cancelProcessing() {
if (this.isRecording) {
this.isRecording = false;
this.video.pause();
this.video.currentTime = 0;
if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
this.mediaRecorder.stop();
}
}
}
finishProcessing() {
this.isRecording = false;
this.downloadVideoBtn.classList.remove('processing');
this.downloadVideoBtn.disabled = false;
this.downloadVideoBtn.textContent = 'Process Video';
this.cancelBtn.style.display = 'none';
this.recordedChunks = [];
this.mediaRecorder = null;
// Re-enable looping for preview
if (this.currentMedia && this.currentMedia.type === 'video') {
this.currentMedia.element.loop = true;
}
}
clearMedia() {
if (this.currentMedia && this.currentMedia.type === 'video') {
cancelAnimationFrame(this.animationFrame);
this.currentMedia.element.pause();
this.currentMedia.element.src = '';
}
this.currentMedia = null;
this.downloadBtn.style.display = 'none';
this.downloadVideoBtn.style.display = 'none';
this.clearBtn.style.display = 'none';
this.cancelBtn.style.display = 'none';
this.canvas.width = 0;
this.canvas.height = 0;
this.canvas.style.display = 'none';
// Keep dropzone empty until clicked
this.dropMessage.style.display = 'none';
this.dropZone.style.cursor = 'pointer';
const onClick = (e) => {
e.stopPropagation(); // Prevent triggering the file input click
this.showDropMessage();
this.dropZone.removeEventListener('click', onClick);
};
// Remove any existing click handlers and add new one
this.dropZone.removeEventListener('click', onClick);
this.dropZone.addEventListener('click', onClick);
}
showDropMessage() {
this.dropMessage.style.display = 'block';
this.canvas.style.display = 'none';
}
hideDropMessage() {
this.dropMessage.style.display = 'none';
this.canvas.style.display = 'block';
}
updateCanvasSize() {
if (!this.currentMedia) return;
const containerWidth = this.dropZone.clientWidth;
const containerHeight = this.dropZone.clientHeight;
const imageAspect = this.currentMedia.width / this.currentMedia.height;
let canvasWidth, canvasHeight;
if (containerWidth / containerHeight > imageAspect) {
// Container is wider than image aspect ratio
canvasHeight = Math.min(containerHeight, this.currentMedia.height);
canvasWidth = canvasHeight * imageAspect;
} else {
// Container is taller than image aspect ratio
canvasWidth = Math.min(containerWidth, this.currentMedia.width);
canvasHeight = canvasWidth / imageAspect;
}
// Update canvas size
this.canvas.width = canvasWidth;
this.canvas.height = canvasHeight;
this.canvas.style.display = 'block';
// Apply the effect immediately
this.applyEffect();
}
}
// Initialize the application
new DottedEffect();