Skip to content

Commit 9e53cb6

Browse files
author
Oskar Widmark
committed
feat: add visualization type
1 parent 2bf9f0c commit 9e53cb6

File tree

4 files changed

+101
-45
lines changed

4 files changed

+101
-45
lines changed

src/ColorTab.tsx

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FormControl, Stack, Grid2 } from '@mui/material';
2-
import { ColorPreset, ColorSettings } from './types';
2+
import { ColorPreset, ColorSettings, VisualizationType } from './types';
33
import { TitledSelect } from './components/TitledSelect';
44
import { ColorField } from './components/ColorField';
55

@@ -14,51 +14,68 @@ export function ColorTab(props: {
1414
columnColor2,
1515
highlightColor,
1616
backgroundColor,
17+
visualizationType,
1718
},
1819
setColorSettings,
1920
} = props;
2021

2122
return (
22-
<FormControl component="fieldset">
23-
<Stack direction="column" spacing={2} alignItems="left">
24-
<TitledSelect
25-
title="Preset"
26-
value={colorPreset}
27-
onSelect={(value) =>
28-
setColorSettings({ colorPreset: value as ColorPreset })
29-
}
30-
options={Object.values(ColorPreset)}
31-
/>
32-
{(colorPreset === ColorPreset.Custom ||
33-
colorPreset === ColorPreset.CustomGradient) && (
34-
<Grid2 container spacing={2}>
35-
<ColorField
36-
label={
37-
colorPreset === ColorPreset.Custom ? 'Columns' : 'Columns(1)'
38-
}
39-
color={columnColor1}
40-
setColor={(value) => setColorSettings({ columnColor1: value })}
41-
/>
42-
{colorPreset === ColorPreset.CustomGradient && (
23+
<>
24+
<FormControl component="fieldset">
25+
<Stack direction="column" spacing={2} alignItems="left">
26+
<TitledSelect
27+
title="Preset"
28+
value={colorPreset}
29+
onSelect={(value) =>
30+
setColorSettings({ colorPreset: value as ColorPreset })
31+
}
32+
options={Object.values(ColorPreset)}
33+
/>
34+
{(colorPreset === ColorPreset.Custom ||
35+
colorPreset === ColorPreset.CustomGradient) && (
36+
<Grid2 container spacing={2}>
4337
<ColorField
44-
label="Columns(2)"
45-
color={columnColor2}
46-
setColor={(value) => setColorSettings({ columnColor2: value })}
38+
label={
39+
colorPreset === ColorPreset.Custom ? 'Columns' : 'Columns(1)'
40+
}
41+
color={columnColor1}
42+
setColor={(value) => setColorSettings({ columnColor1: value })}
4743
/>
48-
)}
49-
<ColorField
50-
label="Background"
51-
color={backgroundColor}
52-
setColor={(value) => setColorSettings({ backgroundColor: value })}
53-
/>
54-
<ColorField
55-
label="Highlight"
56-
color={highlightColor}
57-
setColor={(value) => setColorSettings({ highlightColor: value })}
58-
/>
59-
</Grid2>
60-
)}
61-
</Stack>
62-
</FormControl>
44+
{colorPreset === ColorPreset.CustomGradient && (
45+
<ColorField
46+
label="Columns(2)"
47+
color={columnColor2}
48+
setColor={(value) =>
49+
setColorSettings({ columnColor2: value })
50+
}
51+
/>
52+
)}
53+
<ColorField
54+
label="Background"
55+
color={backgroundColor}
56+
setColor={(value) =>
57+
setColorSettings({ backgroundColor: value })
58+
}
59+
/>
60+
<ColorField
61+
label="Highlight"
62+
color={highlightColor}
63+
setColor={(value) =>
64+
setColorSettings({ highlightColor: value })
65+
}
66+
/>
67+
</Grid2>
68+
)}
69+
</Stack>
70+
</FormControl>
71+
<TitledSelect
72+
title="Visualization type"
73+
value={visualizationType}
74+
onSelect={(value) =>
75+
setColorSettings({ visualizationType: value as VisualizationType })
76+
}
77+
options={Object.values(VisualizationType)}
78+
/>
79+
</>
6380
);
6481
}

