TameemADR commited on
Commit
3eb0f9c
·
verified ·
1 Parent(s): aae513e

Update static/js/script.js

Browse files
Files changed (1) hide show
  1. static/js/script.js +26 -16
static/js/script.js CHANGED
@@ -5,28 +5,38 @@ console.log("JS is loaded!");
5
  let hMin = 0, hMax = 179, sMin = 0, sMax = 255, vMin = 0, vMax = 255;
6
  let currentImage = null;
7
 
8
- document.getElementById('upload-form').addEventListener('submit', function(e) {
9
- e.preventDefault();
10
 
11
- const fileInput = document.getElementById('pdf-input');
 
12
  const formData = new FormData();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- formData.append('file', fileInput.files[0]);
 
 
15
 
16
- fetch(`${window.location.origin}/upload`, {
17
  method: 'POST',
18
- body: formData
19
- })
20
- .then(res => res.json())
21
- .then(data => {
22
- document.getElementById('upload-status').innerText = data.message;
23
- console.log('Upload success:', data);
24
- updateImage(data.image_path);
25
- })
26
- .catch(err => {
27
- document.getElementById('upload-status').innerText = 'Upload failed';
28
- console.error('Upload error:', err);
29
  });
 
 
 
30
  });
31
 
32
  function updateImage(src) {
 
5
  let hMin = 0, hMax = 179, sMin = 0, sMax = 255, vMin = 0, vMax = 255;
6
  let currentImage = null;
7
 
 
 
8
 
9
+ document.getElementById('upload-form').onsubmit = async function(e) {
10
+ e.preventDefault();
11
  const formData = new FormData();
12
+ formData.append("file", document.getElementById("pdf-input").files[0]);
13
+
14
+ const response = await fetch("/upload", { method: "POST", body: formData });
15
+ const result = await response.json();
16
+
17
+ const img = document.getElementById("image");
18
+ img.src = `/static/converted_images/${result.image_filename}?${new Date().getTime()}`; // cache-bust
19
+ img.onload = () => {
20
+ const canvas = document.getElementById("canvas");
21
+ const ctx = canvas.getContext("2d");
22
+ canvas.width = img.width;
23
+ canvas.height = img.height;
24
+ ctx.drawImage(img, 0, 0);
25
+ };
26
+ };
27
 
28
+ canvas.addEventListener('click', async (event) => {
29
+ const x = event.offsetX;
30
+ const y = event.offsetY;
31
 
32
+ const response = await fetch('/pixel', {
33
  method: 'POST',
34
+ headers: {'Content-Type': 'application/json'},
35
+ body: JSON.stringify({ x, y })
 
 
 
 
 
 
 
 
 
36
  });
37
+
38
+ const pixelData = await response.json();
39
+ document.getElementById("pixel-info").textContent = `Clicked: (${x}, ${y}) - RGB: ${pixelData.rgb} - HSV: ${pixelData.hsv}`;
40
  });
41
 
42
  function updateImage(src) {