1- import { ColorPreset , DrawData , SortValue , VisualizationType } from './types' ;
1+ import {
2+ ColorPreset ,
3+ DisplayType ,
4+ DrawData ,
5+ SortValue ,
6+ VisualizationType ,
7+ } from './types' ;
28import { hsvToRgbHex , rgbHexToHsv } from './utils' ;
39
410export 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
0 commit comments