import { EventEmitter } from 'eventemitter3'; import { IGridRenderer, IGrid } from './Interfaces'; import { GridRendererEventMap } from './Types'; export default class GridCanvasRenderer extends EventEmitter implements IGridRenderer { private spriteSheet; private particleSystem; private ctx; private canvas; /** * @internal */ private internalListeners; private lastPlayheadX; private mouseX; private mouseY; private dragging; private lastView; /** * @param canvas - The canvas DOM element to render to * @param gridWidth - The width of the grid, in tiles * @param gridHeight - The height of the grid, in tiles */ constructor(containerEl: HTMLElement, gridWidth: number, gridHeight: number); private emitTileEvent; /** * Updates the this.mouseX and this.mouseY variables based on where the mouse is on the canvas * @param e - The touch or click event that contains the new "mouse" position */ updateCanvasMousePosition(e: MouseEvent | Touch): void; /** * Resets the this.mouseX and this.mouseY variables. * Call this when the mouse leaves the canvas or the screen is not being touched. */ resetCanvasMousePosition(): void; /** * Update, then draw the current state of the app to the canvas element. * @param grid - The grid to be rendered * @param mouseX - The x position of the mouse on the canvas * @param mouseY - The y position of the mouse on the canvas */ update(grid: IGrid): void; private getSpriteSheet; /** * Draw the current state of the app to the canvas element. * @private * @param grid - The grid to be rendered * @param mouseX - The x position of the mouse on the canvas * @param mouseY - The y position of the mouse on the canvas */ private draw; /** * Gets the "heat" of every tile by calculating how many particles are on top of the tile * @returns An array of numbers from 0 to 1, representing the "heat" of each tile */ private getParticleHeatMap; /** * Converts coordinates in "pixel space" to coordinates in "tile space". * In essence, if you pass in an (x, y) position on the canvas, * this returns the corresponding (x, y) position on the grid. * @param x - The x position, in pixels, to get the corresponding grid position for * @param y - The y position, in pixels, to get the corresponding grid position for * @param gridWidth - The width of the grid, in grid tiles * @param gridHeight - The height of the grid, in grid tiles * @param canvasWidth - The width of the pixel space, * typically the width of the canvas * @param canvasHeight - The height of the pixel space, * typically the height of the canvas */ pixelCoordsToTileCoords(x: number, y: number, gridWidth: number, gridHeight: number): [x: number, y: number] | false; /** * Add an event listener to a listenable object and save everything needed * for removing it later. * @internal */ private listen; /** * Remove a listener from a listenable object. * @internal */ private unlisten; setSize(newWidth: number, newHeight: number): void; dispose(): void; }