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";
|
|
|
|
|
import { cityCoords } from "./cityCoords";
|
2023-08-08 17:22:52 +03:00
|
|
|
import { drawRotatedImage, getAngleFromString } from "./windAngles";
|
|
|
|
|
import { WindInputs, WindSignals } from "./WindInputs";
|
|
|
|
|
|
2023-08-08 19:00:22 +03:00
|
|
|
import mapUrl from "../../assets/map_1920x1080.webp";
|
|
|
|
|
import arrowUrl from "../../assets/arrow.webp";
|
2023-08-07 23:25:35 +03:00
|
|
|
|
|
|
|
|
export const MapView: Component<{ data: () => ResultKeyVal[] }> = ({ data }) => {
|
|
|
|
|
const [getCanvas, setCanvas] = createSignal<HTMLCanvasElement>();
|
|
|
|
|
const [getImg, setImg] = createSignal(new Image());
|
|
|
|
|
let lastCoords: [string, {x: number, y: number }, number][] = [];
|
|
|
|
|
|
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(""),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getWindData(): [string, string, string] {
|
|
|
|
|
return [
|
|
|
|
|
windSignals.direction[0](),
|
|
|
|
|
windSignals.speed[0](),
|
|
|
|
|
windSignals.gusts[0](),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
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 19:00:22 +03:00
|
|
|
drawOnMap(ctx, [img, arrowImg], lastCoords, getWindData());
|
2023-08-07 23:25:35 +03:00
|
|
|
};
|
2023-08-08 19:00:22 +03:00
|
|
|
img.src = mapUrl;
|
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 19:00:22 +03:00
|
|
|
drawOnMap(ctx, [getImg(), arrowImg], coordsAndData, getWindData());
|
2023-08-07 23:25:35 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
2023-08-08 17:22:52 +03:00
|
|
|
<>
|
|
|
|
|
<WindInputs signals={windSignals} />
|
|
|
|
|
<canvas ref={setCanvas} width="1920px" height="1080px" />
|
|
|
|
|
</>
|
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][],
|
|
|
|
|
windData: [string, string, string],
|
2023-08-07 23:25:35 +03:00
|
|
|
): void {
|
|
|
|
|
const size = 80;
|
2023-08-08 17:22:52 +03:00
|
|
|
|
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]) => {
|
|
|
|
|
ctx.rect(x, y, size, size);
|
|
|
|
|
});
|
|
|
|
|
ctx.fill();
|
|
|
|
|
|
|
|
|
|
// weather values
|
2023-08-07 23:25:35 +03:00
|
|
|
ctx.font = "bold 30px Arial";
|
|
|
|
|
ctx.textAlign = "center";
|
|
|
|
|
ctx.textBaseline = "middle";
|
2023-08-08 17:22:52 +03:00
|
|
|
ctx.fillStyle = "#000000";
|
2023-08-07 23:25:35 +03:00
|
|
|
coords.forEach(([, {x, y}, value]) => {
|
|
|
|
|
ctx.rect(x, y, size, size);
|
|
|
|
|
ctx.fillText(`${value}`, x + size/2, y + size/2);
|
|
|
|
|
});
|
2023-08-08 17:22:52 +03:00
|
|
|
|
|
|
|
|
// wind values
|
|
|
|
|
ctx.font = "bold 45px Arial";
|
|
|
|
|
ctx.textAlign = "left";
|
|
|
|
|
ctx.textBaseline = "top";
|
|
|
|
|
ctx.fillStyle = "#FFFFFF";
|
|
|
|
|
const boxMiddleX = 1675;
|
|
|
|
|
|
|
|
|
|
const directionWidth = ctx.measureText(windData[0]).width;
|
|
|
|
|
ctx.fillText(windData[0], boxMiddleX - directionWidth / 2 + 30, 370);
|
|
|
|
|
const angleInDegrees = getAngleFromString(windData[0]);
|
|
|
|
|
drawRotatedImage(ctx, arrowImg, boxMiddleX - directionWidth / 2 - 30, 370, angleInDegrees);
|
|
|
|
|
|
|
|
|
|
const speedWidth = ctx.measureText(windData[1]).width;
|
|
|
|
|
ctx.fillText(windData[1], boxMiddleX - speedWidth / 2 - 30, 575);
|
|
|
|
|
ctx.font = "bold 25px Arial";
|
|
|
|
|
ctx.fillText("M/S", boxMiddleX + speedWidth / 2 - 22, 590);
|
|
|
|
|
|
|
|
|
|
ctx.font = "bold 45px Arial";
|
|
|
|
|
const gustsWidth = ctx.measureText(windData[2]).width;
|
|
|
|
|
ctx.fillText(windData[2], boxMiddleX - gustsWidth / 2 - 30, 790);
|
|
|
|
|
ctx.font = "bold 25px Arial";
|
|
|
|
|
ctx.fillText("M/S", boxMiddleX + gustsWidth / 2 - 22, 805);
|
2023-08-07 23:25:35 +03:00
|
|
|
}
|