-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
99 lines (76 loc) · 3.14 KB
/
Copy pathscript.js
File metadata and controls
99 lines (76 loc) · 3.14 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
// Javascript Code File
console.log("RecycleRight is running!");
// Variables
const heading1 = document.querySelector("h1");
const paragraphs = document.querySelectorAll(".description");
const itemInput = document.querySelector(".item-input");
const camera = document.querySelector(".camera");
const photoButton = document.querySelector(".photo-button")
const canvas = document.querySelector(".snapshot");
const retakeButton = document.querySelector(".retake-button")
const analyzeButton = document.querySelector(".analyze-button")
canvas.style.display = "none";
let cameraStream;
heading1.textContent = "Welcome to RecycleRight!";
// use document.querySelector to select elements or classes from the HTML file.
const scanButton = document.querySelector(".scan-button");
// Add an event listener to the scanButton that triggers an alert when clicked.
scanButton.addEventListener("click", function() {
heading1.textContent = "Scanner Button Clicked!";
paragraphs[0].textContent = "You have successfully scanned an item!";
paragraphs[1].textContent = "Good work man";
});
// function with paragraph as the parameter
paragraphs.forEach(function(paragraph, index) {
paragraph.textContent = "Description #" + (index + 1);
});
// Function to check items and update paragraph text based on it
scanButton.addEventListener("click", function() {
let item = itemInput.value.trim().toLowerCase();
if (item === "plastic") {
heading1.textContent = "The item is Plastic";
paragraphs[0].textContent = "Plastic detected!";
paragraphs[1].textContent = "Check whether this type of plastic is recyclable.";
} else if (item === "glass") {
paragraphs[0].textContent = "Glass Detected!";
paragraphs[1].textContent = "Glass may need to be separated from other recyclables.";
} else if (item === "metal") {
paragraphs[0].textContent = "Metal Detected!"
paragraphs[1].textContent = "Metal needs to be safely recycled or disposed based on its type.";
}
else {
heading1.textContent = "Unknown item";
paragraphs[0].textContent = "We couldn't identify this item.";
paragraphs[1].textContent = "Try scanning again.";
}
});
// getUserMedia() → requests camera/microphone access
// stream → the live camera data
// srcObject → connects that stream to the <video> element
scanButton.addEventListener("click", function() {
startCamera();
});
// context.drawImage(source, x, y, width, height);
photoButton.addEventListener("click", function() {
const context = canvas.getContext("2d");
context.drawImage(camera, 0, 0, 400, 300);
canvas.style.display = "block";
cameraStream.getTracks().forEach(function(track) {
track.stop();
});
});
function startCamera() {
navigator.mediaDevices.getUserMedia({ video: true })
.then(function(stream) {
cameraStream = stream;
camera.srcObject = stream;
});
}
// retakeButton
retakeButton.addEventListener("click", function() {
canvas.style.display = "none";
startCamera();
});
// analyzeButton
analyzeButton.addEventListener("click", function() {
});