@@ -137,7 +137,7 @@ export class CanvasController {
137137 ) ;
138138 }
139139
140- getColumnColor1 ( value : number ) {
140+ getColumnColor ( value : number ) {
141141 switch ( this . context . colorPreset ) {
142142 case ColorPreset . Custom :
143143 return this . context . columnColor1 ;
@@ -148,6 +148,21 @@ export class CanvasController {
148148 }
149149 }
150150
151+ getCellColor ( value1 : number , value2 : number ) {
152+ switch ( this . context . colorPreset ) {
153+ case ColorPreset . Custom :
154+ return this . context . columnColor1 ;
155+ case ColorPreset . CustomGradient :
156+ return this . getGradientColor ( ( value1 + value2 ) / 2 ) ;
157+ case ColorPreset . Rainbow :
158+ return hsvToRgbHex (
159+ ( ( value1 + value2 ) / this . context . columnNbr ) * 180 ,
160+ 1 ,
161+ 1 ,
162+ ) ;
163+ }
164+ }
165+
151166 getHighlightColor ( ) {
152167 switch ( this . context . colorPreset ) {
153168 case ColorPreset . Custom :
@@ -172,28 +187,41 @@ export class CanvasController {
172187 this . drawAll ( arr ) ;
173188 } ;
174189
175- highlightColumns = ( arr : SortValue [ ] , indices : number [ ] ) => {
176- //if (!this.state.isSorting) throw Error('isSorting is false!');
177-
190+ highlight = ( arr : SortValue [ ] , indices : number [ ] ) => {
178191 if ( this . prevHighlightIndices ) {
179192 for ( const idx of this . prevHighlightIndices ) {
193+ if ( this . context . visualizationType === VisualizationType . Matrix ) {
194+ this . redrawCellRow ( arr , idx ) ;
195+ this . redrawCellColumn ( arr , idx ) ;
196+ continue ;
197+ }
180198 this . redrawColumn ( arr , idx ) ;
181199 }
182200 }
183201 this . prevHighlightIndices = indices ;
184202
185203 for ( const idx of indices ) {
204+ if ( this . context . visualizationType === VisualizationType . Matrix ) {
205+ this . redrawCellRow ( arr , idx , this . getHighlightColor ( ) ) ;
206+ this . redrawCellColumn ( arr , idx , this . getHighlightColor ( ) ) ;
207+ continue ;
208+ }
186209 this . redrawColumn ( arr , idx , this . getHighlightColor ( ) ) ;
187210 }
188211 } ;
189212
190- redrawColumns = ( arr : SortValue [ ] , indices : number [ ] ) => {
191- for ( const index of indices ) {
192- this . redrawColumn ( arr , index ) ;
213+ redraw = ( arr : SortValue [ ] , indices : number [ ] ) => {
214+ for ( const idx of indices ) {
215+ if ( this . context . visualizationType === VisualizationType . Matrix ) {
216+ this . redrawCellRow ( arr , idx ) ;
217+ this . redrawCellColumn ( arr , idx ) ;
218+ continue ;
219+ }
220+ this . redrawColumn ( arr , idx ) ;
193221 }
194222 } ;
195223
196- redraw = ( arr : SortValue [ ] ) => {
224+ redrawAll = ( arr : SortValue [ ] ) => {
197225 this . clearAll ( ) ;
198226 this . drawAll ( arr ) ;
199227 } ;
@@ -260,15 +288,46 @@ export class CanvasController {
260288 } ;
261289
262290 private removeHighlight = ( arr : SortValue [ ] ) => {
263- this . highlightColumns ( arr , [ ] ) ;
291+ this . highlight ( arr , [ ] ) ;
264292 } ;
265293
266294 private redrawColumn = ( arr : SortValue [ ] , i : number , color ?: string ) => {
267295 this . clearColumn ( i ) ;
268296 this . drawColumn ( arr , i , color ) ;
269297 } ;
270298
299+ private redrawCellColumn = ( arr : SortValue [ ] , i : number , color ?: string ) => {
300+ for ( let j = 0 ; j < this . context . columnNbr ; j ++ ) {
301+ this . redrawCell ( arr , i , j , color ) ;
302+ }
303+ } ;
304+
305+ private redrawCellRow = ( arr : SortValue [ ] , j : number , color ?: string ) => {
306+ for ( let i = 0 ; i < this . context . columnNbr ; i ++ ) {
307+ this . redrawCell ( arr , i , j , color ) ;
308+ }
309+ } ;
310+
311+ private redrawCell = (
312+ arr : SortValue [ ] ,
313+ i : number ,
314+ j : number ,
315+ color ?: string ,
316+ ) => {
317+ this . clearCell ( i , j ) ;
318+ this . drawCell ( arr , i , j , color ) ;
319+ } ;
320+
271321 private drawAll = ( arr : SortValue [ ] ) => {
322+ if ( this . context . visualizationType === VisualizationType . Matrix ) {
323+ for ( let i = 0 ; i < arr . length ; i ++ ) {
324+ for ( let j = 0 ; j < arr . length ; j ++ ) {
325+ this . drawCell ( arr , i , j ) ;
326+ }
327+ }
328+ return ;
329+ }
330+
272331 for ( let i = 0 ; i < arr . length ; i ++ ) {
273332 this . drawColumn ( arr , i ) ;
274333 }
@@ -283,13 +342,29 @@ export class CanvasController {
283342 ) ;
284343 } ;
285344
345+ private drawCell = (
346+ arr : SortValue [ ] ,
347+ i : number ,
348+ j : number ,
349+ color ?: string ,
350+ ) => {
351+ const width = this . width / this . context . columnNbr - 1 ;
352+ const height = this . height / this . context . columnNbr - 1 ;
353+ const startX = ( width + 1 ) * i ;
354+ const startY = ( height + 1 ) * j ;
355+
356+ this . canvas2dCtx . fillStyle =
357+ color || this . getCellColor ( arr [ i ] . value , arr [ j ] . value ) ;
358+ this . fillRect ( startX , startY , width , height ) ;
359+ } ;
360+
286361 private drawColumn = ( arr : SortValue [ ] , i : number , color ?: string ) => {
287362 const width = this . width / this . context . columnNbr ;
288363 const height = this . getColumnHeight ( arr [ i ] . value ) ;
289364 const startX = width * i ;
290365 const startY = this . getColumnStartY ( arr [ i ] . value ) ;
291366
292- this . canvas2dCtx . fillStyle = color || this . getColumnColor1 ( arr [ i ] . value ) ;
367+ this . canvas2dCtx . fillStyle = color || this . getColumnColor ( arr [ i ] . value ) ;
293368 this . fillRect ( startX , startY , width , height ) ;
294369 } ;
295370
@@ -308,7 +383,7 @@ export class CanvasController {
308383 return ( this . height / this . context . columnNbr ) * ( value + 1 ) ;
309384 case VisualizationType . Dots :
310385 return this . width / this . context . columnNbr ;
311- case VisualizationType . Colors :
386+ default :
312387 return this . height ;
313388 }
314389 } ;
@@ -331,15 +406,34 @@ export class CanvasController {
331406 const width = this . width / this . context . columnNbr ;
332407 const startX = width * idx ;
333408
334- this . clearRect ( startX , width ) ;
409+ this . clearRect ( { startX, width } ) ;
335410 } ;
336411
337- private clearRect = ( startX : number , width : number ) => {
412+ private clearCell = ( i : number , j : number ) => {
413+ const width = this . width / this . context . columnNbr ;
414+ const height = this . height / this . context . columnNbr ;
415+ const startX = width * i ;
416+ const startY = height * j ;
417+
418+ this . clearRect ( { startX, startY, width, height } ) ;
419+ } ;
420+
421+ private clearRect = ( params : {
422+ startX : number ;
423+ startY ?: number ;
424+ width : number ;
425+ height ?: number ;
426+ } ) => {
427+ const { startX, startY, width, height } = params ;
338428 this . canvas2dCtx . clearRect (
339429 startX - 1 ,
340- 0 ,
430+ startY != null
431+ ? Math . floor ( this . height ) -
432+ Math . floor ( startY ) -
433+ Math . floor ( height ?? this . height )
434+ : 0 ,
341435 Math . floor ( width ) + 2 ,
342- Math . floor ( this . height ) ,
436+ height != null ? Math . floor ( height ) + 2 : Math . floor ( this . height ) ,
343437 ) ;
344438 } ;
345439
0 commit comments