2024-02-08 15:25:51 +02:00
|
|
|
import { ResolutionPropsValue, WindProps, windProps } from "./mapConsts";
|
|
|
|
|
import { drawRotatedImage, getAngleFromString } from "./windAngles";
|
2026-08-19 17:44:46 +03:00
|
|
|
import { faktiskaMarkerLayout, FaktiskaStation } from "../../pages/map-graphics/faktiskaConfig";
|
2026-08-20 10:46:26 +03:00
|
|
|
import { getLoadedWeatherIcon } from "../weatherIcons/weatherIconAssets";
|
2024-02-08 15:25:51 +02:00
|
|
|
|
2024-02-11 21:44:29 +02:00
|
|
|
// cityName, {x, y}, weatherValue, icon
|
2025-03-12 13:16:35 +02:00
|
|
|
export type CityData = [cityName: string, {x: number, y: number }, weatherValue: number, icon: string | undefined]
|
2024-02-11 21:44:29 +02:00
|
|
|
|
2024-02-08 15:25:51 +02:00
|
|
|
export function drawOnMap(
|
|
|
|
|
ctx: CanvasRenderingContext2D,
|
|
|
|
|
imgArr: [HTMLImageElement, HTMLImageElement],
|
2025-03-12 13:16:35 +02:00
|
|
|
cityData: CityData[],
|
2024-02-08 15:25:51 +02:00
|
|
|
windData: [string, string, string, boolean, WindProps],
|
2026-08-18 22:14:40 +03:00
|
|
|
overlayData: [string, string],
|
2024-02-08 15:25:51 +02:00
|
|
|
props: ResolutionPropsValue,
|
2026-08-19 17:44:46 +03:00
|
|
|
productionTemplate = false,
|
2024-02-08 15:25:51 +02:00
|
|
|
): void {
|
|
|
|
|
const [windDirection, windSpeed, windGusts, roundValues, windProp] = windData;
|
|
|
|
|
const [bgImg, arrowImg] = imgArr;
|
2026-08-18 22:14:40 +03:00
|
|
|
const [title, source] = overlayData;
|
2024-02-08 15:25:51 +02:00
|
|
|
|
|
|
|
|
// bg
|
|
|
|
|
ctx.drawImage(bgImg, 0, 0);
|
|
|
|
|
|
2026-08-18 22:14:40 +03:00
|
|
|
// City value badges use a consistent height and expand horizontally to fit the value.
|
|
|
|
|
cityData.forEach(([, {x, y}, value]) => {
|
2024-02-08 15:25:51 +02:00
|
|
|
const localX = props.offsetX + x * props.scale;
|
|
|
|
|
const localY = props.offsetY + y * props.scale;
|
2026-08-18 22:14:40 +03:00
|
|
|
const numericValue = roundValues ? Math.round(value) : value.toFixed(1);
|
|
|
|
|
const stringValue = numericValue.toString().replace(".", ",");
|
2024-02-08 15:25:51 +02:00
|
|
|
|
2026-08-19 22:01:42 +03:00
|
|
|
ctx.font = `bold ${props.fontSize}px Monda`;
|
2026-08-19 17:44:46 +03:00
|
|
|
const badgeWidth = getBadgeWidth(ctx, stringValue, props);
|
2024-02-08 15:25:51 +02:00
|
|
|
ctx.fillStyle = "#FFFFFF";
|
2026-08-18 22:14:40 +03:00
|
|
|
ctx.fillRect(localX - badgeWidth / 2, localY - props.boxSize / 2, badgeWidth, props.boxSize);
|
2024-02-08 15:25:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// weather values
|
2026-08-19 17:44:46 +03:00
|
|
|
cityData.forEach(([city, {x, y}, value, icon]) => {
|
2024-02-08 15:25:51 +02:00
|
|
|
const localX = props.offsetX + x * props.scale;
|
|
|
|
|
const localY = props.offsetY + y * props.scale;
|
|
|
|
|
|
2026-08-19 22:01:42 +03:00
|
|
|
ctx.font = `bold ${props.fontSize}px Monda`;
|
2024-02-11 21:44:29 +02:00
|
|
|
ctx.textAlign = "center";
|
|
|
|
|
ctx.textBaseline = "middle";
|
2024-02-08 15:25:51 +02:00
|
|
|
ctx.fillStyle = "#000000";
|
2025-03-20 22:17:28 +02:00
|
|
|
const numericValue = roundValues ? Math.round(value) : value.toFixed(1);
|
2024-02-08 15:25:51 +02:00
|
|
|
const stringValue = numericValue.toString().replace(".", ",");
|
|
|
|
|
ctx.fillText(stringValue, localX, localY);
|
2024-02-11 21:44:29 +02:00
|
|
|
|
|
|
|
|
if (icon) {
|
2026-08-20 10:46:26 +03:00
|
|
|
const iconImage = getLoadedWeatherIcon(icon);
|
|
|
|
|
if (!iconImage) return;
|
|
|
|
|
const iconSize = productionTemplate ? 190 : props.boxSize * 1.35;
|
|
|
|
|
const badgeWidth = getBadgeWidth(ctx, stringValue, props);
|
2026-08-19 17:44:46 +03:00
|
|
|
|
|
|
|
|
if (productionTemplate) {
|
|
|
|
|
const layout = faktiskaMarkerLayout[city as FaktiskaStation];
|
|
|
|
|
const side = layout?.iconSide ?? "right";
|
|
|
|
|
const gap = layout?.iconGap ?? 22;
|
|
|
|
|
const offsetX = layout?.iconOffsetX ?? 0;
|
|
|
|
|
const offsetY = layout?.iconOffsetY ?? 0;
|
|
|
|
|
const iconCenterX = side === "right"
|
2026-08-20 10:46:26 +03:00
|
|
|
? localX + badgeWidth / 2 + gap + iconSize / 2 + offsetX
|
|
|
|
|
: localX - badgeWidth / 2 - gap - iconSize / 2 + offsetX;
|
|
|
|
|
drawWeatherIcon(ctx, iconImage, iconCenterX, localY + offsetY, iconSize);
|
2026-08-19 17:44:46 +03:00
|
|
|
} else {
|
2026-08-20 10:46:26 +03:00
|
|
|
drawWeatherIcon(ctx, iconImage, localX + badgeWidth / 2 + 10 + iconSize / 2, localY, iconSize);
|
2026-08-19 17:44:46 +03:00
|
|
|
}
|
2024-02-11 21:44:29 +02:00
|
|
|
}
|
2024-02-08 15:25:51 +02:00
|
|
|
});
|
|
|
|
|
|
2026-08-18 22:14:40 +03:00
|
|
|
drawTitleOverlay(ctx, title, source, props.width);
|
|
|
|
|
|
2024-02-08 15:25:51 +02:00
|
|
|
if (!props.showWind) return;
|
|
|
|
|
|
|
|
|
|
// wind values
|
2026-08-19 22:01:42 +03:00
|
|
|
ctx.font = `bold ${props.windFontSize}px Monda`;
|
2024-02-08 15:25:51 +02:00
|
|
|
ctx.textAlign = "left";
|
|
|
|
|
ctx.textBaseline = "top";
|
|
|
|
|
ctx.fillStyle = "#FFFFFF";
|
|
|
|
|
const boxMiddleX = 1675;
|
|
|
|
|
|
|
|
|
|
const directionWidth = ctx.measureText(windDirection).width;
|
|
|
|
|
ctx.fillText(windDirection, windProp.offsetX + boxMiddleX - directionWidth / 2 + 30, (windProp.offsetY + 370)*windProp.scaleY);
|
|
|
|
|
const angleInDegrees = getAngleFromString(windDirection);
|
|
|
|
|
drawRotatedImage(ctx, arrowImg, windProp.offsetX + boxMiddleX - directionWidth / 2 - 30, (windProp.offsetY + 370)*windProp.scaleY, angleInDegrees);
|
|
|
|
|
|
|
|
|
|
const speedWidth = ctx.measureText(windSpeed).width;
|
|
|
|
|
ctx.fillText(windSpeed, windProp.offsetX + boxMiddleX - speedWidth / 2 - 30, (windProp.offsetY + 575)*windProp.scaleY);
|
2026-08-19 22:01:42 +03:00
|
|
|
ctx.font = "bold 25px Monda";
|
2024-02-08 15:25:51 +02:00
|
|
|
ctx.fillText("M/S", windProp.offsetX + boxMiddleX + speedWidth / 2 - 22, (windProp.offsetY + 590)*windProp.scaleY);
|
|
|
|
|
|
2026-08-19 22:01:42 +03:00
|
|
|
ctx.font = `bold ${props.windFontSize}px Monda`;
|
2024-02-08 15:25:51 +02:00
|
|
|
const gustsWidth = ctx.measureText(windGusts).width;
|
|
|
|
|
ctx.fillText(windGusts, windProp.offsetX + boxMiddleX - gustsWidth / 2 - 30, (windProp.offsetY + 790)*windProp.scaleY);
|
2026-08-19 22:01:42 +03:00
|
|
|
ctx.font = "bold 25px Monda";
|
2024-02-08 15:25:51 +02:00
|
|
|
ctx.fillText("M/S", windProp.offsetX + boxMiddleX + gustsWidth / 2 - 22, (windProp.offsetY + 805)*windProp.scaleY);
|
|
|
|
|
}
|
2026-08-18 22:14:40 +03:00
|
|
|
|
2026-08-20 10:46:26 +03:00
|
|
|
function getBadgeWidth(ctx: CanvasRenderingContext2D, value: string, props: ResolutionPropsValue): number {
|
|
|
|
|
ctx.font = `bold ${props.fontSize}px Monda`;
|
|
|
|
|
return Math.max(props.boxSize, Math.ceil(ctx.measureText(value).width + props.fontSize * 0.8));
|
2026-08-19 17:44:46 +03:00
|
|
|
}
|
|
|
|
|
|
2026-08-20 10:46:26 +03:00
|
|
|
export function drawWeatherIcon(ctx: CanvasRenderingContext2D, image: HTMLImageElement, centerX: number, centerY: number, size: number): void {
|
|
|
|
|
ctx.drawImage(image, centerX - size / 2, centerY - size / 2, size, size);
|
2026-08-18 23:01:07 +03:00
|
|
|
}
|
|
|
|
|
|
2026-08-18 22:14:40 +03:00
|
|
|
function drawTitleOverlay(ctx: CanvasRenderingContext2D, title: string, source: string, canvasWidth: number): void {
|
|
|
|
|
if (!title.trim() && !source.trim()) return;
|
|
|
|
|
|
|
|
|
|
const scale = canvasWidth / 1920;
|
|
|
|
|
const right = 34 * scale;
|
|
|
|
|
const top = 38 * scale;
|
|
|
|
|
const horizontalPadding = 24 * scale;
|
|
|
|
|
const titleHeight = 62 * scale;
|
|
|
|
|
const sourceHeight = 38 * scale;
|
|
|
|
|
|
|
|
|
|
ctx.textAlign = "left";
|
|
|
|
|
ctx.textBaseline = "middle";
|
2026-08-19 22:01:42 +03:00
|
|
|
ctx.font = `bold ${30 * scale}px Monda`;
|
2026-08-18 22:14:40 +03:00
|
|
|
const titleText = title.trim().toUpperCase();
|
2026-08-20 09:52:58 +03:00
|
|
|
const titleWidth = ctx.measureText(titleText).width + horizontalPadding * 2;
|
2026-08-18 22:14:40 +03:00
|
|
|
const titleX = canvasWidth - right - titleWidth;
|
|
|
|
|
|
|
|
|
|
ctx.fillStyle = "#9b0008";
|
|
|
|
|
ctx.fillRect(titleX, top, titleWidth, titleHeight);
|
|
|
|
|
ctx.fillStyle = "#FFFFFF";
|
|
|
|
|
ctx.fillText(titleText, titleX + horizontalPadding, top + titleHeight / 2);
|
|
|
|
|
|
|
|
|
|
if (source.trim()) {
|
2026-08-19 22:01:42 +03:00
|
|
|
ctx.font = `bold ${19 * scale}px Monda`;
|
2026-08-18 22:14:40 +03:00
|
|
|
const sourceText = source.trim().toUpperCase();
|
2026-08-20 09:52:58 +03:00
|
|
|
const sourceWidth = ctx.measureText(sourceText).width + horizontalPadding * 2;
|
2026-08-18 22:14:40 +03:00
|
|
|
const sourceX = canvasWidth - right - sourceWidth;
|
|
|
|
|
ctx.fillStyle = "#FFFFFF";
|
|
|
|
|
ctx.fillRect(sourceX, top + titleHeight, sourceWidth, sourceHeight);
|
|
|
|
|
ctx.fillStyle = "#000000";
|
|
|
|
|
ctx.fillText(sourceText, sourceX + horizontalPadding, top + titleHeight + sourceHeight / 2);
|
|
|
|
|
}
|
|
|
|
|
}
|