src/canvas-controller.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ColorPreset, DrawData, SortValue } from './types';
1+
import { ColorPreset, DrawData, SortValue, VisualizationType } from './types';
22
import { hsvToRgbHex, rgbHexToHsv } from './utils';
33

44
export class CanvasController {
@@ -17,6 +17,7 @@ export class CanvasController {
1717
columnColor1: string;
1818
columnColor2: string;
1919
highlightColor: string;
20+
visualizationType: VisualizationType;
2021
},
2122
) {}
2223

@@ -69,6 +70,10 @@ export class CanvasController {
6970
this.context.highlightColor = value;
7071
}
7172

73+
set visualizationType(value: VisualizationType) {
74+
this.context.visualizationType = value;
75+
}
76+
7277
getGradientColor(value: number) {
7378
// eslint-disable-next-line prefer-const
7479
let [h1, s1, v1] = rgbHexToHsv(this.context.columnColor1);
@@ -245,13 +250,38 @@ export class CanvasController {
245250

246251
private drawColumn = (arr: SortValue[], i: number, color?: string) => {
247252
const width = this.canvas2dCtx.canvas.width / this.context.columnNbr;
248-
const height =
249-
(this.canvas2dCtx.canvas.height / this.context.columnNbr) *
250-
(arr[i].value + 1);
253+
const height = this.getColumnHeight(arr[i].value);
251254
const startX = width * i;
255+
const startY = this.getColumnStartY(arr[i].value);
252256

253257
this.canvas2dCtx.fillStyle = color || this.getColumnColor1(arr[i].value);
254-
this.fillRect(startX, 0, width, height);
258+
this.fillRect(startX, startY, width, height);
259+
};
260+
261+
private getColumnStartY = (value: number) => {
262+
switch (this.context.visualizationType) {
263+
case VisualizationType.Dots:
264+
return (
265+
(this.canvas2dCtx.canvas.height / this.context.columnNbr) *
266+
(value - 1)
267+
);
268+
default:
269+
return 0;
270+
}
271+
};
272+
273+
private getColumnHeight = (value: number) => {
274+
switch (this.context.visualizationType) {
275+
case VisualizationType.Bars:
276+
return (
277+
(this.canvas2dCtx.canvas.height / this.context.columnNbr) *
278+
(value + 1)
279+
);
280+
case VisualizationType.Dots:
281+
return this.canvas2dCtx.canvas.width / this.context.columnNbr;
282+
case VisualizationType.Colors:
283+
return this.canvas2dCtx.canvas.height;
284+
}
255285
};
256286

257287
private fillRect = (

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ResetPreset,
55
Settings,
66
SortName,
7+
VisualizationType,
78
} from './types';
89

910
export const RAINBOW_BACKGROUND_COLOR = '#282c34';
@@ -39,6 +40,7 @@ export const INIT_SETTINGS: Settings = {
3940
columnColor2: '#ffffff',
4041
backgroundColor: '#000000',
4142
highlightColor: '#ff0000',
43+
visualizationType: VisualizationType.Bars,
4244
soundVolume: DEFAULT_SOUND_VOLUME,
4345
soundType: DEFAULT_SOUND_TYPE,
4446
frequencyRange: [200, 640],

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ export type DrawData = {
4848
value: number;
4949
};
5050

51+
export enum VisualizationType {
52+
Bars = 'Bars',
53+
Dots = 'Dots',
54+
Colors = 'Colors',
55+
}
56+
5157
export enum ColorPreset {
5258
Rainbow = 'Rainbow',
5359
Custom = 'Custom',
@@ -84,6 +90,7 @@ export type ColorSettings = {
8490
columnColor2: string;
8591
highlightColor: string;
8692
backgroundColor: string;
93+
visualizationType: VisualizationType;
8794
};
8895

8996
export type SoundSettings = {

0 commit comments

Comments
 (0)