@coverslide/three-canvasui
v0.2.0
Published
Canvas-textured HUD and menu overlays for Three.js
Readme
@coverslide/three-canvasui
Three.js canvas HUD and menu overlay. ThreeCanvasUi renders a painter callback
to an offscreen 2D canvas, uploads it as a texture, and places that texture on a
plane attached to the supplied camera. The package also exports canvas drawing
and row/column layout helpers.
Guides and complete reference
| Guide | Covers | | --- | --- | | API reference | Every constructor option, method, property, widget model, painter field, helper, and exported type. | | Styling and layout | Every style property/default; fonts, colors, borders, alignment, margin, padding, gaps, and custom controls. | | Input and selection | Drag/Shift-click selection, double/triple click, keyboard/clipboard editing, focus, and scrolling. | | Live mesh textures | Texture mode, UV raycasting, occlusion, sharing, materials, resize, and cleanup. | | Demo walkthroughs | Overlay, mesh, and styling examples; run commands and troubleshooting. |
Existing widget defaults are preserved. Customize individual draw calls with optional style objects; no global theme is changed. Markdown guides ship with the core package; demo source remains in the repository.
Quick start
This package is maintained in the same GitHub repository as the enable3d
adapter, in the packages/three-canvasui workspace.
import { ThreeCanvasUi } from '@coverslide/three-canvasui'
// Camera children (including the UI plane) must be part of the rendered scene.
scene.add(camera)
const ui = new ThreeCanvasUi({ renderer, camera }, draw, buttons, {
watchResize: true,
onConfirmSound: () => playUiConfirm(),
})Call update() from the render loop, resize() after known viewport changes,
and dispose() when the owning scene is removed. three and @types/three are
peer dependencies so the overlay shares the host application's Three.js types
and runtime instance.
Register editable single-line fields with CanvasUiOptions.textInputs and draw
them with drawTextInput. The fields are painted on the overlay canvas; an
invisible native input supplies keyboard, IME, and mobile soft-keyboard support.
Use focusTextInput(id) and blurTextInput() for programmatic focus.
Drag and Shift-click select text; double-click selects a word, triple-click
selects the full value, and native keyboard/clipboard shortcuts edit the selection.
Scrollable regions are registered with CanvasUiOptions.scrollAreas and drawn
with drawScrollArea; the helper clips to the viewport and applies the current
vertical offset. Wheel input and pointer dragging update each area's scrollTop.
From this workspace, run npm run demo (or run it from the repository root),
then open http://127.0.0.1:4173/packages/three-canvasui/examples/index.html.
The demo combines text entry and scrolling with a spinning torus and an orbiting
camera, using translucent UI panels to keep the 3D scene visible. It shares its
scene and painter with the root adapter demo. Demo files are excluded from npm
packages by the files: ["dist", "docs/*.md"] allowlist.
For an interactive UI on a world-space mesh, open
http://127.0.0.1:4173/packages/three-canvasui/examples/mesh.html, or use the
"Live mesh texture" button in either overlay demo. A screen and a
spinning cube share one live texture, including a clock, editable name, button
that changes a torus's color, and scrollable content. All interaction uses
raycast UVs, and normal mesh depth testing controls visibility.
For fonts, colors, margin/padding, and alignment, open
http://127.0.0.1:4173/packages/three-canvasui/examples/styling.html or use the
"Styling playground" button. It includes unchanged default widgets alongside
the customized preview.
Pass surface to render a fixed-size texture instead of attaching an overlay
to the camera:
const ui = new ThreeCanvasUi({ renderer, camera }, draw, buttons, {
surface: {
width: 1024,
height: 640,
mapPointer(clientX, clientY) {
// Raycast from the camera using client coordinates; reject misses and occlusion.
const hit = intersectScreen(clientX, clientY)
return hit?.uv ? { x: hit.uv.x * 1024, y: (1 - hit.uv.y) * 640 } : null
},
},
textInputs,
scrollAreas,
})
screenMaterial.map = ui.canvasTexture
screenMaterial.needsUpdate = true
// Share ui.canvasTexture with other materials to mirror the same UI.The complete raycasting implementation is in examples/mesh.js. This mapping
assumes the default texture UV transform; account for texture offset, repeat,
or rotation if you customize those. null means the pointer missed the UI.
Painter coordinates use the surface dimensions. Call invalidate() after data
changes; it repaints and flags the texture for upload. update() does not
repaint automatically. The texture stays stable through viewport resizes in
surface mode and is owned and disposed by the UI. Dispose the UI when removing
its screen and stop using its texture in other materials at that time.
