@coverslide/enable3d-canvasui
v0.2.0
Published
Canvas-textured HUD and menu overlay for Phaser games using enable3d
Readme
@coverslide/enable3d-canvasui
Thin enable3d adapter for the Three.js canvas UI core in
packages/three-canvasui. It takes the renderer,
camera, and cleanup lifecycle from an enable3d Phaser scene and forwards them
to @coverslide/three-canvasui. Games continue to own their UI drawing.
Development
npm install
npm run build
npm run test
npm run demonpm run demo serves the text input and scrollable list at
http://127.0.0.1:4173.
Set HOST and PORT to choose the bind address and port, for example:
HOST=0.0.0.0 PORT=8080 npm run demo.
Both demos orbit a spinning torus behind translucent canvas controls. The root
demo exercises the enable3d adapter with a minimal scene.third host (without
loading Phaser or physics). The standalone Three.js demo uses ThreeCanvasUi
directly at /packages/three-canvasui/examples/index.html on the same server.
The interactive mesh texture demo lives in the Three.js package at
/packages/three-canvasui/examples/mesh.html; the adapter inherits its surface
option and canvasTexture API without extra implementation.
The styling playground is at /packages/three-canvasui/examples/styling.html.
Both packages exclude demos from npm; the core includes dist and Markdown guides,
and the adapter includes dist, alongside npm's standard metadata/README files.
Guides and reference
The Three.js package owns the implementation and detailed documentation; this adapter exposes the same API and adds enable3d lifecycle integration.
- Complete API reference: all options, widgets, helpers, methods, and types
- Formatting, fonts, color, margin/padding, and every style default
- Text selection, editing, keyboard/clipboard behavior, and scrolling
- Live mesh textures, UV input mapping, material choices, and cleanup
- Demo walkthroughs, shared architecture, and package contents
GitHub is the canonical public source for this package; Gitea is an additional
remote. The npm metadata points to the GitHub repository, and
publishConfig.access marks the scoped package for public publication. The
package has not been published to npm yet. Its enable3d dependency is a peer
dependency; the Three.js core package is installed as a runtime dependency.
This repository contains two publishable npm packages. Build both from the
repository root with npm run build; publish the Three.js core first, then
publish this adapter so the declared core version is available.
npm publish --workspace=@coverslide/three-canvasui --access public
npm publish --access publicAPI
CanvasUi accepts an enable3d Phaser scene, a painter callback, button hit
regions, and optional audio/resize settings. Call update() from the scene's
update loop and resize() after known viewport changes. The scene's
deconstructor automatically disposes the overlay when available; dispose()
can also be called directly.
new CanvasUi(scene, draw, buttons, {
unlockAudio: () => audio.unlock(),
onClickSound: () => audio.play('uiClick'),
onConfirmSound: () => audio.play('uiConfirm'),
watchResize: true,
})Drawing and layout helpers
The painter receives a CanvasRenderingContext2D and remains free to draw
custom HUD artwork. For common UI, the package also exports drawText,
drawPanel, drawButton, drawTextInput, drawScrollArea, inset, row, column, equalRow,
and equalColumn. These helpers work in the same virtual UI coordinate space as
the painter frame and do not impose a component tree or a visual theme.
import { column, drawButton, drawText, inset } from '@coverslide/enable3d-canvasui'
const content = inset({ x: 80, y: 60, width: frame.width - 160, height: 240 }, 16)
const [title, play, options] = column(content, [48, 52, 52], 12)
drawText(frame.ctx, 'PAUSED', title, { align: 'center', font: '700 28px sans-serif' })
drawButton(frame, { id: 'resume', rect: play, label: 'Resume' })
drawButton(frame, { id: 'options', rect: options, label: 'Options' })Canvas-rendered single-line text fields can be registered with textInputs in
the options object. CanvasUi draws no DOM control: a visually hidden native
input bridges keyboard, IME, and mobile soft-keyboard input to the canvas field.
Use drawTextInput in the painter and read field.value or frame.focusedInputId
to render the current value and focus state.
const playerName: UiTextInput = {
id: 'player-name',
label: 'Player name',
rect: { x: 420, y: 310, width: 440, height: 48 },
value: '',
placeholder: 'Enter a name',
maxLength: 24,
onInput: (value) => updateNamePreview(value),
onSubmit: (value) => savePlayerName(value),
}
new CanvasUi(scene, (frame) => {
drawTextInput(frame, playerName)
}, [], { textInputs: [playerName] })Pointer users tap the field to edit it. Keyboard users can call
focusTextInput('player-name'); it accepts an optional character index for the
caret. Pointer presses choose the caret using the painted font and padding.
Drag or Shift-click to select; double-click selects a word and triple-click
selects the full value. Native keyboard selection and clipboard shortcuts also
work. blurTextInput() closes the field. Enter calls onSubmit, Escape
blurs it, and typing is stopped from bubbling into game controls while the field
is active. isOverButton() also returns true for text fields so existing scene
pointer guards treat them as CanvasUi controls.
Scrollable regions are registered with scrollAreas in the same options
object. Their scrollTop is updated by wheel input and pointer/touch dragging;
drawScrollArea clips the painter to the viewport, translates content by the
current offset, and draws a small scrollbar when the content exceeds the view.
Draw content in UI coordinates relative to the viewport's top edge.
const feed: UiScrollArea = {
id: 'feed',
rect: { x: 80, y: 180, width: 360, height: 420 },
contentHeight: 900,
scrollTop: 0,
}
drawScrollArea(frame, feed, (ctx, area) => {
entries.forEach((entry, index) => {
drawText(ctx, entry, {
x: area.rect.x + 12,
y: area.rect.y + index * 48,
width: area.rect.width - 24,
height: 40,
})
})
})