forked from sudiptasarkar011/FusionVision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserClass.js
106 lines (106 loc) · 3.44 KB
/
UserClass.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
var currentUser = null;
var userImages = {};
// Function to handle user login
function handleLogin(username, password) {
// In a real app, validate against backend
// For demo, just store username
currentUser = username;
// Load user's saved images from localStorage
var savedData = localStorage.getItem(username);
if (savedData) {
userImages[username] = JSON.parse(savedData);
}
else {
userImages[username] = [];
}
// Update welcome message
updateWelcomeMessage();
}
// Function to handle user registration
function handleRegistration(username, password) {
// In a real app, store in backend
// For demo, just initialize storage
currentUser = username;
userImages[username] = [];
localStorage.setItem(username, JSON.stringify([]));
updateWelcomeMessage();
}
// Function to update welcome message
function updateWelcomeMessage() {
var welcomeDiv = document.createElement('div');
welcomeDiv.id = 'welcome-message';
welcomeDiv.style.color = 'white';
welcomeDiv.style.padding = '10px';
welcomeDiv.style.textAlign = 'center';
welcomeDiv.innerHTML = "Welcome, ".concat(currentUser, "!");
// Insert at top of main content
var mainElement = document.querySelector('main');
if (mainElement) {
mainElement.prepend(welcomeDiv);
}
}
// Modified captureImage function to save images
function captureImage() {
if (!currentUser) {
alert('Please log in first!');
return;
}
var canvas = document.getElementById("capturedCanvas");
if (!canvas)
return;
var context = canvas.getContext("2d");
if (!context)
return;
var video = document.getElementById("cameraPreview");
if (!video)
return;
// Set canvas dimensions to match video
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
// Draw video frame to canvas
context.drawImage(video, 0, 0, canvas.width, canvas.height);
// Save image data
var imageData = canvas.toDataURL('image/jpeg');
if (currentUser && userImages[currentUser]) {
userImages[currentUser].push({
timestamp: new Date().toISOString(),
data: imageData
});
// Save to localStorage
localStorage.setItem(currentUser, JSON.stringify(userImages[currentUser]));
}
}
// Function to handle logout
function handleLogout() {
if (currentUser) {
// Save any pending data
localStorage.setItem(currentUser, JSON.stringify(userImages[currentUser]));
currentUser = null;
// Remove welcome message
var welcomeMsg = document.getElementById('welcome-message');
if (welcomeMsg) {
welcomeMsg.remove();
}
// Redirect to login page
window.location.href = 'index.html';
}
}
// Function to display user's saved images
function displaySavedImages() {
if (!currentUser || !userImages[currentUser])
return;
var images = userImages[currentUser];
var container = document.createElement('div');
container.className = 'saved-images';
images.forEach(function (img) {
var imgElement = document.createElement('img');
imgElement.src = img.data;
imgElement.style.maxWidth = '200px';
imgElement.style.margin = '10px';
container.appendChild(imgElement);
});
var mainElement = document.querySelector('main');
if (mainElement) {
mainElement.appendChild(container);
}
}