Window

View source

Stores the dimensions and canvas selector for a window component.

With the default WindowPlugin configuration, the primary window entity includes a Window and MainWindow component. Set initPrimaryWindow to false when the application will create its own window entities.

selector defaults to undefined and identifies an existing canvas by CSS selector when one is available; without a matching canvas, a new one is used. width and height default to 720 and 360. They are the dimensions stored by the component. When the entity’s canvas is opened, these values are synchronized with it, and a resize operation updates them after the canvas is resized. Calling set() only changes the component’s stored dimensions; it does not resize the canvas by itself. Use WindowCommands to queue operations such as resizing, fullscreen, pointer lock, or pointer capture.

The Windows resource maps window entity ids to their active HTMLCanvasElement. Window operations use that resource to find the canvas for this component’s entity. If the entity has no registered canvas, the operation does nothing.

serialize() includes width, height, and selector, and deserialize() restores all three fields. The current copy() implementation copies only width and height; it leaves the target’s selector unchanged. clone() delegates to copy() with a new Window, so a clone has an undefined selector even when the source has one.

Examples

import { World } from '@wimaengine/ecs'
import { Window } from '@wimaengine/window'

const world = new World()
const entity = world.spawn([
  new Window({ selector: '#game', width: 1280, height: 720 })
])

const window = world.get(entity, Window)
window?.set(1024, 576)
console.log(window?.getWidth(), window?.getHeight()) // 1024 576

Constructor


constructor(options?: WindowOptions)

View source

Creates window state for a canvas-backed entity.

When no options are supplied, the component starts with a width of 720, a height of 360, and no selector.

Parameters

NameDescriptionDefault
options?

Initial dimensions and optional CSS selector.

{}

Properties


Methods


staticcopy(source: Window, target?: Window): Window

View source

Copies the stored dimensions from one window component to another.

Only width and height are copied. The target’s selector is retained; an omitted target therefore keeps the default undefined selector.

Parameters

NameDescriptionDefault
source

Component whose dimensions are copied.

—
target?

Component to update.

...

Returns

The updated target component.

staticclone(target: Window): Window

View source

Creates a new component with the source window’s stored dimensions.

This delegates to Window.copy, so the source selector is not copied and the returned component has an undefined selector.

Parameters

NameDescriptionDefault
target

Component whose dimensions are cloned.

—

Returns

A new component with copied dimensions.

staticserialize(value: Window): WindowSerial

View source

Serializes the window state for persistence.

The returned value contains the stored width, height, and selector.

Parameters

NameDescriptionDefault
value

Component to serialize.

—

Returns

The serialized window state.

staticdeserialize(value: WindowSerial, out?: Window): Window

View source

Restores serialized window state into a component.

All three serialized fields are assigned to out. When out is omitted, a new Window is created.

Parameters

NameDescriptionDefault
value

Serialized width, height, and selector.

—
out?

Component to populate.

...

Returns

The populated component.

getWidth(): number

View source

Returns the width currently stored by the component.

Returns

The stored width.

getHeight(): number

View source

Returns the height currently stored by the component.

Returns

The stored height.

set(width: number, height: number): void

View source

Updates the dimensions stored by the component.

This method does not resize the associated canvas. A queued resize through WindowCommands performs the window operation and then synchronizes the resulting dimensions with this component.

Parameters

NameDescriptionDefault
width

New stored width.

—
height

New stored height.

—