2023-08-07 23:25:35 +03:00
|
|
|
import { Component, createEffect, createSignal, onMount } from "solid-js";
|
2023-08-08 17:22:52 +03:00
|
|
|
|
2023-08-07 23:25:35 +03:00
|
|
|
import { ResultKeyVal } from "../../consts";
|
2023-08-08 17:22:52 +03:00
|
|
|
import { drawRotatedImage, getAngleFromString } from "./windAngles";
|
|
|
|
|
import { WindInputs, WindSignals } from "./WindInputs";
|
|
|
|
|
|
2024-01-23 22:58:26 +02:00
|
|
|
import map_1920x1080 from "../../assets/map_1920x1080.webp";
|
2024-02-06 16:56:31 +02:00
|
|
|
import map_1920x1080_wind from "../../assets/map_1920x1080_wind.webp";
|
2024-01-23 22:58:26 +02:00
|
|
|
import map_3840x1440 from "../../assets/map_3840x1440.webp";
|
2024-02-06 16:56:31 +02:00
|
|
|
import map_3840x1440_wind from "../../assets/map_3840x1440_wind.webp";
|
2023-08-08 19:00:22 +03:00
|
|
|
import arrowUrl from "../../assets/arrow.webp";
|
2024-01-22 23:54:33 +02:00
|
|
|
import { cityCoords } from "../../components/cityCoords";
|
2023-08-07 23:25:35 +03:00
|
|
|
|
2024-02-06 16:56:31 +02:00
|
|
|
type MapResolution = "map_1920x1080" | "map_1920x1080_wind" | "map_3840x1440" | "map_3840x1440_wind";
|
2024-01-23 22:58:26 +02:00
|
|
|
|
|
|
|
|
type ResolutionPropsValue = {
|
|
|
|
|
offsetX: number;
|
|
|
|
|
offsetY: number;
|
|
|
|
|
width: number;
|
|
|
|
|
height: number;
|
|
|
|
|
scale: number;
|
|
|
|
|
showWind: boolean;
|
|
|
|
|
map: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const resolutionProps: Record<string, ResolutionPropsValue> = {
|
2024-02-06 16:56:31 +02:00
|
|
|
"map_1920x1080": { offsetX: 90, offsetY: 120, width: 1920, height: 1080, scale: 1.35, showWind: false, map: map_1920x1080 },
|
|
|
|
|
"map_1920x1080_wind": { offsetX: 90, offsetY: 120, width: 1920, height: 1080, scale: 1.35, showWind: true, map: map_1920x1080_wind },
|
|
|
|
|
"map_3840x1440": { offsetX: 800, offsetY: 120, width: 3840, height: 1440, scale: 2.2, showWind: false, map: map_3840x1440 },
|
|
|
|
|
"map_3840x1440_wind": { offsetX: 800, offsetY: 120, width: 3840, height: 1440, scale: 2.2, showWind: true, map: map_3840x1440_wind },
|
2024-01-23 22:58:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const MapView: Component<{ type: MapResolution, data: () => ResultKeyVal[] }> = ({ type, data }) => {
|
2023-08-07 23:25:35 +03:00
|
|
|
const [getCanvas, setCanvas] = createSignal<HTMLCanvasElement>();
|
|
|
|
|
const [getImg, setImg] = createSignal(new Image());
|
|
|
|
|
let lastCoords: [string, {x: number, y: number }, number][] = [];
|
2024-01-23 22:58:26 +02:00
|
|
|
const props = resolutionProps[type];
|
2023-08-07 23:25:35 +03:00
|
|
|
|
2023-08-08 19:00:22 +03:00
|
|
|
const arrowImg = new Image();
|
|
|
|
|
arrowImg.src = arrowUrl;
|
|
|
|
|
|
2023-08-08 17:22:52 +03:00
|
|
|
const windSignals: WindSignals = {
|
|
|
|
|
direction: createSignal(""),
|
|
|
|
|
speed: createSignal(""),
|
|
|
|
|
gusts: createSignal(""),
|
2023-08-08 23:05:55 +03:00
|
|
|
roundValues: createSignal(false),
|
2023-08-08 17:22:52 +03:00
|
|
|
}
|
|
|
|
|
|
2023-08-08 23:05:55 +03:00
|
|
|
function getWindData(): [string, string, string, boolean] {
|
2023-08-08 17:22:52 +03:00
|
|
|
return [
|
|
|
|
|
windSignals.direction[0](),
|
|
|
|
|
windSignals.speed[0](),
|
|
|
|
|
windSignals.gusts[0](),
|
2023-08-08 23:05:55 +03:00
|
|
|
windSignals.roundValues[0](),
|
2023-08-08 17:22:52 +03:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 23:25:35 +03:00
|
|
|
onMount(() => {
|
|
|
|
|
const ctx = getCanvas()!.getContext("2d")!;
|
|
|
|
|
const img = new Image();
|
|
|
|
|
img.onload = () => {
|
|
|
|
|
setImg(img);
|
2023-08-08 23:05:55 +03:00
|
|
|
drawOnMap(
|
|
|
|
|
ctx,
|
|
|
|
|
[img, arrowImg],
|
|
|
|
|
lastCoords,
|
|
|
|
|
getWindData(),
|
2024-01-23 22:58:26 +02:00
|
|
|
props,
|
2023-08-08 23:05:55 +03:00
|
|
|
);
|
2023-08-07 23:25:35 +03:00
|
|
|
};
|
2024-01-23 22:58:26 +02:00
|
|
|
img.src = props.map;
|
2023-08-07 23:25:35 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
createEffect(() => {
|
|
|
|
|
const ctx = getCanvas()!.getContext("2d")!;
|
|
|
|
|
const coordsAndData: [string, {x: number, y: number }, number][] = data()
|
|
|
|
|
.map(([city, value]) => [
|
|
|
|
|
city,
|
|
|
|
|
cityCoords[city as keyof typeof cityCoords],
|
|
|
|
|
typeof value === "number" ? value : -99,
|
|
|
|
|
]);
|
|
|
|
|
lastCoords = coordsAndData;
|
2023-08-08 23:05:55 +03:00
|
|
|
drawOnMap(
|
|
|
|
|
ctx,
|
|
|
|
|
[getImg(),arrowImg],
|
|
|
|
|
coordsAndData,
|
|
|
|
|
getWindData(),
|
2024-01-23 22:58:26 +02:00
|
|
|
props,
|
2023-08-08 23:05:55 +03:00
|
|
|
);
|
2023-08-07 23:25:35 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
2023-08-08 17:22:52 +03:00
|
|
|
<>
|
|
|
|
|
<WindInputs signals={windSignals} />
|
2024-02-06 16:56:31 +02:00
|
|
|
<canvas
|
|
|
|
|
ref={setCanvas}
|
|
|
|
|
width={props.width+"px"}
|
|
|
|
|
height={props.height+"px"}
|
|
|
|
|
style={{"max-width": "1000px"}}
|
|
|
|
|
/>
|
2023-08-08 17:22:52 +03:00
|
|
|
</>
|
2023-08-07 23:25:35 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function drawOnMap(
|
|
|
|
|
ctx: CanvasRenderingContext2D,
|
2023-08-08 19:00:22 +03:00
|
|
|
imgArr: [HTMLImageElement, HTMLImageElement],
|
2023-08-08 17:22:52 +03:00
|
|
|
coords: [string, {x: number, y: number }, number][],
|
2023-08-08 23:05:55 +03:00
|
|
|
windData: [string, string, string, boolean],
|
2024-01-23 22:58:26 +02:00
|
|
|
props: ResolutionPropsValue,
|
2023-08-07 23:25:35 +03:00
|
|
|
): void {
|
2024-01-22 23:54:33 +02:00
|
|
|
const boxSize = 80;
|
2023-08-08 17:22:52 +03:00
|
|
|
|
2023-08-08 23:05:55 +03:00
|
|
|
const [windDirection, windSpeed, windGusts, roundValues] = windData;
|
2023-08-08 19:00:22 +03:00
|
|
|
const [bgImg, arrowImg] = imgArr;
|
|
|
|
|
|
2023-08-08 17:22:52 +03:00
|
|
|
// bg
|
2023-08-08 19:00:22 +03:00
|
|
|
ctx.drawImage(bgImg, 0, 0);
|
2023-08-08 17:22:52 +03:00
|
|
|
|
|
|
|
|
// city boxes
|
|
|
|
|
ctx.fillStyle = "#FFFFFF";
|
|
|
|
|
coords.forEach(([, {x, y}, value]) => {
|
2024-01-23 22:58:26 +02:00
|
|
|
const localX = props.offsetX + x * props.scale;
|
|
|
|
|
const localY = props.offsetY + y * props.scale;
|
2024-01-22 23:54:33 +02:00
|
|
|
|
|
|
|
|
ctx.beginPath();
|
|
|
|
|
ctx.fillStyle = "#FFFFFF";
|
|
|
|
|
ctx.rect(localX-boxSize/2, localY-boxSize/2, boxSize, boxSize);
|
|
|
|
|
ctx.fill();
|
2023-08-08 17:22:52 +03:00
|
|
|
});
|
|
|
|
|
ctx.fill();
|
|
|
|
|
|
|
|
|
|
// weather values
|
2023-08-08 19:50:19 +03:00
|
|
|
ctx.font = "bold 30px Rubik";
|
2023-08-07 23:25:35 +03:00
|
|
|
ctx.textAlign = "center";
|
|
|
|
|
ctx.textBaseline = "middle";
|
|
|
|
|
coords.forEach(([, {x, y}, value]) => {
|
2024-01-23 22:58:26 +02:00
|
|
|
const localX = props.offsetX + x * props.scale;
|
|
|
|
|
const localY = props.offsetY + y * props.scale;
|
2024-01-22 23:54:33 +02:00
|
|
|
|
|
|
|
|
ctx.fillStyle = "#000000";
|
2023-08-08 23:05:55 +03:00
|
|
|
const numericValue = roundValues ? Math.round(value) : value;
|
|
|
|
|
const stringValue = numericValue.toString().replace(".", ",");
|
2024-01-22 23:54:33 +02:00
|
|
|
ctx.fillText(stringValue, localX, localY);
|
2023-08-07 23:25:35 +03:00
|
|
|
});
|
2023-08-08 17:22:52 +03:00
|
|
|
|
2024-01-23 22:58:26 +02:00
|
|
|
if (!props.showWind) return;
|
|
|
|
|
|
2023-08-08 17:22:52 +03:00
|
|
|
// wind values
|
2023-08-08 19:50:19 +03:00
|
|
|
ctx.font = "bold 45px Rubik";
|
2023-08-08 17:22:52 +03:00
|
|
|
ctx.textAlign = "left";
|
|
|
|
|
ctx.textBaseline = "top";
|
|
|
|
|
ctx.fillStyle = "#FFFFFF";
|
|
|
|
|
const boxMiddleX = 1675;
|
|
|
|
|
|
2023-08-08 23:05:55 +03:00
|
|
|
const directionWidth = ctx.measureText(windDirection).width;
|
|
|
|
|
ctx.fillText(windDirection, boxMiddleX - directionWidth / 2 + 30, 370);
|
|
|
|
|
const angleInDegrees = getAngleFromString(windDirection);
|
2023-08-08 17:22:52 +03:00
|
|
|
drawRotatedImage(ctx, arrowImg, boxMiddleX - directionWidth / 2 - 30, 370, angleInDegrees);
|
|
|
|
|
|
2023-08-08 23:05:55 +03:00
|
|
|
const speedWidth = ctx.measureText(windSpeed).width;
|
|
|
|
|
ctx.fillText(windSpeed, boxMiddleX - speedWidth / 2 - 30, 575);
|
2023-08-08 19:50:19 +03:00
|
|
|
ctx.font = "bold 25px Rubik";
|
2023-08-08 17:22:52 +03:00
|
|
|
ctx.fillText("M/S", boxMiddleX + speedWidth / 2 - 22, 590);
|
|
|
|
|
|
2023-08-08 19:50:19 +03:00
|
|
|
ctx.font = "bold 45px Rubik";
|
2023-08-08 23:05:55 +03:00
|
|
|
const gustsWidth = ctx.measureText(windGusts).width;
|
|
|
|
|
ctx.fillText(windGusts, boxMiddleX - gustsWidth / 2 - 30, 790);
|
2023-08-08 19:50:19 +03:00
|
|
|
ctx.font = "bold 25px Rubik";
|
2023-08-08 17:22:52 +03:00
|
|
|
ctx.fillText("M/S", boxMiddleX + gustsWidth / 2 - 22, 805);
|
2023-08-07 23:25:35 +03:00
|
|
|
}
|