Skip to content

Commit

Permalink
Utilize translate when drawing cells.
Browse files Browse the repository at this point in the history
  • Loading branch information
samccone committed Aug 31, 2016
1 parent 7e922aa commit c41ea14
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
33 changes: 14 additions & 19 deletions cell_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class CellRenderer {
return 'grey';
}

if (this.grid.isColumnHovered(colIndex) || this.grid.isRowHovered(rowIndex)) {
if (this.grid.isColumnHovered(colIndex) ||
this.grid.isRowHovered(rowIndex)) {
return '#AAA';
}

Expand All @@ -30,7 +31,7 @@ class CellRenderer {
innerHeight);
}

private getCellDrawCoords(rowIndex: number, colIndex: number):{
getCellDrawCoords(rowIndex: number, colIndex: number):{
leftX: number,
topY: number,
innerWidth: number,
Expand All @@ -40,7 +41,8 @@ class CellRenderer {
this.grid.s(((1 + colIndex) * this.grid.dimensions.cellMargin) +
colIndex * this.grid.dimensions.cellWidth);
let topY = this.grid.s(this.grid.dimensions.columnHeaderHeight) +
this.grid.s(((1 + rowIndex) * this.grid.dimensions.cellMargin) + rowIndex * this.grid.dimensions.cellHeight)
this.grid.s(((1 + rowIndex) * this.grid.dimensions.cellMargin) +
rowIndex * this.grid.dimensions.cellHeight);

let innerWidth = this.grid.s(this.grid.dimensions.cellWidth);
let innerHeight = this.grid.s(this.grid.dimensions.cellHeight);
Expand Down Expand Up @@ -74,25 +76,18 @@ class CellRenderer {
rowIndex,
columnIndex);

let {leftX, topY, innerWidth, innerHeight} = this.getCellDrawCoords(
rowIndex,
columnIndex);

let width = this.grid.s(this.grid.dimensions.cellWidth)
let height = this.grid.s(this.grid.dimensions.cellHeight)
this.grid.debug && this.grid.debugInfo.drawnCells++;

this.grid.ctx.fillRect(
leftX - this.grid.viewportOffset.x,
topY - this.grid.viewportOffset.y,
innerWidth,
innerHeight);

this.grid.ctx.fillStyle = 'red';
this.grid.ctx.fillRect(0, 0, width, height);
this.grid.ctx.fillStyle = 'red';

this.grid.drawText(
this.grid.s(12),
leftX - this.grid.viewportOffset.x,
topY + innerHeight / 2 - this.grid.viewportOffset.y,
String(rowIndex) + ' - ' + String(columnIndex))
this.grid.drawText(
this.grid.s(12),
0,
innerHeight / 2,
String(rowIndex) + ' - ' + String(columnIndex))
}
}

Expand Down
13 changes: 12 additions & 1 deletion grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,18 @@ class InfiniteGrid {
const cell = row.columns[columnIndex];
if (!cell) break;

this.cellRenderer.drawCell(rowIndex, columnIndex, cell, this);
this.ctx.save()
let cellCoords = this.cellRenderer.getCellDrawCoords(rowIndex, columnIndex);
this.ctx.translate(
cellCoords.leftX - this.viewportOffset.x,
cellCoords.topY - this.viewportOffset.y);

this.cellRenderer.drawCell(
rowIndex,
columnIndex,
cell);

this.ctx.restore()
}

this.renderRowGuide(rowIndex);
Expand Down

0 comments on commit c41ea14

Please sign in to comment.