Skip to content

Commit f9aa936

Browse files
committed
ui-charts: add striped color for blinking signals
Signed-off-by: Theo Macron <[email protected]>
1 parent 84d6d4f commit f9aa936

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

Diff for: storybook/stories/ui-charts/spaceTimeChart/layers.stories.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ const OCCUPANCY_BLOCKS = [
7676
spaceStart: 10 * KILOMETER,
7777
spaceEnd: 14 * KILOMETER,
7878
color: OCCUPANCY_WARNING,
79+
blinking: true,
7980
},
8081
{
8182
timeStart: +START_DATE + 44 * MINUTE,

Diff for: ui-charts/src/spaceTimeChart/components/OccupancyBlockLayer.tsx

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,31 @@ export type OccupancyBlock = {
99
spaceStart: number;
1010
spaceEnd: number;
1111
color: string;
12+
blinking?: boolean;
1213
};
1314

1415
export type OccupancyBlockLayerProps = {
1516
occupancyBlocks: OccupancyBlock[];
1617
};
1718

19+
const getFillStyle = (ctx: CanvasRenderingContext2D, color: string, isBlinking?: boolean) => {
20+
if (isBlinking) {
21+
const patternCanvas = document.createElement('canvas');
22+
patternCanvas.width = 8;
23+
patternCanvas.height = 8;
24+
const pctx = patternCanvas.getContext('2d')!;
25+
pctx.clearRect(0, 0, 8, 8);
26+
pctx.strokeStyle = color;
27+
pctx.lineWidth = 2;
28+
pctx.beginPath();
29+
pctx.moveTo(0, 8);
30+
pctx.lineTo(8, 0);
31+
pctx.stroke();
32+
return ctx.createPattern(patternCanvas, 'repeat')!;
33+
}
34+
return color;
35+
};
36+
1837
export const OccupancyBlockLayer = ({ occupancyBlocks }: OccupancyBlockLayerProps) => {
1938
const drawOccupancyBlockLayer = useCallback<DrawingFunction>(
2039
(ctx, { getTimePixel, getSpacePixel }) => {
@@ -24,7 +43,7 @@ export const OccupancyBlockLayer = ({ occupancyBlocks }: OccupancyBlockLayerProp
2443
const width = getTimePixel(occupancyBlock.timeEnd) - x;
2544
const height = getSpacePixel(occupancyBlock.spaceEnd) - y;
2645

27-
ctx.fillStyle = occupancyBlock.color;
46+
ctx.fillStyle = getFillStyle(ctx, occupancyBlock.color, occupancyBlock.blinking);
2847
ctx.fillRect(x, y, width, height);
2948
}
3049
},

0 commit comments

Comments
 (0)