2024-01-05 16:17:00 +02:00
|
|
|
import { Component, createEffect, createSignal, onMount } from "solid-js";
|
2024-01-06 23:48:23 +02:00
|
|
|
import moment from "moment";
|
2024-01-05 16:17:00 +02:00
|
|
|
|
|
|
|
|
import { cityCoords } from "./cityCoords";
|
|
|
|
|
import { SelectCity } from "../components/SelectCity";
|
|
|
|
|
|
2024-01-06 23:48:23 +02:00
|
|
|
import mapUrl from "../assets/map_1000x570.webp";
|
|
|
|
|
import "../css/station.css"
|
|
|
|
|
import { SelectField } from "../components/SelectField";
|
|
|
|
|
import { coordToCity } from "./coordToCity";
|
|
|
|
|
import { Result } from "./Result";
|
|
|
|
|
|
2024-01-05 16:17:00 +02:00
|
|
|
export const Station: Component<{}> = () => {
|
|
|
|
|
const [getCanvas, setCanvas] = createSignal<HTMLCanvasElement>();
|
|
|
|
|
const [getImg, setImg] = createSignal(new Image());
|
|
|
|
|
const [getShowCities, setShowCities] = createSignal(false);
|
2024-01-06 23:48:23 +02:00
|
|
|
const [getCities, setCities] = createSignal<Set<string>>(new Set(["Ainaži", "Rīga", "Rēzekne", "Liepāja", "Daugavpils", "Ventspils", "Madona"]));
|
|
|
|
|
const [getDate, setDate] = createSignal(moment());
|
|
|
|
|
const [getField, setField] = createSignal("tempMax");
|
|
|
|
|
const [getCity, setCity] = createSignal<string | undefined>(undefined);
|
2024-01-05 16:17:00 +02:00
|
|
|
|
|
|
|
|
let ctx: CanvasRenderingContext2D | undefined;
|
|
|
|
|
|
|
|
|
|
onMount(() => {
|
2024-01-06 23:48:23 +02:00
|
|
|
const canvas = getCanvas()!;
|
|
|
|
|
canvas.addEventListener("click", event => {
|
|
|
|
|
const rect = canvas.getBoundingClientRect();
|
|
|
|
|
const scaleX = canvas.width / rect.width;
|
|
|
|
|
const scaleY = canvas.height / rect.height;
|
|
|
|
|
|
|
|
|
|
const x = Math.round((event.clientX - rect.left) * scaleX);
|
|
|
|
|
const y = Math.round((event.clientY - rect.top) * scaleY);
|
|
|
|
|
|
|
|
|
|
const selectedCityCoords = Object.entries(cityCoords).filter(([city]) => getCities().has(city));
|
|
|
|
|
const optionalCity = coordToCity(x, y, selectedCityCoords);
|
|
|
|
|
setCity(optionalCity);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ctx = canvas.getContext("2d")!;
|
2024-01-05 16:17:00 +02:00
|
|
|
|
|
|
|
|
const img = new Image();
|
|
|
|
|
img.onload = () => setImg(img);
|
|
|
|
|
img.src = mapUrl;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
createEffect(() => {
|
|
|
|
|
if (!ctx || !getImg()) return;
|
|
|
|
|
drawOnMap(
|
|
|
|
|
ctx!,
|
|
|
|
|
[getImg()],
|
|
|
|
|
getCities(),
|
|
|
|
|
);
|
|
|
|
|
});
|
2023-12-25 23:20:34 +02:00
|
|
|
|
|
|
|
|
return (
|
2024-01-05 16:17:00 +02:00
|
|
|
<div class="stationWrapper">
|
2023-12-25 23:20:34 +02:00
|
|
|
<div class="container">
|
|
|
|
|
<div class="column">
|
2024-01-05 16:17:00 +02:00
|
|
|
<div class="submenu">
|
|
|
|
|
<input
|
|
|
|
|
type="button"
|
|
|
|
|
value="Select stations"
|
|
|
|
|
onClick={() => setShowCities(!getShowCities())}
|
|
|
|
|
/>
|
2024-01-06 23:48:23 +02:00
|
|
|
<input type="date" value={getDate().format("YYYY-MM-DD")} />
|
|
|
|
|
<SelectField getField={getField} setField={setField} />
|
2024-01-05 16:17:00 +02:00
|
|
|
</div>
|
|
|
|
|
<div class={"cities " + (getShowCities() ? "visible" : "hidden")}>
|
|
|
|
|
<SelectCity getCities={getCities} setCities={setCities} />
|
|
|
|
|
</div>
|
|
|
|
|
<canvas ref={setCanvas} width={1000} height={570} />
|
2023-12-25 23:20:34 +02:00
|
|
|
</div>
|
|
|
|
|
<div class="column">
|
2024-01-06 23:48:23 +02:00
|
|
|
<Result getCity={getCity} />
|
2023-12-25 23:20:34 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2024-01-05 16:17:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function drawOnMap(
|
|
|
|
|
ctx: CanvasRenderingContext2D,
|
|
|
|
|
imgArr: [HTMLImageElement],
|
|
|
|
|
cities: Set<string>,
|
|
|
|
|
): void {
|
|
|
|
|
ctx.drawImage(imgArr[0], 0, 0);
|
|
|
|
|
ctx.fillStyle = "red";
|
|
|
|
|
ctx.font = "18px serif";
|
|
|
|
|
cities.forEach(city => {
|
|
|
|
|
const coord = cityCoords[city];
|
|
|
|
|
if (!coord) return;
|
|
|
|
|
|
|
|
|
|
ctx.beginPath();
|
2024-01-06 23:48:23 +02:00
|
|
|
ctx.arc(coord.x, coord.y, 4, 0, 2 * Math.PI);
|
2024-01-05 16:17:00 +02:00
|
|
|
const cityTextSize = ctx.measureText(city);
|
|
|
|
|
ctx.fillText(city, coord.x - cityTextSize.width/2, coord.y - 6);
|
|
|
|
|
ctx.fill();
|
2024-01-06 23:48:23 +02:00
|
|
|
// const boxWidth = 60;
|
|
|
|
|
// const boxHeight = 30;
|
|
|
|
|
// ctx.strokeRect(coord.x-boxWidth/2, coord.y-boxHeight/2, boxWidth, boxHeight);
|
2024-01-05 16:17:00 +02:00
|
|
|
});
|
2023-12-25 23:20:34 +02:00
|
|
|
}
|