Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

docs(comments): trim verbose comments in the UI examples + glyph bearing note

yhh5135695c3766
diff --git a/examples/ui-controls/src/config.ts b/examples/ui-controls/src/config.ts
index 2b5e8293e..deac8c715 100644
--- a/examples/ui-controls/src/config.ts
+++ b/examples/ui-controls/src/config.ts
@@ -1,9 +1,7 @@
import type { Color } from 'esengine';
-// Panel + row geometry. The panel is a fixed box centred on the canvas (the
-// dialog-widget centring trick: 50% inset shifted back by half its size), laid
-// out internally with a flex column. Rows are fixed-height flex items; inside a
-// row the label and the widget slot are placed by absolute insets.
+// Panel + row geometry (a canvas-centred panel laid out as a flex column; each
+// row places its label + widget slot by absolute insets).
export const PANEL_W = 460;
export const PANEL_H = 452;
export const PANEL_PAD = 24;
diff --git a/examples/ui-controls/src/main.ts b/examples/ui-controls/src/main.ts
index bb691a5d9..bad007a5c 100644
--- a/examples/ui-controls/src/main.ts
+++ b/examples/ui-controls/src/main.ts
@@ -1,17 +1,7 @@
-// UI Controls — a gallery of the SDK's imperative UI widget factories, built in
-// code and styled entirely from the design-token theme (no literal RGBA):
-//
-// • Button — increments a click counter
-// • Toggle — pauses / resumes the progress animation
-// • Slider — 0..100 volume, drag the track (wired in systems/controls.ts)
-// • Progress — auto-animating ping-pong bar
-// • Dropdown — re-tints the slider + progress fills (a live theme accent)
-// • Dialog — a modal opened by a button
-//
-// The scene carries only a Camera + Canvas; everything else is spawned by the
-// startup buildSystem via createButton/createSlider/… + themeColors(). This is
-// the modern, ECS-native path — widgets are factory functions that compose the
-// same primitives you'd otherwise author in a scene.
+// UI Controls — a gallery of the imperative widget factories (createButton,
+// createSlider, createToggle, createProgress, createDropdown, createDialog) styled
+// from the design-token theme. The scene is just a Camera + Canvas; buildSystem
+// spawns everything, and controls.ts ticks the slider drag + progress animation.
import { addStartupSystem, addSystemToSchedule, Schedule } from 'esengine';
import { buildSystem } from './systems/build';
diff --git a/examples/ui-controls/src/state.ts b/examples/ui-controls/src/state.ts
index bd72cfe31..9629159fe 100644
--- a/examples/ui-controls/src/state.ts
+++ b/examples/ui-controls/src/state.ts
@@ -1,9 +1,7 @@
import type { Entity, SliderHandle, ProgressHandle } from 'esengine';
-// Handles the build system hands to the per-frame controls system. Widget
-// callbacks (button/toggle/dropdown/dialog) are self-wired at build time and
-// need nothing here; only the slider (pointer drag) and the progress bar
-// (time-driven animation) are ticked each frame.
+// What buildSystem hands to the per-frame controls system (only the slider and
+// progress bar are ticked; the other widgets self-wire their callbacks).
export const state = {
slider: null as SliderHandle | null,
progress: null as ProgressHandle | null,
diff --git a/examples/ui-controls/src/systems/build.ts b/examples/ui-controls/src/systems/build.ts
index 554ac9ed8..ae80f3a08 100644
--- a/examples/ui-controls/src/systems/build.ts
+++ b/examples/ui-controls/src/systems/build.ts
@@ -18,11 +18,7 @@ import {
} from '../config';
import { state } from '../state';
-// One startup system builds the whole screen imperatively from the widget
-// factories (createButton/…/createDialog), styled entirely from the design
-// tokens (themeColors) — no literal RGBA. Each widget wires its own interaction;
-// only the slider (pointer) and progress bar (time) are ticked per-frame by
-// systems/controls.ts.
+// Builds the panel and every widget once, at startup, from the widget factories + theme.
export const buildSystem = defineSystem(
[Query(Canvas), Res(UIEvents), GetWorld()],
(canvases: Iterable<[Entity, unknown]>, events: UIEventQueue, world: World) => {
@@ -34,7 +30,7 @@ export const buildSystem = defineSystem(
const c = themeColors();
- // Centered panel (dialog-centring trick), laid out as a flex column.
+ // Centred panel, laid out as a flex column.
const panel = spawnUIEntity({
world, parent: canvas,
node: {
@@ -62,8 +58,7 @@ export const buildSystem = defineSystem(
align: TextAlign.Left, verticalAlign: TextVerticalAlign.Middle },
});
- // Modal dialog: an overlay on the canvas, hidden until "Open…". Populated
- // with its own title/body/Close children.
+ // Modal dialog overlay (hidden until "Open…"), populated with its own children.
const dialog = createDialog({
world, parent: canvas, startHidden: true,
panelNode: {
@@ -97,7 +92,7 @@ export const buildSystem = defineSystem(
onClick: () => dialog.close(),
});
- // ── Button — increments a click counter shown in the row label. ───────
+ // Button: a click counter in the row label.
{
const { row, label } = makeRow(world, panel, c, 'Clicks: 0');
let clicks = 0;
@@ -110,7 +105,7 @@ export const buildSystem = defineSystem(
});
}
- // ── Toggle — pauses / resumes the progress animation. ─────────────────
+ // Toggle: pause/resume the progress animation.
{
const { row } = makeRow(world, panel, c, 'Animate');
createToggle({
@@ -125,7 +120,7 @@ export const buildSystem = defineSystem(
});
}
- // ── Slider — 0..100 volume, live in the row label; dragged in controls. ─
+ // Slider: 0..100 volume; drag handled in controls.ts.
{
const { row, label } = makeRow(world, panel, c, 'Volume 60%');
state.volumeLabel = label;
@@ -135,8 +130,7 @@ export const buildSystem = defineSystem(
min: 0, max: 100, step: 1, value: 60, handleWidth: 14,
onChange: (v) => setText(world, label, `Volume ${Math.round(v)}%`),
});
- // createSlider ships no input; make the track hit-testable so the
- // controls system can read hover + drag it.
+ // createSlider ships no input — make the track hit-testable for controls.ts.
world.insert(slider.trackEntity, Interactable,
{ enabled: true, blockRaycast: true, raycastTarget: true });
world.insert(slider.trackEntity, UIInteraction,
@@ -144,7 +138,7 @@ export const buildSystem = defineSystem(
state.slider = slider;
}
- // ── Progress — auto-animates (ping-pong) in the controls system. ──────
+ // Progress: auto-animated in controls.ts.
{
const { row } = makeRow(world, panel, c, 'Loading');
state.progress = createProgress({
@@ -155,7 +149,7 @@ export const buildSystem = defineSystem(
});
}
- // ── Dialog opener. ────────────────────────────────────────────────────
+ // Dialog opener.
{
const { row } = makeRow(world, panel, c, 'Modal');
createButton({
@@ -167,8 +161,7 @@ export const buildSystem = defineSystem(
});
}
- // ── Dropdown — re-tints the slider + progress fills. Last row so its
- // popup opens into the space below the panel. ─────────────────────
+ // Dropdown: re-tints the fills. Last row so its popup opens below the panel.
{
const { row } = makeRow(world, panel, c, 'Accent');
createDropdown<Accent>({
@@ -187,13 +180,11 @@ export const buildSystem = defineSystem(
{ name: 'BuildSystem' },
);
-// ── helpers ──────────────────────────────────────────────────────────────────
+// helpers
interface RowRefs { row: Entity; label: Entity; }
-// A row is a fixed-height flex item (its width stretches to the panel content
-// box); inside it the descriptor label sits at the left and the widget slot is
-// pinned to the right by absolute insets.
+// A row: fixed-height flex item; label pinned left, widget slot pinned right.
function makeRow(world: World, panel: Entity, c: ThemeColors, labelText: string): RowRefs {
const row = spawnUIEntity({ world, parent: panel, node: { height: px(ROW_H) } });
const label = spawnUIEntity({
diff --git a/examples/ui-controls/src/systems/controls.ts b/examples/ui-controls/src/systems/controls.ts
index 1785a49bf..7a5437af0 100644
--- a/examples/ui-controls/src/systems/controls.ts
+++ b/examples/ui-controls/src/systems/controls.ts
@@ -7,12 +7,8 @@ import type { TransformData, UIInteractionData } from 'esengine';
import { SLIDER_W, PROGRESS_SPEED } from '../config';
import { state } from '../state';
-// Per-frame wiring for the two widgets the factories don't self-drive:
-// • Slider — createSlider ships no input, so we drag it here. The engine's
-// hit-test already sets UIInteraction.hovered on the track; we only map the
-// pointer's world x (same space as the track's laid-out worldPosition) back
-// to a 0..100 value.
-// • Progress — a simple time-driven ping-pong, pausable via the toggle.
+// Per-frame wiring the factories don't self-drive: drag the slider (mapping the
+// pointer's world x onto the track) and animate the progress bar (pausable).
export const controlsSystem = defineSystem(
[Res(Time), Res(Input), Res(UICameraInfo), GetWorld()],
(time, input, camera, world) => {
diff --git a/examples/ui-interaction/src/systems/dragFocus.ts b/examples/ui-interaction/src/systems/dragFocus.ts
index a6c0ec475..63a557bac 100644
--- a/examples/ui-interaction/src/systems/dragFocus.ts
+++ b/examples/ui-interaction/src/systems/dragFocus.ts
@@ -15,15 +15,10 @@ interface CardNode {
marginTop: Dimension;
}
-// Six draggable cards rest in a row. Drag one into the drop zone to file it
-// there (the zone lists what it holds); drop it back over the row to snap into
-// the nearest slot, swapping whoever sat there. Focused cards brighten.
-//
-// The engine drives the actual dragging (Draggable + DragPlugin nudges the
-// card's absolute UINode insets) and focus (Focusable); this system owns the
-// resting layout and the drop/snap logic. Everything is in the canvas's y-down
-// pixel-inset space (design resolution 800×600), the same space DragPlugin
-// writes while a card is held.
+// Cards rest in a row; drag one into the drop zone to file it, or back onto the
+// row to snap into the nearest slot (swapping the occupant). Focused cards brighten.
+// The engine owns dragging (Draggable) + focus; this system owns the resting layout
+// and drop/snap, working in the canvas's y-down pixel-inset space (the DragPlugin's).
const CARD_COUNT = 6;
const CANVAS_W = 800;
const CARD_W = 100;
diff --git a/examples/ui-layout/src/main.ts b/examples/ui-layout/src/main.ts
index 0a4d70a83..f32edf0d7 100644
--- a/examples/ui-layout/src/main.ts
+++ b/examples/ui-layout/src/main.ts
@@ -1,15 +1,6 @@
-// UI Layout — a live flexbox playground. The scene carries only a Camera +
-// Canvas; a startup system builds a real FlexContainer full of item boxes plus
-// a row of cycle-buttons that drive its properties:
-//
-// • Dir — flex-direction (row / column / *-reverse)
-// • Justify — justify-content (start / center / end / space-*)
-// • Align — align-items (start / center / end / stretch)
-// • Wrap — flex-wrap (nowrap / wrap)
-// • Items — how many boxes to lay out
-//
-// Click a button and the Yoga layout pass re-flows the boxes next frame. Built
-// imperatively from the widget factories + design tokens, matching ui-controls.
+// UI Layout — a live flexbox playground. buildSystem spawns a real FlexContainer of
+// item boxes plus cycle-buttons that drive its flex-direction / justify-content /
+// align-items / flex-wrap / item-count; the Yoga pass re-flows them each frame.
import { addStartupSystem } from 'esengine';
import { buildSystem } from './systems/build';
diff --git a/examples/ui-layout/src/state.ts b/examples/ui-layout/src/state.ts
index acd1b045d..f02420532 100644
--- a/examples/ui-layout/src/state.ts
+++ b/examples/ui-layout/src/state.ts
@@ -1,9 +1,7 @@
import type { Entity } from 'esengine';
-// The live playground state: the demo FlexContainer, its item boxes, the
-// readout label, and the current option index for each control. The control
-// buttons advance an index and re-apply; the Yoga layout pass re-flows the
-// items next frame.
+// Live playground state: the demo FlexContainer, its item boxes, the readout, and
+// the current option index per control (a button click advances its index + re-applies).
export const state = {
container: 0 as Entity,
readout: 0 as Entity,
diff --git a/examples/ui-layout/src/systems/build.ts b/examples/ui-layout/src/systems/build.ts
index 59d2525c2..574941bd6 100644
--- a/examples/ui-layout/src/systems/build.ts
+++ b/examples/ui-layout/src/systems/build.ts
@@ -16,11 +16,7 @@ import {
} from '../config';
import { state } from '../state';
-// A live flexbox playground: control buttons cycle a real FlexContainer's
-// flex-direction / justify-content / align-items / flex-wrap / item-count, and
-// the Yoga layout pass re-flows the item boxes next frame. Built imperatively
-// from the widget factories + design tokens — the same modern path as
-// ui-controls.
+// Builds the demo FlexContainer, item boxes, and the cycle-buttons that drive it.
export const buildSystem = defineSystem(
[Query(Canvas), Res(UIEvents), GetWorld()],
(canvases: Iterable<[Entity, unknown]>, events: UIEventQueue, world: World) => {
@@ -72,7 +68,7 @@ export const buildSystem = defineSystem(
text: { content: '', fontSize: 13, color: { r: 0.55, g: 0.55, b: 0.58, a: 1 } },
});
- // ── inner helpers (close over world / c / events / state) ─────────────
+ // inner helpers (close over world / c / events / state)
function spawnItem(i: number): Entity {
const box = spawnUIEntity({
@@ -143,7 +139,7 @@ export const buildSystem = defineSystem(
{ name: 'BuildSystem' },
);
-// ── shared helpers ─────────────────────────────────────────────────────────
+// shared helpers
interface TextInit {
content: string;
diff --git a/sdk/src/ui/text/glyph-rasterizer.ts b/sdk/src/ui/text/glyph-rasterizer.ts
index 2a08d0e08..a2d1a93f4 100644
--- a/sdk/src/ui/text/glyph-rasterizer.ts
+++ b/sdk/src/ui/text/glyph-rasterizer.ts
@@ -120,14 +120,10 @@ export class CanvasGlyphRasterizer implements GlyphRasterizer {
width: w,
height: h,
advance,
- // The glyph is drawn at pen x = pad + left, so its ink left edge lands
- // at tile x = pad; mapping the pen origin back to the tile's left edge
- // (tile x = 0) is therefore -(left + pad). (The earlier `left - pad`
- // mirrored the left bearing, shifting each glyph by 2·left — visible as
- // uneven spacing / overlap, worst at small sizes where the browser
- // rounds actualBoundingBoxLeft to whole pixels.)
- bearingX: -(left + pad), // pen origin → bitmap left edge
- bearingY: ascent + pad, // bitmap top above the baseline
+ // Drawn at pen x = pad + left (ink lands at tile x = pad), so the pen
+ // origin maps back to tile x = 0 at -(left + pad).
+ bearingX: -(left + pad),
+ bearingY: ascent + pad,
};
}
}