Provides a lightweight wrapper around the HTML5 Canvas 2D rendering context. The module is responsible for:
RenderState)Offering simple drawing helper functions for common primitives:
Creation → Update → Destruction
createRenderState(canvasId).RenderState object is initialized.During rendering:
clear().Draw primitives using:
drawCircle()drawRect()drawLine()drawText()resize() is automatically invoked.This module does not provide explicit cleanup.
Consumers that dynamically create and destroy renderers should remove the registered resize listener if lifecycle management is required.
RenderStateRepresents the active rendering environment.
interface RenderState {
canvas: HTMLCanvasElement;
ctx: CanvasRenderingContext2D;
}
| Property | Type | Description |
|---|---|---|
canvas |
HTMLCanvasElement |
Target canvas element. |
ctx |
CanvasRenderingContext2D |
2D drawing context used for rendering operations. |
createRenderState(canvasId: string): RenderStateCreates and initializes a rendering state.
| Name | Type | Description |
|---|---|---|
canvasId |
string |
DOM id of the target canvas element. |
RenderState
Error("Canvas not found!")
If no canvas exists with the specified id.
resize(state: RenderState): voidResizes the canvas to fill the browser viewport.
| Name | Type |
|---|---|
state |
RenderState |
window.devicePixelRatio for high-DPI rendering.clear(state: RenderState): voidClears the entire canvas.
| Name | Type |
|---|---|
state |
RenderState |
ctx.clearRect(0, 0, canvas.width, canvas.height);
Removes all previously rendered content.
state,
x,
y,
radius,
color
): void`
Draws 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. |
state,
x,
y,
width,
height,
color
): void`
Draws a filled rectangle.
| Name | Type | Description |
|---|---|---|
state |
RenderState |
Render state. |
x |
number |
Left position. |
y |
number |
Top position. |
width |
number |
Rectangle width. |
height |
number |
Rectangle height. |
color |
string |
Fill color. |
state,
x1,
y1,
x2,
y2,
color,
lineWidth?
): void`
Draws 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 |
Stroke width. Defaults to 1. |
state,
text,
x,
y,
color,
font?
): void`
Draws text on the canvas.
| Name | Type | Description |
|---|---|---|
state |
RenderState |
Render state. |
text |
string |
Text content. |
x |
number |
Text X coordinate. |
y |
number |
Text Y coordinate. |
color |
string |
Text color. |
font |
string |
Canvas font definition. Defaults to "16px sans-serif". |
The module compensates for high-density displays by scaling the canvas backing resolution using:
const dpr = window.devicePixelRatio || 1;
The canvas pixel dimensions are multiplied by dpr, while the displayed size remains equal to the viewport size.
Example:
Viewport: 1920 × 1080
DPR: 2
Canvas resolution:
3840 × 2160
Displayed size:
1920 × 1080
This prevents blurry rendering on Retina and other high-density screens.
After resizing, the transform is reset:
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
This ensures drawing functions continue to use viewport pixel coordinates rather than scaled device coordinates.
A resize listener is registered during initialization:
window.addEventListener("resize", () => resize(state));
Whenever the browser window changes size, the canvas resolution and display dimensions are recalculated automatically.
const render = createRenderState("gameCanvas");
function frame() {
clear(render);
drawCircle(render, 200, 150, 40, "red");
drawRect(render, 300, 100, 120, 80, "blue");
drawLine(render, 50, 50, 250, 200, "white", 2);
drawText(render, "Hello Canvas", 20, 30, "yellow");
requestAnimationFrame(frame);
}
frame();
<canvas id="gameCanvas"></canvas>
HTMLCanvasElementCanvasRenderingContext2Dwindow.devicePixelRatiorequestAnimationFrame()arc, fillRect, lineTo, fillText)