-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
77 lines (59 loc) · 2.37 KB
/
main.js
File metadata and controls
77 lines (59 loc) · 2.37 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
// Get necessary elements from the HTML
const fileInput = document.getElementById('file-input');
const imageContainer = document.getElementById('image-container');
const clickableImage = document.getElementById('clickable-image');
const pointCounter = document.getElementById('point-counter');
// Initialize the score
let score = 0;
// Function to handle the image upload
fileInput.addEventListener('change', (event) => {
// Get the selected file
const file = event.target.files[0];
if (file) {
// Reset the environment for the new image
resetPoints();
// 1. Create a new FileReader object
const reader = new FileReader();
// 2. Define what happens when the file is successfully read
reader.onload = (e) => {
// e.target.result contains the data URL (base64 string) of the image
clickableImage.src = e.target.result;
// Make the image visible now that it has a source
clickableImage.style.visibility = 'visible';
};
// 3. Read the file as a Data URL
// This is a synchronous operation that triggers the onload event when complete
reader.readAsDataURL(file);
}
});
// Function to reset points when a new image is loaded
function resetPoints() {
// Clear all existing point elements from the container
const points = imageContainer.querySelectorAll('.point');
points.forEach(point => point.remove());
// Reset the counter
score = 0;
pointCounter.textContent = score;
}
// Event listener for adding points (remains the same)
imageContainer.addEventListener('click', (event) => {
// Only allow clicking if an image is actually loaded (i.e., has a src)
if (!clickableImage.src || clickableImage.style.visibility === 'hidden') {
alert("Please upload an image first!");
return;
}
if(score >= 15) {
alert("Too Many Points. (>15)");
return;
}
const x = event.offsetX;
const y = event.offsetY;
const newPoint = document.createElement('div');
newPoint.classList.add('point');
newPoint.style.left = `${x}px`;
newPoint.style.top = `${y}px`;
newPoint.style.backgroundColor = `rgb(${Math.random()*255}, ${Math.random()*255}, ${Math.random()*255})`;
imageContainer.appendChild(newPoint);
score++;
pointCounter.textContent = score;
});