-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathindex.html
More file actions
209 lines (177 loc) · 7.62 KB
/
index.html
File metadata and controls
209 lines (177 loc) · 7.62 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>List-Builder - Scan to Cart Simulation</title>
<link rel="stylesheet" href="./index.css">
<!-- Include Dynamsoft Barcode Reader Bundle via CDN -->
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader-bundle@11.4.2000/dist/dbr.bundle.js"></script>
<!-- If the network is unstable or you prefer to self-host the SDK, uncomment the line below to load it locally -->
<!-- <script src="../../../dist/dbr.bundle.js"></script> -->
</head>
<body>
<header>
<h1>🛒 Scan Barcode to Add Items to Cart</h1>
</header>
<!-- Scan Button -->
<button class="scan-btn">
Scan Barcode
<svg class="barcode-icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect x="2" y="4" width="2" height="16" fill="black" />
<rect x="6" y="4" width="1" height="16" fill="black" />
<rect x="9" y="4" width="2" height="16" fill="black" />
<rect x="13" y="4" width="1" height="16" fill="black" />
<rect x="16" y="4" width="2" height="16" fill="black" />
<rect x="20" y="4" width="1" height="16" fill="black" />
</svg>
</button>
<!-- Floating Scanner Container -->
<div class="floatingDiv" hidden>
<svg class="btn-close" id="closed" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="26" height="26" viewBox="0 0 24 24" style="display: inline;">
<g id="Group_2681" data-name="Group 2681">
<path id="Path_8292" data-name="Path 8292" d="M12,0h0A12,12,0,1,0,24,12,12.013,12.013,0,0,0,12,0m0,1.914A10.086,10.086,0,1,1,1.914,12,10.1,10.1,0,0,1,12,1.914" fill="#fff"></path>
<path id="Path_8293" data-name="Path 8293" d="M7.416,16.578a.985.985,0,0,0,1.351,0L12,13.35l3.21,3.208a.958.958,0,0,0,1.665-.6.988.988,0,0,0-.3-.738L13.351,12l3.226-3.226a.959.959,0,0,0,0-1.353l0,0a.959.959,0,0,0-1.353,0L12,10.647,8.755,7.4A.957.957,0,0,0,7.42,8.774L10.646,12,7.421,15.227a.956.956,0,0,0-.005,1.351" fill="#fff"></path>
</g>
</svg>
</div>
<!-- Shopping Cart -->
<div class="cart">
<h2>Simulated Shopping Cart</h2>
<div class="cart-items">
<p>No items in cart. Start scanning!</p>
</div>
</div>
<script>
const dragDiv = document.querySelector(".floatingDiv");
Dynamsoft.License.LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9");
const pInit = (async () => {
const cameraView = await Dynamsoft.DCE.CameraView.createInstance();
const cameraEnhancer = await Dynamsoft.DCE.CameraEnhancer.createInstance(cameraView);
const cvRouter = await Dynamsoft.CVR.CaptureVisionRouter.createInstance();
cvRouter.setInput(cameraEnhancer);
const resultReceiver = new Dynamsoft.CVR.CapturedResultReceiver();
resultReceiver.onDecodedBarcodesReceived = async (result) => {
simulateAddingToCart(result.barcodeResultItems[0].text);
}
await cvRouter.addResultReceiver(resultReceiver);
const filter = new Dynamsoft.Utility.MultiFrameResultCrossFilter();
filter.enableResultCrossVerification("barcode", true);
filter.enableResultDeduplication("barcode", true);
await cvRouter.addResultFilter(filter);
const uiElement = cameraView.getUIElement();
uiElement.querySelector(".dce-sel-camera").style.display = "none";
uiElement.querySelector(".dce-sel-resolution").style.display = "none";
cameraEnhancer.setVideoFit("cover");
dragDiv.append(uiElement);
return {
cameraView,
cameraEnhancer,
cvRouter
}
})();
// Function to initialize and launch the barcode scanner
async function startScanner() {
const { cameraView, cameraEnhancer, cvRouter } = await pInit;
await cameraEnhancer.open();
await cvRouter.startCapturing("ReadBarcodes_SpeedFirst");
dragDiv.hidden = false;
}
async function stopScanner() {
const { cameraEnhancer, cvRouter } = await pInit;
await cvRouter.stopCapturing();
await cameraEnhancer.close();
dragDiv.hidden = true;
}
// Product List
const products = [
{ name: "Wireless Mouse", price: 19.99 },
{ name: "Bluetooth Speaker", price: 29.99 },
{ name: "USB-C Cable", price: 9.99 },
{ name: "Notebook", price: 4.99 },
{ name: "Water Bottle", price: 14.99 },
{ name: "Laptop Stand", price: 34.99 },
{ name: "Wireless Keyboard", price: 24.99 },
{ name: "Portable Charger", price: 39.99 },
{ name: "LED Desk Lamp", price: 22.5 },
{ name: "Backpack", price: 49.99 },
{ name: "Earbuds", price: 17.99 },
{ name: "Smartphone Holder", price: 12.99 },
{ name: "Desk Organizer", price: 15.49 },
{ name: "Fitness Tracker", price: 59.99 },
{ name: "Coffee Mug", price: 8.99 },
{ name: "HDMI Cable", price: 6.99 },
{ name: "Flash Drive 64GB", price: 14.49 },
{ name: "Gaming Headset", price: 45.0 },
{ name: "Desk Chair Cushion", price: 27.99 },
{ name: "Sticky Notes Pack", price: 3.99 },
];
let cart = [];
// Simulate adding product to cart
function simulateAddingToCart(barcodeText) {
const product = {
...products[Math.floor(Math.random() * products.length)],
};
product.name += ` (${shortenString(barcodeText)})`;
cart.push(product);
updateCartDisplay();
}
// Shorten barcode text
const shortenString = (str, max = 15) =>
str.length <= max ? str : str.slice(0, max) + "...";
// Update Cart UI
function updateCartDisplay() {
const cartDiv = document.querySelector(".cart-items");
cartDiv.innerHTML = "";
const header = document.createElement("div");
header.className = "cart-item";
header.innerHTML = `<strong>Item</strong><strong>Price</strong>`;
cartDiv.appendChild(header);
cart.forEach((item) => {
const div = document.createElement("div");
div.className = "cart-item";
div.innerHTML = `<span>${item.name}</span><span>$${item.price.toFixed(
2
)}</span>`;
cartDiv.appendChild(div);
});
}
// Initialize Scan Button
window.onload = () => {
document.querySelector(".scan-btn").addEventListener("click", startScanner);
document.querySelector(".btn-close").addEventListener("click", stopScanner);
// Make the Floating Div Draggable
let offsetX = 0, offsetY = 0, isDragging = false;
dragDiv.addEventListener("mousedown", (e) => {
isDragging = true;
offsetX = e.clientX - dragDiv.offsetLeft;
offsetY = e.clientY - dragDiv.offsetTop;
});
document.addEventListener("mousemove", (e) => {
if (isDragging) {
dragDiv.style.left = `${e.clientX - offsetX}px`;
dragDiv.style.top = `${e.clientY - offsetY}px`;
}
});
document.addEventListener("mouseup", () => (isDragging = false));
dragDiv.addEventListener("touchstart", (e) => {
isDragging = true;
offsetX = e.touches[0].clientX - dragDiv.offsetLeft;
offsetY = e.touches[0].clientY - dragDiv.offsetTop;
});
document.addEventListener("touchmove", (e) => {
if (isDragging) {
e.preventDefault();
dragDiv.style.left = `${e.touches[0].clientX - offsetX}px`;
dragDiv.style.top = `${e.touches[0].clientY - offsetY}px`;
}
}, { passive: false });
document.addEventListener("touchend", () => (isDragging = false));
};
window.addEventListener("orientationchange", () => {
dragDiv.style.left = "10%"
dragDiv.style.top = "40%"
})
</script>
</body>
</html>