Skip to content

Commit fb6ac1f

Browse files
author
Oskar Widmark
committed
feat: add display type
1 parent 9e53cb6 commit fb6ac1f

File tree

4 files changed

+69
-20
lines changed

4 files changed

+69
-20
lines changed

src/ColorTab.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { FormControl, Stack, Grid2 } from '@mui/material';
2-
import { ColorPreset, ColorSettings, VisualizationType } from './types';
2+
import {
3+
ColorPreset,
4+
ColorSettings,
5+
DisplayType,
6+
VisualizationType,
7+
} from './types';
38
import { TitledSelect } from './components/TitledSelect';
49
import { ColorField } from './components/ColorField';
510

@@ -15,6 +20,7 @@ export function ColorTab(props: {
1520
highlightColor,
1621
backgroundColor,
1722
visualizationType,
23+
displayType,
1824
},
1925
setColorSettings,
2026
} = props;
@@ -76,6 +82,14 @@ export function ColorTab(props: {
7682
}
7783
options={Object.values(VisualizationType)}
7884
/>
85+
<TitledSelect
86+
title="Display type"
87+
value={displayType}
88+
onSelect={(value) =>
89+
setColorSettings({ displayType: value as DisplayType })
90+
}
91+
options={Object.values(DisplayType)}
92+
/>
7993
</>
8094
);
8195
}

src/canvas-controller.ts

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { ColorPreset, DrawData, SortValue, VisualizationType } from './types';
1+
import {
2+
ColorPreset,
3+
DisplayType,
4+
DrawData,
5+
SortValue,
6+
VisualizationType,
7+
} from './types';
28
import { hsvToRgbHex, rgbHexToHsv } from './utils';
39

410
export class CanvasController {
@@ -18,6 +24,7 @@ export class CanvasController {
1824
columnColor2: string;
1925
highlightColor: string;
2026
visualizationType: VisualizationType;
27+
displayType: DisplayType;
2128
},
2229
) {}
2330

@@ -74,6 +81,34 @@ export class CanvasController {
7481
this.context.visualizationType = value;
7582
}
7683

84+
set displayType(value: DisplayType) {
85+
this.context.displayType = value;
86+
}
87+
88+
get height() {
89+
switch (this.context.displayType) {
90+
case DisplayType.Full:
91+
return this.canvas2dCtx.canvas.height;
92+
case DisplayType.Square:
93+
return Math.min(
94+
this.canvas2dCtx.canvas.width,
95+
this.canvas2dCtx.canvas.height,
96+
);
97+
}
98+
}
99+
100+
get width() {
101+
switch (this.context.displayType) {
102+
case DisplayType.Full:
103+
return this.canvas2dCtx.canvas.width;
104+
case DisplayType.Square:
105+
return Math.min(
106+
this.canvas2dCtx.canvas.width,
107+
this.canvas2dCtx.canvas.height,
108+
);
109+
}
110+
}
111+
77112
getGradientColor(value: number) {
78113
// eslint-disable-next-line prefer-const
79114
let [h1, s1, v1] = rgbHexToHsv(this.context.columnColor1);
@@ -180,10 +215,10 @@ export class CanvasController {
180215
const rect = canvas.getBoundingClientRect();
181216

182217
const colIndex = Math.floor(
183-
((mouseX - rect.left) / canvas.width) * this.context.columnNbr,
218+
((mouseX - rect.left) / this.width) * this.context.columnNbr,
184219
);
185220
const colHeight = Math.floor(
186-
((canvas.height - (mouseY - rect.top)) / canvas.height) *
221+
((this.height - (mouseY - rect.top)) / this.height) *
187222
this.context.columnNbr,
188223
);
189224

@@ -249,7 +284,7 @@ export class CanvasController {
249284
};
250285

251286
private drawColumn = (arr: SortValue[], i: number, color?: string) => {
252-
const width = this.canvas2dCtx.canvas.width / this.context.columnNbr;
287+
const width = this.width / this.context.columnNbr;
253288
const height = this.getColumnHeight(arr[i].value);
254289
const startX = width * i;
255290
const startY = this.getColumnStartY(arr[i].value);
@@ -261,10 +296,7 @@ export class CanvasController {
261296
private getColumnStartY = (value: number) => {
262297
switch (this.context.visualizationType) {
263298
case VisualizationType.Dots:
264-
return (
265-
(this.canvas2dCtx.canvas.height / this.context.columnNbr) *
266-
(value - 1)
267-
);
299+
return (this.height / this.context.columnNbr) * value;
268300
default:
269301
return 0;
270302
}
@@ -273,14 +305,11 @@ export class CanvasController {
273305
private getColumnHeight = (value: number) => {
274306
switch (this.context.visualizationType) {
275307
case VisualizationType.Bars:
276-
return (
277-
(this.canvas2dCtx.canvas.height / this.context.columnNbr) *
278-
(value + 1)
279-
);
308+
return (this.height / this.context.columnNbr) * (value + 1);
280309
case VisualizationType.Dots:
281-
return this.canvas2dCtx.canvas.width / this.context.columnNbr;
310+
return this.width / this.context.columnNbr;
282311
case VisualizationType.Colors:
283-
return this.canvas2dCtx.canvas.height;
312+
return this.height;
284313
}
285314
};
286315

@@ -290,29 +319,27 @@ export class CanvasController {
290319
width: number,
291320
height: number,
292321
) => {
293-
const ctxHeight = this.canvas2dCtx.canvas.height;
294322
this.canvas2dCtx.fillRect(
295323
startX,
296-
Math.floor(ctxHeight) - Math.floor(startY) - Math.floor(height),
324+
Math.floor(this.height) - Math.floor(startY) - Math.floor(height),
297325
Math.floor(width),
298326
Math.floor(height),
299327
);
300328
};
301329

302330
private clearColumn = (idx: number) => {
303-
const width = this.canvas2dCtx.canvas.width / this.context.columnNbr;
331+
const width = this.width / this.context.columnNbr;
304332
const startX = width * idx;
305333

306334
this.clearRect(startX, width);
307335
};
308336

309337
private clearRect = (startX: number, width: number) => {
310-
const ctxHeight = this.canvas2dCtx.canvas.height;
311338
this.canvas2dCtx.clearRect(
312339
startX - 1,
313340
0,
314341
Math.floor(width) + 2,
315-
Math.floor(ctxHeight),
342+
Math.floor(this.height),
316343
);
317344
};
318345

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
AppState,
33
ColorPreset,
4+
DisplayType,
45
ResetPreset,
56
Settings,
67
SortName,
@@ -41,6 +42,7 @@ export const INIT_SETTINGS: Settings = {
4142
backgroundColor: '#000000',
4243
highlightColor: '#ff0000',
4344
visualizationType: VisualizationType.Bars,
45+
displayType: DisplayType.Full,
4446
soundVolume: DEFAULT_SOUND_VOLUME,
4547
soundType: DEFAULT_SOUND_TYPE,
4648
frequencyRange: [200, 640],

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ export enum VisualizationType {
5454
Colors = 'Colors',
5555
}
5656

57+
export enum DisplayType {
58+
Full = 'Full',
59+
Square = 'Square',
60+
}
61+
5762
export enum ColorPreset {
5863
Rainbow = 'Rainbow',
5964
Custom = 'Custom',
@@ -91,6 +96,7 @@ export type ColorSettings = {
9196
highlightColor: string;
9297
backgroundColor: string;
9398
visualizationType: VisualizationType;
99+
displayType: DisplayType;
94100
};
95101

96102
export type SoundSettings = {

0 commit comments

Comments
 (0)