forked from lineupjs/lineupjs
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementation of a canvas texture renderer, for Caleydo/lineup#35 and …
- Loading branch information
Showing
7 changed files
with
198 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@import '../vars'; | ||
|
||
##{$lu-css_prefix}-texture-container{ | ||
display: flex; | ||
overflow: hidden; | ||
img{ | ||
margin-right: 5px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import EngineRanking from './EngineRanking'; | ||
import {IDataRow} from '../../provider/ADataProvider'; | ||
import NumbersColumn from '../../model/NumbersColumn'; | ||
|
||
export interface ITextureRenderer{ | ||
update(rankings: EngineRanking[], localData: IDataRow[][]): void; | ||
destroy(): void; | ||
show(): void; | ||
hide(): void; | ||
} | ||
|
||
export default class CanvasTextureRenderer implements ITextureRenderer { | ||
|
||
readonly node: HTMLElement; | ||
readonly canvas: any; | ||
|
||
constructor(parent: Element) { | ||
this.node = parent.ownerDocument.createElement('main'); | ||
this.node.id = 'lu-texture-container'; | ||
parent.appendChild(this.node); | ||
this.canvas = parent.ownerDocument.createElement('canvas'); | ||
} | ||
|
||
update(rankings: EngineRanking[], localData: IDataRow[][]) { | ||
this.node.innerHTML = ''; //remove all children | ||
|
||
rankings.forEach((r, i) => { | ||
const grouped = r.groupData(localData[i]); | ||
|
||
r.ranking.flatColumns.forEach((column) => { | ||
let newElement = null; | ||
if (column instanceof NumbersColumn) { | ||
const col = <NumbersColumn>column; | ||
newElement = this.generateImage(grouped.map((value) => { | ||
return (<any>value).v[(<any>col.desc).column]; | ||
}), col.getRawColorScale()) | ||
} else { | ||
newElement = this.node.ownerDocument.createElement('img'); | ||
} | ||
newElement.style.width = column.getWidth() + 'px'; | ||
this.node.appendChild(newElement); | ||
}); | ||
}); | ||
} | ||
|
||
private generateImage(data: any[][], colorScale: any){ | ||
let height = data.length; | ||
let width = 0; | ||
if(height > 0){ | ||
width = data[0].length; | ||
} | ||
|
||
this.canvas.setAttribute('height', '' + height); | ||
this.canvas.setAttribute('width', '' + width); | ||
|
||
const ctx = <CanvasRenderingContext2D>this.canvas.getContext('2d'); | ||
|
||
data.forEach((row, y) => { | ||
row.forEach((value, x) => { | ||
ctx.fillStyle = colorScale(value); | ||
ctx.fillRect(x, y, 1, 1); | ||
}); | ||
}); | ||
|
||
ctx.save(); | ||
|
||
const image = this.node.ownerDocument.createElement('img'); | ||
image.src = this.canvas.toDataURL(); | ||
|
||
return image; | ||
} | ||
|
||
|
||
destroy() { | ||
this.node.remove(); | ||
} | ||
|
||
show() { | ||
this.node.classList.remove('hidden'); | ||
} | ||
|
||
hide() { | ||
this.node.classList.add('hidden'); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import CanvasTextureRenderer from '../engine/CanvasTextureRenderer'; | ||
import DataProvider from '../../provider/ADataProvider'; | ||
import {AEventDispatcher} from '../../utils'; | ||
|
||
|
||
export default class TextureRenderer extends AEventDispatcher { | ||
|
||
private readonly renderer: CanvasTextureRenderer; | ||
|
||
constructor(parent: HTMLElement, public data: DataProvider) { | ||
super(); | ||
|
||
this.renderer = new CanvasTextureRenderer(data, parent); | ||
} | ||
|
||
get ctx() { | ||
return this.renderer.ctx; | ||
} | ||
|
||
protected createEventList() { | ||
return super.createEventList(); /*.concat([TaggleRenderer.EVENT_HOVER_CHANGED])*/ | ||
} | ||
|
||
destroy() { | ||
this.renderer.destroy(); | ||
} | ||
|
||
update() { | ||
this.renderer.update(); | ||
} | ||
|
||
changeDataStorage(data: DataProvider) { | ||
this.renderer.changeDataStorage(data); | ||
this.update(); | ||
} | ||
|
||
show() { | ||
this.renderer.show(); | ||
} | ||
|
||
hide() { | ||
this.renderer.hide(); | ||
} | ||
} |