ws4kp-linhanced/server/scripts/modules/radar-worker.mjs

59 lines
2.4 KiB
JavaScript
Raw Normal View History

import {
2025-06-16 15:30:56 -05:00
removeDopplerRadarImageNoise,
} from './radar-utils.mjs';
2025-06-16 15:30:56 -05:00
import { RADAR_FULL_SIZE, RADAR_FINAL_SIZE } from './radar-constants.mjs';
2025-05-25 15:17:56 -05:00
onmessage = async (e) => {
2025-05-24 16:36:41 -05:00
const {
url, RADAR_HOST, OVERRIDES, radarSourceXY,
2025-05-24 16:36:41 -05:00
} = e.data;
2025-05-25 15:17:56 -05:00
// get the image
const modifiedRadarUrl = OVERRIDES.RADAR_HOST ? url.replace(RADAR_HOST, OVERRIDES.RADAR_HOST) : url;
const radarResponsePromise = fetch(modifiedRadarUrl);
2025-05-24 16:36:41 -05:00
// calculate offsets and sizes
2025-05-25 15:17:56 -05:00
const radarSource = {
width: 240,
height: 163,
x: Math.round(radarSourceXY.x / 2),
y: Math.round(radarSourceXY.y / 2),
};
2025-05-24 16:36:41 -05:00
2025-06-13 17:58:05 -05:00
// create radar context for manipulation
2025-06-16 15:30:56 -05:00
const radarCanvas = new OffscreenCanvas(RADAR_FULL_SIZE.width, RADAR_FULL_SIZE.height);
2025-05-25 15:17:56 -05:00
const radarContext = radarCanvas.getContext('2d');
2025-05-24 16:36:41 -05:00
radarContext.imageSmoothingEnabled = false;
// test response
2025-05-25 15:17:56 -05:00
const radarResponse = await radarResponsePromise;
if (!radarResponse.ok) throw new Error(`Unable to fetch radar error ${radarResponse.status} ${radarResponse.statusText} from ${radarResponse.url}`);
2025-05-24 16:36:41 -05:00
// get the blob
2025-05-25 15:17:56 -05:00
const radarImgBlob = await radarResponse.blob();
2025-05-24 16:36:41 -05:00
// assign to an html image element
const radarImgElement = await createImageBitmap(radarImgBlob);
// draw the entire image
2025-06-16 15:30:56 -05:00
radarContext.clearRect(0, 0, RADAR_FULL_SIZE.width, RADAR_FULL_SIZE.height);
radarContext.drawImage(radarImgElement, 0, 0, RADAR_FULL_SIZE.width, RADAR_FULL_SIZE.height);
2025-05-25 15:17:56 -05:00
// crop the radar image without scaling
const croppedRadarCanvas = new OffscreenCanvas(radarSource.width, radarSource.height);
const croppedRadarContext = croppedRadarCanvas.getContext('2d');
croppedRadarContext.imageSmoothingEnabled = false;
croppedRadarContext.drawImage(radarCanvas, radarSource.x, radarSource.y, croppedRadarCanvas.width, croppedRadarCanvas.height, 0, 0, croppedRadarCanvas.width, croppedRadarCanvas.height);
2025-05-24 16:36:41 -05:00
// clean the image
removeDopplerRadarImageNoise(croppedRadarContext);
2025-05-25 15:17:56 -05:00
// stretch the radar image
2025-06-16 15:30:56 -05:00
const stretchCanvas = new OffscreenCanvas(RADAR_FINAL_SIZE.width, RADAR_FINAL_SIZE.height);
2025-05-25 15:17:56 -05:00
const stretchContext = stretchCanvas.getContext('2d', { willReadFrequently: true });
stretchContext.imageSmoothingEnabled = false;
2025-06-16 15:30:56 -05:00
stretchContext.drawImage(croppedRadarCanvas, 0, 0, radarSource.width, radarSource.height, 0, 0, RADAR_FINAL_SIZE.width, RADAR_FINAL_SIZE.height);
2025-05-25 15:17:56 -05:00
2025-06-13 17:58:05 -05:00
const stretchedRadar = stretchCanvas.transferToImageBitmap();
2025-05-24 16:36:41 -05:00
2025-06-13 17:58:05 -05:00
postMessage(stretchedRadar, [stretchedRadar]);
2025-05-24 16:36:41 -05:00
};