A super simple and lightweight TypeScript rendering engine for making 2D JavaScript games using HTML Canvases.
Usage | Installation | Examples | Documentation
import Engine, { StaticLayer, IEntity } from '@zacktherrien/typescript-render-engine';
const engine = new Engine();
// create a new layer.
const backgroundZIndex = 0;
const backgroundLayer = new StaticLayer(backgroundZIndex);
// add the layer to the engine
engine.registerLayer(backgroundLayer);
// create an entity that extends the IEntity interface.
const terrain: IEntity = new Terrain(); // your class
// add the new entity to the layer
backgroundLayer.addEntity(terrain);
// render static background:
backgroundLayer.allowRenderOnNextFrame();
// start rendering dynamic layers
engine.start();
This package uses Github Packages as the NPM repository. See here for help.
Add the following to the .npmrc file at the root of your project's folder:
@zacktherrien:registry=https://npm.pkg.github.com
Then to install:
npm install @zacktherrien/typescript-render-engine
start()Starts rendering the registered layers.stop()Stops rendering.registerLayer(layer: IRenderingLayer)Add a layer to the enginelayerThe layer to be added
getLayer(layerIndex: LayerIndex): IRenderingLayerGet a previous registered layer.layerIndexa number representing the Z-Index of the layer.
readonly layerIndex: LayerIndexnumber representing the zindex of the layer
constructor(layerIndex: LayerIndex, initialWidth?: number, initialHeight?: number, initialX: number = 0, initialY: number = 0,)Creates a rendering layer.layerIndexNumber representing the z-index of the layer on the screen.entityAn optional, default first entity of the layer.
addEntity(entity: IEntity)Add an entity to be renderedentityThe entity to be added
removeEntity(entity: IEntity)Remove an entity from being renderedentityThe entity to be deleted
getContext(): CanvasRenderingContext2DGet the rendering context of this layerupdate(deltaTime: number)Updates all entities sequentially in the order they were added.deltaTimeTime (inms) since the last frame was rendered
render()Renders all the entities in this layer sequentially in the order they were added.getWidth(): numberGet the width of the layergetHeight(): numberGet the height of the layergetX(): numberGet the x position of the layer, from the left side of the dom.getY(): numberGet the y position of the layer, from the top of the dom.resize(width: number, height: number, resizeMethod: ResizeMethod = ResizeMethod.FROM_ORIGIN): numberChange the size of the layer.widthThe new width of the layerheightThe new height of the layerresizeMethodHow the resize will be performed, from the origin or from the center of layer.
setPosition(x: number, height: number)Change the position of the layer.xthe new x position of the layer.ythe new y position of the layer.
Will update and render all entities in the layer when a new frame becomes available
Will render all entities in the layer when told via the allowRenderOnNextFrame function.
allowRenderOnNextFrame()When the next frame becomes available, the render function will be called.
Once the deferredTime (in MS) has been reached, the next frame will update and render. After another deferredTime the layer will update and rerender.
constructor(deferredTime: number, layerIndex: LayerIndex, initialWidth?: number, initialHeight?: number, initialX: number = 0, initialY: number = 0,)Creates a deferred rendering layer.
RenderLayerFunctionFunction signature of a layer's render function.UpdateLayerFunctionFunction signature of a layer's update function.RenderFunctionFunction signature of an entity's render function.UpdateFunctionFunction signature of an entity's update function.LayerIndexNumber representing the Z-Index of a layer.ResizeMethodEnum representing the resizing strategy of a layerFROM_ORIGINResize the layer conserving the (0,0) point at the same position in the screen.FROM_CENTERResize the layer conserving the center point at the same position in the screen.
/**
* Enum representing all layers' z-index.
*/
const enum LayerIndex {
BACKGROUND = 0,
HOUSES = 1,
PLAYERS = 2,
HUD = 10,
}