Provides a lightweight canvas rendering abstraction for HTML5 <canvas> elements. The module:
RenderState)Provides basic drawing primitives:
This module is intended to simplify common 2D canvas setup and rendering operations.
Creation → Update → Destruction
createRenderState(canvasId).drawCircle, drawRect, drawLine, drawText) are called each frame or whenever visual updates are needed.clear() can be used to reset the canvas before redrawing.resize() is automatically invoked.This module does not provide explicit cleanup functionality.
The registered resize event listener remains active for the lifetime of the page. If disposal behavior is required, additional logic should be implemented to remove the listener.
interface RenderStateRepresents the rendering environment.
| Property | Type | Description |
|---|---|---|
canvas |
HTMLCanvasElement |
Target canvas element. |
ctx |
CanvasRenderingContext2D |
2D rendering context used for drawing. |
createRenderState(canvasId: string): RenderStateCreates and initializes a rendering state.
| Name | Type | Description |
|---|---|---|
canvasId |
string |
DOM id of the target canvas element. |
RenderState
| Condition | Error |
|---|---|
| Canvas element cannot be found | Error("Canvas not found!") |
resize(state: RenderState): voidResizes the canvas to match the browser window.
| Name | Type |
|---|---|
state |
RenderState |
window.devicePixelRatio to support high-DPI displays.clear(state: RenderState): voidClears the entire canvas.
| Name | Type |
|---|---|
state |
RenderState |
Removes all rendered content from the canvas.
drawCircle(state, x, y, radius, color): voidDraws a filled circle.
| Name | Type | Description |
|---|---|---|
state |
RenderState |
Render state. |
x |
number |
Center X coordinate. |
y |
number |
Center Y coordinate. |
radius |
number |
Circle radius. |
color |
string |
Fill color. |
drawRect(state, x, y, width, height, color): voidDraws a filled rectangle.
| Name | Type | Description |
|---|---|---|
state |
RenderState |
Render state. |
x |
number |
Top-left X coordinate. |
y |
number |
Top-left Y coordinate. |
width |
number |
Rectangle width. |
height |
number |
Rectangle height. |
color |
string |
Fill color. |
drawLine(state, x1, y1, x2, y2, color, lineWidth?): voidDraws a line segment.
| Name | Type | Description |
|---|---|---|
state |
RenderState |
Render state. |
x1 |
number |
Start X coordinate. |
y1 |
number |
Start Y coordinate. |
x2 |
number |
End X coordinate. |
y2 |
number |
End Y coordinate. |
color |
string |
Stroke color. |
lineWidth |
number |
Optional line width. Defaults to 1. |
drawText(state, text, x, y, color, font?): voidDraws text on the canvas.
| Name | Type | Description |
|---|---|---|
state |
RenderState |
Render state. |
text |
string |
Text content. |
x |
number |
Text X position. |
y |
number |
Text baseline Y position. |
color |
string |
Text color. |
font |
string |
CSS font declaration. Defaults to "16px sans-serif". |
The module scales the canvas using:
const dpr = window.devicePixelRatio || 1;
This ensures rendered content appears sharp on Retina and other high-density displays.
The canvas maintains two separate sizes:
Internal pixel resolution
canvas.width
canvas.height
Visual CSS size
canvas.style.width
canvas.style.height
The internal resolution is multiplied by the device pixel ratio while the CSS size matches the viewport dimensions.
After resizing, the context transform is reset:
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
This allows drawing operations to continue using logical screen coordinates rather than manually scaling values.
All drawing helpers operate directly on the shared CanvasRenderingContext2D stored in RenderState. No scene graph, buffering, or object tracking is maintained.
import {
createRenderState,
clear,
drawCircle,
drawRect,
drawLine,
drawText
} from "./render";
const render = createRenderState("game-canvas");
function frame() {
clear(render);
drawRect(render, 50, 50, 200, 100, "#4caf50");
drawCircle(render, 200, 200, 40, "#2196f3");
drawLine(render, 100, 100, 300, 200, "#ffffff", 2);
drawText(render, "Hello Canvas", 50, 300, "#000");
requestAnimationFrame(frame);
}
frame();
<canvas> elementCanvasRenderingContext2Dwindow resize eventswindow.devicePixelRatiorequestAnimationFrame() rendering loopsfillRect, arc, fillText, stroke)