constructor(options?: WindowOptions)
View sourceCreates 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.
Class
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.
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 576constructor(options?: WindowOptions)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.
copy(source: Window, target?: Window): WindowCopies 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.
clone(target: Window): WindowCreates 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.
default(): Windowserialize(value: Window): WindowSerialSerializes the window state for persistence.
The returned value contains the stored width, height, and selector.
deserialize(value: WindowSerial, out?: Window): WindowRestores serialized window state into a component.
All three serialized fields are assigned to out. When out is omitted,
a new Window is created.
getWidth(): numberReturns the width currently stored by the component.
getHeight(): numberReturns the height currently stored by the component.
set(width: number, height: number): voidUpdates 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.