import Renderer from 'WTF-Adventure/WTF-Adventure/client/renderer/renderer.js'Renderer
Handles all canvas-based rendering for the game world
Constructor Summary
| Public Constructor | ||
| public |
constructor(backgroundCanvas: HTMLCanvasElement, entitiesCanvas: HTMLCanvasElement, foregroundCanvas: HTMLCanvasElement, textCanvas: HTMLCanvasElement, cursorCanvas: HTMLCanvasElement, game: Game) Default constructor |
|
Member Summary
| Public Members | ||
| public |
Whether tile animations are enabled |
|
| public |
List of tiles that are currently animated |
|
| public |
Whether to automatically centre the camera on the player |
|
| public |
backContext: CanvasRenderingContext2D The 2D rendering context for the background |
|
| public |
backgroundCanvas: HTMLCanvasElement The background tile canvas element |
|
| public |
The current brightness level (0-100) |
|
| public |
The camera used to determine visible area |
|
| public |
canvases: HTMLCanvasElement[] Array of all canvas elements managed by the renderer |
|
| public |
context: CanvasRenderingContext2D The 2D rendering context for entities |
|
| public |
contexts: CanvasRenderingContext2D[] Array of all drawing contexts used for tile and entity rendering |
|
| public |
cursorCanvas: HTMLCanvasElement The cursor canvas element |
|
| public |
cursorContext: CanvasRenderingContext2D The 2D rendering context for the cursor |
|
| public |
Whether to render debug information |
|
| public |
Whether to draw entity levels above characters |
|
| public |
Whether to draw entity names above characters |
|
| public |
Whether to draw the target cell indicator |
|
| public |
|
|
| public |
The entities controller |
|
| public |
entitiesCanvas: HTMLCanvasElement The entities canvas element |
|
| public |
Whether the client is running in Firefox |
|
| public |
The base font size for rendered text |
|
| public |
When true the next frame will be rendered even if the camera has not moved |
|
| public |
foreContext: CanvasRenderingContext2D The 2D rendering context for the foreground |
|
| public |
foregroundCanvas: HTMLCanvasElement The foreground tile canvas element |
|
| public |
The current frames per second value |
|
| public |
Number of frames rendered in the current second |
|
| public |
Reference to the main game instance |
|
| public |
The input handler |
|
| public |
lastTarget: Number[] The last target cell position [x, y] |
|
| public |
|
|
| public |
Whether the client is running on a mobile device |
|
| public |
|
|
| public |
The last rendered camera position [x, y] |
|
| public |
Timeout handle used to debounce resize events |
|
| public |
The current rendering scale factor |
|
| public |
The screen height in pixels |
|
| public |
The screen width in pixels |
|
| public |
Whether the selected cell highlight is currently visible |
|
| public |
|
|
| public |
|
|
| public |
When true the render loop is paused |
|
| public |
Whether the client is running on a tablet device |
|
| public |
|
|
| public |
textCanvas: HTMLCanvasElement The text canvas element |
|
| public |
textContext: CanvasRenderingContext2D The 2D rendering context for text |
|
| public |
The size of a single tile in pixels |
|
| public |
|
|
| public |
Timestamp used for FPS calculation |
|
Method Summary
| Public Methods | ||
| public |
adjustBrightness(level: Number) Adjusts the canvas overlay opacity to achieve the desired brightness level |
|
| public |
Detects whether the current device is mobile, tablet or Firefox and caches the result |
|
| public |
checkDirty(rectOne: Object, source: Object, x: Number, y: Number) Checks whether a bounding rectangle overlaps with nearby entities or tiles and marks them dirty |
|
| public |
clearAll() Clears every tracked rendering context |
|
| public |
Clears the entities canvas context |
|
| public |
Clears only the background and foreground drawing contexts |
|
| public |
clearScreen(context: CanvasRenderingContext2D) Clears the full area of the given context |
|
| public |
Clears the entire text canvas context |
|
| public |
Clears the pixel area occupied by a single tile cell on the given context |
|
| public |
draw() Draws all static (non-animated) visible tiles onto the background and foreground contexts |
|
| public |
Draws all animated tiles onto the entities context each frame |
|
| public |
drawCellHighlight(x: Number, y: Number, colour: String) Draws a coloured highlight rectangle over a grid cell at the given grid coordinates |
|
| public |
drawCellRect(x: Number, y: Number, colour: String) Draws a hollow coloured rectangle outline around a single grid cell |
|
| public |
Draws the custom mouse cursor sprite on the cursor canvas |
|
| public |
Draws debug overlays such as FPS, player position and the pathing grid |
|
| public |
Iterates over all visible entities and draws each one that has a loaded sprite |
|
| public |
drawEntity(entity: Entity) Renders a single entity including its shadow, weapon overlay, item sparks and status effects |
|
| public |
drawEntityBack(entity: *) Function used to draw special effects prior to rendering the entity. |
|
| public |
drawEntityFore(entity: Entity) Draws active status-effect sprite overlays on top of the entity after it has been rendered |
|
| public |
drawFPS() Calculates and draws the current frames-per-second counter on screen |
|
| public |
drawHealth(entity: Entity) Draws a health bar above an entity when its health bar is visible |
|
| public |
Draws floating damage and status info text above entities |
|
| public |
Draws the username and/or level label above an entity |
|
| public |
Highlights all non-zero cells in the pathing grid for debugging |
|
| public |
Draws the player's current grid position to the screen for debugging |
|
| public |
drawScaledImage(context: CanvasRenderingContext2D, image: HTMLImageElement, x: Number, y: Number, width: Number, height: Number, dx: Number, dy: Number) Draws a portion of an image scaled to the current drawing scale factor |
|
| public |
Draws a highlight over the cell the player has selected when it is not adjacent |
|
| public |
Draws a highlight over the cell currently under the mouse cursor |
|
| public |
drawText(text: String, x: Number, y: Number, centered: Boolean, colour: String, strokeColour: String) Draws a string of text at the specified canvas coordinates |
|
| public |
drawTile(context: CanvasRenderingContext2D, tileId: Number, tileset: HTMLImageElement, setWidth: Number, gridWidth: Number, cellId: Number) Draws a single tile from the tileset onto the given context at the correct grid position |
|
| public |
focus() Focuses every tracked rendering context |
|
| public |
forEachAnimatedTile(callback: Function) Iterates over all currently tracked animated tiles and invokes a callback for each |
|
| public |
forEachCanvas(callback: Function) Iterates over every tracked canvas element and invokes a callback for each |
|
| public |
forEachContext(callback: Function) Iterates over every tracked rendering context and invokes a callback for each |
|
| public |
forEachDrawingContext(callback: Function) Iterates over drawing contexts (background and foreground) skipping the entities context |
|
| public |
forEachVisibleEntity(callback: Function) Iterates over all entities occupying visible grid cells and invokes a callback for each |
|
| public |
forEachVisibleIndex(callback: Function, offset: Number) Iterates over every map index currently visible to the camera and invokes a callback |
|
| public |
forEachVisibleTile(callback: Function, offset: Number) Iterates over all tile IDs in the visible area and invokes a callback for each |
|
| public |
Returns the drawing scale factor, clamped to 2 on mobile devices |
|
| public |
getEntityBounds(entity: Entity): Object Calculates and returns the bounding rectangle for an entity in screen space |
|
| public |
Returns the current rendering scale factor from the game |
|
| public |
getTargetBounds(x: Number, y: Number): Object Calculates and returns the bounding rectangle for the current target cell in screen space |
|
| public |
getTileBounds(tile: Tile): Object Calculates and returns the bounding rectangle for an animated tile in screen space |
|
| public |
getTileset(): HTMLImageElement Returns the currently active tileset image |
|
| public |
getUpscale(): Number Returns the upscale factor capped at a maximum of 2 |
|
| public |
Returns true if the current camera position matches the last rendered frame position |
|
| public |
Returns true if the current device is a mobile or tablet |
|
| public |
isVisiblePosition(x: Number, y: Number): Boolean Returns true when the given grid position falls within the camera viewport |
|
| public |
Creates a new Camera and adjusts centring settings based on device capabilities |
|
| public |
Initialises scale and drawing scale values and disables image smoothing on all contexts |
|
| public |
Recalculates screen dimensions based on the camera grid size and resizes all canvases |
|
| public |
Loads the shadow and sparks static sprites from the entity sprite store |
|
| public |
render() Main render function called each frame to draw the full game scene |
|
| public |
resize() Handles window resize events by debouncing a full renderer refresh |
|
| public |
restore() Calls restore on every tracked rendering context |
|
| public |
Restores the state of every tracked rendering context |
|
| public |
saveAll() Saves the state of every tracked rendering context |
|
| public |
Stores the current camera position as the last rendered frame |
|
| public |
setCameraView(context: CanvasRenderingContext2D) Translates a context so that the camera position maps to the top-left of the canvas |
|
| public |
setEntities(entities: Entities) Sets the entities controller used during rendering |
|
| public |
Sets the input handler used during rendering |
|
| public |
Sets the map instance used during rendering |
|
| public |
setTileset(tileset: HTMLImageElement) Sets the tileset image used for tile rendering |
|
| public |
stop() Stops all rendering and fills each canvas with the background colour |
|
| public |
Rebuilds the list of animated tiles that are currently within the visible camera area |
|
| public |
Applies the camera translation to all drawing (non-entity) contexts |
|
| public |
Applies the camera translation to every tracked rendering context |
|
| public |
Updates forceRendering based on whether the device is portable and the camera is centred |
|
Public Constructors
public constructor(backgroundCanvas: HTMLCanvasElement, entitiesCanvas: HTMLCanvasElement, foregroundCanvas: HTMLCanvasElement, textCanvas: HTMLCanvasElement, cursorCanvas: HTMLCanvasElement, game: Game) source
Default constructor
Params:
| Name | Type | Attribute | Description |
| backgroundCanvas | HTMLCanvasElement | the canvas for background tiles |
|
| entitiesCanvas | HTMLCanvasElement | the canvas for entities |
|
| foregroundCanvas | HTMLCanvasElement | the canvas for foreground tiles |
|
| textCanvas | HTMLCanvasElement | the canvas for text and UI labels |
|
| cursorCanvas | HTMLCanvasElement | the canvas for the cursor |
|
| game | Game | an instance of the game |
Public Members
public contexts: CanvasRenderingContext2D[] source
Array of all drawing contexts used for tile and entity rendering
Public Methods
public adjustBrightness(level: Number) source
Adjusts the canvas overlay opacity to achieve the desired brightness level
Params:
| Name | Type | Attribute | Description |
| level | Number | the brightness level between 0 and 100 |
public checkDevice() source
Detects whether the current device is mobile, tablet or Firefox and caches the result
public checkDirty(rectOne: Object, source: Object, x: Number, y: Number) source
Checks whether a bounding rectangle overlaps with nearby entities or tiles and marks them dirty
public clearScreen(context: CanvasRenderingContext2D) source
Clears the full area of the given context
Params:
| Name | Type | Attribute | Description |
| context | CanvasRenderingContext2D | the context to clear |
public clearTile(context: CanvasRenderingContext2D, gridWidth: Number, cellId: Number) source
Clears the pixel area occupied by a single tile cell on the given context
public draw() source
Draws all static (non-animated) visible tiles onto the background and foreground contexts
public drawCellHighlight(x: Number, y: Number, colour: String) source
Draws a coloured highlight rectangle over a grid cell at the given grid coordinates
public drawCellRect(x: Number, y: Number, colour: String) source
Draws a hollow coloured rectangle outline around a single grid cell
public drawDebugging() source
Draws debug overlays such as FPS, player position and the pathing grid
public drawEntities() source
Iterates over all visible entities and draws each one that has a loaded sprite
public drawEntity(entity: Entity) source
Renders a single entity including its shadow, weapon overlay, item sparks and status effects
Params:
| Name | Type | Attribute | Description |
| entity | Entity | the entity to draw |
public drawEntityBack(entity: *) source
Function used to draw special effects prior to rendering the entity.
Params:
| Name | Type | Attribute | Description |
| entity | * |
public drawEntityFore(entity: Entity) source
Draws active status-effect sprite overlays on top of the entity after it has been rendered
Params:
| Name | Type | Attribute | Description |
| entity | Entity | the entity to draw effects for |
public drawHealth(entity: Entity) source
Draws a health bar above an entity when its health bar is visible
Params:
| Name | Type | Attribute | Description |
| entity | Entity | the entity whose health bar should be drawn |
public drawName(entity: Entity) source
Draws the username and/or level label above an entity
Params:
| Name | Type | Attribute | Description |
| entity | Entity | the entity whose name should be drawn |
public drawScaledImage(context: CanvasRenderingContext2D, image: HTMLImageElement, x: Number, y: Number, width: Number, height: Number, dx: Number, dy: Number) source
Draws a portion of an image scaled to the current drawing scale factor
Params:
| Name | Type | Attribute | Description |
| context | CanvasRenderingContext2D | the context to draw onto |
|
| image | HTMLImageElement | the source image |
|
| x | Number | the source x offset in unscaled pixels |
|
| y | Number | the source y offset in unscaled pixels |
|
| width | Number | the width of the source region in unscaled pixels |
|
| height | Number | the height of the source region in unscaled pixels |
|
| dx | Number | the destination x position in unscaled pixels |
|
| dy | Number | the destination y position in unscaled pixels |
public drawSelectedCell() source
Draws a highlight over the cell the player has selected when it is not adjacent
public drawText(text: String, x: Number, y: Number, centered: Boolean, colour: String, strokeColour: String) source
Draws a string of text at the specified canvas coordinates
public drawTile(context: CanvasRenderingContext2D, tileId: Number, tileset: HTMLImageElement, setWidth: Number, gridWidth: Number, cellId: Number) source
Draws a single tile from the tileset onto the given context at the correct grid position
Params:
| Name | Type | Attribute | Description |
| context | CanvasRenderingContext2D | the context to draw onto |
|
| tileId | Number | the tile identifier |
|
| tileset | HTMLImageElement | the tileset image |
|
| setWidth | Number | the number of tiles per row in the tileset |
|
| gridWidth | Number | the width of the map in tiles |
|
| cellId | Number | the flat index of the cell in the map grid |
public forEachAnimatedTile(callback: Function) source
Iterates over all currently tracked animated tiles and invokes a callback for each
Params:
| Name | Type | Attribute | Description |
| callback | Function | called with each animated Tile instance |
public forEachCanvas(callback: Function) source
Iterates over every tracked canvas element and invokes a callback for each
Params:
| Name | Type | Attribute | Description |
| callback | Function | called with each HTMLCanvasElement |
public forEachContext(callback: Function) source
Iterates over every tracked rendering context and invokes a callback for each
Params:
| Name | Type | Attribute | Description |
| callback | Function | called with each CanvasRenderingContext2D |
public forEachDrawingContext(callback: Function) source
Iterates over drawing contexts (background and foreground) skipping the entities context
Params:
| Name | Type | Attribute | Description |
| callback | Function | called with each drawing CanvasRenderingContext2D |
public forEachVisibleEntity(callback: Function) source
Iterates over all entities occupying visible grid cells and invokes a callback for each
Params:
| Name | Type | Attribute | Description |
| callback | Function | called with each visible Entity instance |
public forEachVisibleIndex(callback: Function, offset: Number) source
Iterates over every map index currently visible to the camera and invokes a callback
public forEachVisibleTile(callback: Function, offset: Number) source
Iterates over all tile IDs in the visible area and invokes a callback for each
public getDrawingScale(): Number source
Returns the drawing scale factor, clamped to 2 on mobile devices
public getEntityBounds(entity: Entity): Object source
Calculates and returns the bounding rectangle for an entity in screen space
Params:
| Name | Type | Attribute | Description |
| entity | Entity | the entity to compute bounds for |
public getTargetBounds(x: Number, y: Number): Object source
Calculates and returns the bounding rectangle for the current target cell in screen space
public getTileBounds(tile: Tile): Object source
Calculates and returns the bounding rectangle for an animated tile in screen space
Params:
| Name | Type | Attribute | Description |
| tile | Tile | the tile to compute bounds for |
public getTileset(): HTMLImageElement source
Returns the currently active tileset image
Return:
| HTMLImageElement |
public hasRenderedFrame(): Boolean source
Returns true if the current camera position matches the last rendered frame position
public isVisiblePosition(x: Number, y: Number): Boolean source
Returns true when the given grid position falls within the camera viewport
public loadCamera() source
Creates a new Camera and adjusts centring settings based on device capabilities
public loadRenderer() source
Initialises scale and drawing scale values and disables image smoothing on all contexts
public loadSizes() source
Recalculates screen dimensions based on the camera grid size and resizes all canvases
public loadStaticSprites() source
Loads the shadow and sparks static sprites from the entity sprite store
public setCameraView(context: CanvasRenderingContext2D) source
Translates a context so that the camera position maps to the top-left of the canvas
Params:
| Name | Type | Attribute | Description |
| context | CanvasRenderingContext2D | the context to apply the camera translation to |
public setEntities(entities: Entities) source
Sets the entities controller used during rendering
Params:
| Name | Type | Attribute | Description |
| entities | Entities | the entities controller to use |
public setInput(input: Input) source
Sets the input handler used during rendering
Params:
| Name | Type | Attribute | Description |
| input | Input | the input handler to use |
public setMap(map: Map) source
Sets the map instance used during rendering
Params:
| Name | Type | Attribute | Description |
| map | Map | the map to use |
public setTileset(tileset: HTMLImageElement) source
Sets the tileset image used for tile rendering
Params:
| Name | Type | Attribute | Description |
| tileset | HTMLImageElement | the tileset to use |
public updateAnimatedTiles() source
Rebuilds the list of animated tiles that are currently within the visible camera area
public updateDrawingView() source
Applies the camera translation to all drawing (non-entity) contexts
public verifyCentration() source
Updates forceRendering based on whether the device is portable and the camera is centred
Reference
Source
Test
