Update radar/index.php
This commit is contained in:
@@ -5,21 +5,21 @@
|
|||||||
<title>ESP32 Radar</title>
|
<title>ESP32 Radar</title>
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
|
margin: 0;
|
||||||
background: black;
|
background: black;
|
||||||
color: #00ff00;
|
color: #00ff00;
|
||||||
font-family: monospace;
|
font-family: monospace;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
margin: 0;
|
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
canvas {
|
|
||||||
background: black;
|
|
||||||
display: block;
|
|
||||||
margin: auto;
|
|
||||||
}
|
|
||||||
h1 {
|
h1 {
|
||||||
margin: 10px 0;
|
margin: 10px 0;
|
||||||
}
|
}
|
||||||
|
canvas {
|
||||||
|
display: block;
|
||||||
|
margin: auto;
|
||||||
|
background: black;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@@ -35,21 +35,22 @@ const centerX = canvas.width / 2;
|
|||||||
const centerY = canvas.height;
|
const centerY = canvas.height;
|
||||||
const radius = canvas.height - 20;
|
const radius = canvas.height - 20;
|
||||||
|
|
||||||
let sweepAngle = 0;
|
|
||||||
let radarData = {};
|
let radarData = {};
|
||||||
|
let sweepAngle = null;
|
||||||
|
|
||||||
|
// raster / grid
|
||||||
function drawGrid() {
|
function drawGrid() {
|
||||||
ctx.strokeStyle = "#003300";
|
ctx.strokeStyle = "#003300";
|
||||||
ctx.lineWidth = 1;
|
ctx.lineWidth = 1;
|
||||||
|
|
||||||
// cirkels
|
// bogen
|
||||||
for (let r = radius / 4; r <= radius; r += radius / 4) {
|
for (let r = radius / 4; r <= radius; r += radius / 4) {
|
||||||
ctx.beginPath();
|
ctx.beginPath();
|
||||||
ctx.arc(centerX, centerY, r, Math.PI, 0);
|
ctx.arc(centerX, centerY, r, Math.PI, 0);
|
||||||
ctx.stroke();
|
ctx.stroke();
|
||||||
}
|
}
|
||||||
|
|
||||||
// lijnen
|
// hoeklijnen
|
||||||
for (let a = 0; a <= 180; a += 30) {
|
for (let a = 0; a <= 180; a += 30) {
|
||||||
const rad = a * Math.PI / 180;
|
const rad = a * Math.PI / 180;
|
||||||
ctx.beginPath();
|
ctx.beginPath();
|
||||||
@@ -62,23 +63,7 @@ function drawGrid() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function drawSweep() {
|
// doelen tekenen
|
||||||
const rad = sweepAngle * Math.PI / 180;
|
|
||||||
|
|
||||||
ctx.strokeStyle = "#00ff00";
|
|
||||||
ctx.lineWidth = 2;
|
|
||||||
ctx.beginPath();
|
|
||||||
ctx.moveTo(centerX, centerY);
|
|
||||||
ctx.lineTo(
|
|
||||||
centerX + radius * Math.cos(rad - Math.PI),
|
|
||||||
centerY + radius * Math.sin(rad - Math.PI)
|
|
||||||
);
|
|
||||||
ctx.stroke();
|
|
||||||
|
|
||||||
sweepAngle++;
|
|
||||||
if (sweepAngle > 180) sweepAngle = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
function drawTargets() {
|
function drawTargets() {
|
||||||
ctx.fillStyle = "#00ff00";
|
ctx.fillStyle = "#00ff00";
|
||||||
|
|
||||||
@@ -98,15 +83,47 @@ function drawTargets() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sweeplijn = echte servohoek
|
||||||
|
function drawSweep() {
|
||||||
|
if (sweepAngle === null) return;
|
||||||
|
|
||||||
|
const rad = sweepAngle * Math.PI / 180;
|
||||||
|
|
||||||
|
ctx.strokeStyle = "#00ff00";
|
||||||
|
ctx.lineWidth = 2;
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.moveTo(centerX, centerY);
|
||||||
|
ctx.lineTo(
|
||||||
|
centerX + radius * Math.cos(rad - Math.PI),
|
||||||
|
centerY + radius * Math.sin(rad - Math.PI)
|
||||||
|
);
|
||||||
|
ctx.stroke();
|
||||||
|
}
|
||||||
|
|
||||||
|
// radar.json ophalen + laatste hoek bepalen
|
||||||
async function fetchData() {
|
async function fetchData() {
|
||||||
try {
|
try {
|
||||||
const res = await fetch("radar.json?_=" + Date.now());
|
const res = await fetch("radar.json?_=" + Date.now());
|
||||||
radarData = await res.json();
|
radarData = await res.json();
|
||||||
} catch (e) {
|
|
||||||
console.log("Geen data");
|
let latestTime = 0;
|
||||||
|
let latestAngle = null;
|
||||||
|
|
||||||
|
for (const hoek in radarData) {
|
||||||
|
const t = radarData[hoek].tijd;
|
||||||
|
if (t > latestTime) {
|
||||||
|
latestTime = t;
|
||||||
|
latestAngle = parseInt(hoek);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sweepAngle = latestAngle;
|
||||||
|
} catch (e) {
|
||||||
|
console.log("Geen radar data");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// render-loop
|
||||||
function loop() {
|
function loop() {
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
drawGrid();
|
drawGrid();
|
||||||
@@ -115,7 +132,7 @@ function loop() {
|
|||||||
requestAnimationFrame(loop);
|
requestAnimationFrame(loop);
|
||||||
}
|
}
|
||||||
|
|
||||||
// elke 200ms nieuwe radar data ophalen
|
// elke 200 ms nieuwe data
|
||||||
setInterval(fetchData, 200);
|
setInterval(fetchData, 200);
|
||||||
|
|
||||||
fetchData();
|
fetchData();
|
||||||
|
|||||||
Reference in New Issue
Block a user