Create script.js
Browse files- static/script.js +77 -0
static/script.js
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
document.getElementById('lbw-form').addEventListener('submit', function(e) {
|
2 |
+
e.preventDefault();
|
3 |
+
const formData = new FormData(this);
|
4 |
+
fetch('/analyze', {
|
5 |
+
method: 'POST',
|
6 |
+
body: formData
|
7 |
+
})
|
8 |
+
.then(response => response.json())
|
9 |
+
.then(data => {
|
10 |
+
document.getElementById('decision').textContent = `Decision: ${data.decision} (Confidence: ${data.confidence})`;
|
11 |
+
drawPitch(data.actual_path, data.projected_path, data.pitching, data.impact);
|
12 |
+
});
|
13 |
+
});
|
14 |
+
|
15 |
+
function drawPitch(actualPath, projectedPath, pitching, impact) {
|
16 |
+
const canvas = document.getElementById('pitchCanvas');
|
17 |
+
const ctx = canvas.getContext('2d');
|
18 |
+
|
19 |
+
// Clear canvas
|
20 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
21 |
+
|
22 |
+
// Draw pitch (22 yards scaled to 600px height, 10 feet scaled to 400px width)
|
23 |
+
ctx.fillStyle = '#90EE90'; // Light green for pitch
|
24 |
+
ctx.fillRect(50, 50, 300, 500);
|
25 |
+
|
26 |
+
// Draw stumps (at y=1, scaled to canvas)
|
27 |
+
ctx.fillStyle = '#FFF';
|
28 |
+
ctx.fillRect(190, 50, 20, 10); // Top stumps
|
29 |
+
|
30 |
+
// Draw creases
|
31 |
+
ctx.strokeStyle = '#000';
|
32 |
+
ctx.beginPath();
|
33 |
+
ctx.moveTo(50, 150); ctx.lineTo(350, 150); // Popping crease
|
34 |
+
ctx.moveTo(200, 50); ctx.lineTo(200, 150); // Middle stump line
|
35 |
+
ctx.stroke();
|
36 |
+
|
37 |
+
// Scale coordinates (x: 0-1 to 50-350, y: 0-1 to 550-50)
|
38 |
+
function scalePoint(point) {
|
39 |
+
return {
|
40 |
+
x: 50 + point.x * 300,
|
41 |
+
y: 550 - point.y * 500
|
42 |
+
};
|
43 |
+
}
|
44 |
+
|
45 |
+
// Draw actual path (red arc)
|
46 |
+
ctx.strokeStyle = 'red';
|
47 |
+
ctx.lineWidth = 2;
|
48 |
+
ctx.beginPath();
|
49 |
+
const actualScaled = actualPath.map(scalePoint);
|
50 |
+
ctx.moveTo(actualScaled[0].x, actualScaled[0].y);
|
51 |
+
for (let i = 1; i < actualScaled.length; i++) {
|
52 |
+
ctx.lineTo(actualScaled[i].x, actualScaled[i].y);
|
53 |
+
}
|
54 |
+
ctx.stroke();
|
55 |
+
|
56 |
+
// Draw projected path (blue line)
|
57 |
+
ctx.strokeStyle = 'blue';
|
58 |
+
ctx.beginPath();
|
59 |
+
const projectedScaled = projectedPath.map(scalePoint);
|
60 |
+
ctx.moveTo(projectedScaled[0].x, projectedScaled[0].y);
|
61 |
+
ctx.lineTo(projectedScaled[1].x, projectedScaled[1].y);
|
62 |
+
ctx.stroke();
|
63 |
+
|
64 |
+
// Draw pitching point
|
65 |
+
const pitchPt = scalePoint(pitching);
|
66 |
+
ctx.fillStyle = 'black';
|
67 |
+
ctx.beginPath();
|
68 |
+
ctx.arc(pitchPt.x, pitchPt.y, 5, 0, 2 * Math.PI);
|
69 |
+
ctx.fill();
|
70 |
+
|
71 |
+
// Draw impact point
|
72 |
+
const impactPt = scalePoint(impact);
|
73 |
+
ctx.fillStyle = 'orange';
|
74 |
+
ctx.beginPath();
|
75 |
+
ctx.arc(impactPt.x, impactPt.y, 5, 0, 2 * Math.PI);
|
76 |
+
ctx.fill();
|
77 |
+
}
|