Format map values, round option

This commit is contained in:
Guntis Smaukstelis
2023-08-08 23:05:55 +03:00
parent d6cc56e789
commit a52eb2c54f
2 changed files with 54 additions and 18 deletions
+27 -12
View File
@@ -20,13 +20,15 @@ export const MapView: Component<{ data: () => ResultKeyVal[] }> = ({ data }) =>
direction: createSignal(""), direction: createSignal(""),
speed: createSignal(""), speed: createSignal(""),
gusts: createSignal(""), gusts: createSignal(""),
roundValues: createSignal(false),
} }
function getWindData(): [string, string, string] { function getWindData(): [string, string, string, boolean] {
return [ return [
windSignals.direction[0](), windSignals.direction[0](),
windSignals.speed[0](), windSignals.speed[0](),
windSignals.gusts[0](), windSignals.gusts[0](),
windSignals.roundValues[0](),
]; ];
} }
@@ -35,7 +37,12 @@ export const MapView: Component<{ data: () => ResultKeyVal[] }> = ({ data }) =>
const img = new Image(); const img = new Image();
img.onload = () => { img.onload = () => {
setImg(img); setImg(img);
drawOnMap(ctx, [img, arrowImg], lastCoords, getWindData()); drawOnMap(
ctx,
[img, arrowImg],
lastCoords,
getWindData(),
);
}; };
img.src = mapUrl; img.src = mapUrl;
}); });
@@ -49,7 +56,12 @@ export const MapView: Component<{ data: () => ResultKeyVal[] }> = ({ data }) =>
typeof value === "number" ? value : -99, typeof value === "number" ? value : -99,
]); ]);
lastCoords = coordsAndData; lastCoords = coordsAndData;
drawOnMap(ctx, [getImg(), arrowImg], coordsAndData, getWindData()); drawOnMap(
ctx,
[getImg(),arrowImg],
coordsAndData,
getWindData(),
);
}); });
return ( return (
@@ -64,10 +76,11 @@ function drawOnMap(
ctx: CanvasRenderingContext2D, ctx: CanvasRenderingContext2D,
imgArr: [HTMLImageElement, HTMLImageElement], imgArr: [HTMLImageElement, HTMLImageElement],
coords: [string, {x: number, y: number }, number][], coords: [string, {x: number, y: number }, number][],
windData: [string, string, string], windData: [string, string, string, boolean],
): void { ): void {
const size = 80; const size = 80;
const [windDirection, windSpeed, windGusts, roundValues] = windData;
const [bgImg, arrowImg] = imgArr; const [bgImg, arrowImg] = imgArr;
// bg // bg
@@ -87,7 +100,9 @@ function drawOnMap(
ctx.fillStyle = "#000000"; ctx.fillStyle = "#000000";
coords.forEach(([, {x, y}, value]) => { coords.forEach(([, {x, y}, value]) => {
ctx.rect(x, y, size, size); ctx.rect(x, y, size, size);
ctx.fillText(`${value}`, x + size/2, y + size/2); const numericValue = roundValues ? Math.round(value) : value;
const stringValue = numericValue.toString().replace(".", ",");
ctx.fillText(stringValue, x + size/2, y + size/2);
}); });
// wind values // wind values
@@ -97,19 +112,19 @@ function drawOnMap(
ctx.fillStyle = "#FFFFFF"; ctx.fillStyle = "#FFFFFF";
const boxMiddleX = 1675; const boxMiddleX = 1675;
const directionWidth = ctx.measureText(windData[0]).width; const directionWidth = ctx.measureText(windDirection).width;
ctx.fillText(windData[0], boxMiddleX - directionWidth / 2 + 30, 370); ctx.fillText(windDirection, boxMiddleX - directionWidth / 2 + 30, 370);
const angleInDegrees = getAngleFromString(windData[0]); const angleInDegrees = getAngleFromString(windDirection);
drawRotatedImage(ctx, arrowImg, boxMiddleX - directionWidth / 2 - 30, 370, angleInDegrees); drawRotatedImage(ctx, arrowImg, boxMiddleX - directionWidth / 2 - 30, 370, angleInDegrees);
const speedWidth = ctx.measureText(windData[1]).width; const speedWidth = ctx.measureText(windSpeed).width;
ctx.fillText(windData[1], boxMiddleX - speedWidth / 2 - 30, 575); ctx.fillText(windSpeed, boxMiddleX - speedWidth / 2 - 30, 575);
ctx.font = "bold 25px Rubik"; ctx.font = "bold 25px Rubik";
ctx.fillText("M/S", boxMiddleX + speedWidth / 2 - 22, 590); ctx.fillText("M/S", boxMiddleX + speedWidth / 2 - 22, 590);
ctx.font = "bold 45px Rubik"; ctx.font = "bold 45px Rubik";
const gustsWidth = ctx.measureText(windData[2]).width; const gustsWidth = ctx.measureText(windGusts).width;
ctx.fillText(windData[2], boxMiddleX - gustsWidth / 2 - 30, 790); ctx.fillText(windGusts, boxMiddleX - gustsWidth / 2 - 30, 790);
ctx.font = "bold 25px Rubik"; ctx.font = "bold 25px Rubik";
ctx.fillText("M/S", boxMiddleX + gustsWidth / 2 - 22, 805); ctx.fillText("M/S", boxMiddleX + gustsWidth / 2 - 22, 805);
} }
+27 -6
View File
@@ -4,6 +4,7 @@ export interface WindSignals {
direction: Signal<string>; direction: Signal<string>;
speed: Signal<string>; speed: Signal<string>;
gusts: Signal<string>; gusts: Signal<string>;
roundValues: Signal<boolean>;
} }
export const WindInputs: Component<{signals: WindSignals}> = ( export const WindInputs: Component<{signals: WindSignals}> = (
@@ -11,21 +12,41 @@ export const WindInputs: Component<{signals: WindSignals}> = (
direction: [getDirection, setDirection], direction: [getDirection, setDirection],
speed: [getSpeed, setSpeed], speed: [getSpeed, setSpeed],
gusts: [getGusts, setGusts], gusts: [getGusts, setGusts],
roundValues: [getRoundValues, setRoundValues],
}} }}
) => { ) => {
return ( return (
<> <>
<div> <div>
<input type="text" onInput={e => setDirection(e.target.value)} /> <label>
Vēja virziens <input
type="checkbox"
checked={getRoundValues()}
onChange={e => setRoundValues(e.target.checked)}
/>
Round values
</label>
</div> </div>
<div> <div>
<input type="text" onInput={e => setSpeed(e.target.value)} /> <input
Vēja ātrums type="text"
onInput={e => setDirection(e.target.value)}
/>
Wind direction
</div> </div>
<div> <div>
<input type="text" onInput={e => setGusts(e.target.value)} /> <input
Brāzmas type="text"
onInput={e => setSpeed(e.target.value)}
/>
Wind speed
</div>
<div>
<input
type="text"
onInput={e => setGusts(e.target.value)}
/>
Gusts
</div> </div>
</> </>
); );