Skip to content
This repository was archived by the owner on Aug 5, 2025. It is now read-only.

Commit 6f28907

Browse files
committed
speedspacechart: fix blinking in speed limit tags by importing images only once
Signed-off-by: Alice Khoudli <[email protected]>
1 parent f672fd7 commit 6f28907

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

ui-speedspacechart/src/components/helpers/drawElements/speedLimitTags.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ const TEXT_PADDING_TOP = 1;
4343
const ICON_BACKGROUND_WIDTH = 24;
4444
const ICON_BACKGROUND_HEIGHT = 24;
4545

46-
export const drawSpeedLimitTags = async ({
46+
export const loadSpeedLimitTagsImages = async () => ({
47+
questionImage: await loadSvgImage(questionBlobUrl),
48+
alertFillImage: await loadSvgImage(alertFillBlobUrl),
49+
});
50+
51+
export const drawSpeedLimitTags = ({
4752
ctx,
4853
width,
4954
height: marginTop,
5055
store,
56+
images,
5157
}: SpeedLimitTagsLayerDrawFunctionParams) => {
5258
const {
5359
speedLimitTags,
@@ -65,9 +71,6 @@ export const drawSpeedLimitTags = async ({
6571

6672
const maxPosition = maxPositionValue(store.speeds);
6773

68-
const questionImage = await loadSvgImage(questionBlobUrl);
69-
const alertFillImage = await loadSvgImage(alertFillBlobUrl);
70-
7174
let speedLimitTagsBackgroundColor = LINEAR_LAYERS_BACKGROUND_COLOR.FIRST;
7275

7376
if (electricalProfiles && powerRestrictions) {
@@ -127,7 +130,7 @@ export const drawSpeedLimitTags = async ({
127130
}
128131

129132
if (tag === 'incompatible' || tag === 'missing_from_train') {
130-
const image = tag === 'incompatible' ? alertFillImage : questionImage;
133+
const image = tag === 'incompatible' ? images.alertFillImage : images.questionImage;
131134

132135
ctx.fillStyle = color;
133136

@@ -143,16 +146,16 @@ export const drawSpeedLimitTags = async ({
143146
ICON_BACKGROUND_HEIGHT,
144147
cornerRadius
145148
);
146-
147-
drawSvgImageWithColor(
148-
ctx,
149-
image,
150-
iconXPosition + ICON_OFFSET,
151-
iconYPosition + ICON_OFFSET,
152-
ICON_WIDTH,
153-
ICON_HEIGHT,
154-
WHITE.hex()
155-
);
149+
if (image !== null)
150+
drawSvgImageWithColor(
151+
ctx,
152+
image,
153+
iconXPosition + ICON_OFFSET,
154+
iconYPosition + ICON_OFFSET,
155+
ICON_WIDTH,
156+
ICON_HEIGHT,
157+
WHITE.hex()
158+
);
156159
} else {
157160
ctx.fillStyle = 'white';
158161
ctx.font = '600 12px "IBM Plex Sans"';
@@ -172,11 +175,11 @@ export const drawSpeedLimitTags = async ({
172175
ctx.clearRect(width - MARGIN_RIGHT, 0, width, LINEAR_LAYERS_HEIGHTS.SPEED_LIMIT_TAGS_HEIGHT);
173176
};
174177

175-
export const computeTooltip = async ({
178+
export const computeTooltip = ({
176179
width,
177180
height: marginTop,
178181
store,
179-
}: DrawFunctionParams): Promise<tooltipInfos | null> => {
182+
}: DrawFunctionParams): tooltipInfos | null => {
180183
const { speedLimitTags, ratioX, leftOffset, cursor } = store;
181184

182185
const { MARGIN_TOP, MARGIN_LEFT } = MARGINS;

ui-speedspacechart/src/components/layers/SpeedLimitTagsLayer.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
1-
import React, { useEffect, useRef } from 'react';
1+
import React, { useEffect, useRef, useState } from 'react';
22

33
import type { Store, tooltipInfos } from '../../types/chartTypes';
44
import Tooltip from '../common/Tooltip';
55
import { LINEAR_LAYERS_HEIGHTS } from '../const';
6-
import { drawSpeedLimitTags, computeTooltip } from '../helpers/drawElements/speedLimitTags';
6+
import {
7+
drawSpeedLimitTags,
8+
computeTooltip,
9+
loadSpeedLimitTagsImages,
10+
} from '../helpers/drawElements/speedLimitTags';
711

812
type SpeedLimitTagsLayerProps = {
913
width: number;
1014
marginTop: number;
1115
store: Store;
1216
};
17+
type ImagesType = {
18+
questionImage: HTMLImageElement | null;
19+
alertFillImage: HTMLImageElement | null;
20+
};
1321
const TOOLTIP_HEIGHT = 40;
1422
const MARGIN_ADJUSTMENT = 2;
1523

1624
const SpeedLimitTagsLayer = ({ width, marginTop, store }: SpeedLimitTagsLayerProps) => {
1725
const canvas = useRef<HTMLCanvasElement>(null);
1826
const tooltip = useRef<tooltipInfos | null>();
27+
const [images, setImages] = useState<ImagesType>({ questionImage: null, alertFillImage: null });
28+
29+
useEffect(() => {
30+
if (images.questionImage !== null && images.alertFillImage !== null) return;
31+
const fetchImages = async () => {
32+
const { questionImage, alertFillImage } = await loadSpeedLimitTagsImages();
33+
setImages({ questionImage, alertFillImage });
34+
};
35+
fetchImages();
36+
}, [images]);
1937

2038
useEffect(() => {
2139
const updateCanvas = async () => {
@@ -36,6 +54,7 @@ const SpeedLimitTagsLayer = ({ width, marginTop, store }: SpeedLimitTagsLayerPro
3654
width,
3755
height: marginTop,
3856
store: restrictedStore,
57+
images,
3958
});
4059
};
4160
updateCanvas();
@@ -48,6 +67,7 @@ const SpeedLimitTagsLayer = ({ width, marginTop, store }: SpeedLimitTagsLayerPro
4867
store.layersDisplay.electricalProfiles,
4968
store.layersDisplay.powerRestrictions,
5069
store.speeds,
70+
images,
5171
]);
5272

5373
useEffect(() => {

ui-speedspacechart/src/types/chartTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export type SpeedLimitTagsLayerDrawFunctionParams = {
105105
width: number;
106106
height: number;
107107
store: SpeedLimitTagsLayerDrawingStore;
108+
images: { questionImage: HTMLImageElement | null; alertFillImage: HTMLImageElement | null };
108109
};
109110

110111
export type TrainDetails = {

0 commit comments

Comments
 (0)