UI Drawing
Updated
A canvas scripts can draw on: paint, minimaps, charts.
- Type
ent-ui-drawing- Script class
UIDrawingEntity- Family
- UI
Overview
Section titled “Overview”A UI Drawing is a blank canvas inside a UI that your scripts paint on: a chart, a minimap, a signature pad, a whiteboard, a picture made at runtime. Its UI Drawing component (comp-ui-drawing) gives scripts the standard Canvas 2D drawing context (the same fillRect, lineTo, arc and fillText calls as a web page canvas) and shows the result in the panel, sharp on screen and in a headset.
A drawing sits nested in a panel or layout, or stands as a world-space root. It keeps what was drawn when its panel rebuilds; changing its size or resolution starts a new, blank canvas. An idle canvas costs nothing per frame.
With a Signal Emitter on its entity, the canvas takes the pointer and reports UIDrawing::pointer-down, pointer-move, pointer-up, pointer-hover and pointer-leave, each with { x, y, pointerId }: the point in layout pixels from the canvas’s top-left corner, and an id that tells two fingers or two controllers apart. Without a Signal Emitter it ignores the pointer.
Use a UI Drawing for anything you compute and draw yourself. For a ready-made picture, use a UI Image; for a live 3D view, a UI Render View.
The props that matter most:
widthandheight: the surface in layout pixels (px), 256 × 256 by default.resolution: canvas pixels per layout pixel, 1 to 4.2keeps lines sharp up close and in VR, for four times the memory.background: the colourclear()fills with ('#ffffff'by default), or'transparent'.borderRadius: rounds and clips the corners; half the width makes a round canvas.cursor:'crosshair'(the default),'pointer'or'default'over the canvas.
From a script, draw(paint) runs paint(ctx) with the 2D context and shows the result on the next frame, and clear() fills it with background. Each draw starts in layout pixels, whatever the resolution.
Set it up
Section titled “Set it up”- Add a UI Drawing under a UI Panel or UI Layout. See Scenes and hierarchy.
- Set
width,height,background, andresolution: 2if it is seen up close or in VR. - Draw on it from a Behavior with
draw((ctx) => { … }). - Add a Signal Emitter if players should draw or tap on it.
Examples
Section titled “Examples”A bar chart
Section titled “A bar chart”Goal: draw a bar chart of team scores, and redraw it when a score changes.
#pragma lifecycle(startup)
// Scene: a UI Drawing named 'Chart'. Other scripts emit 'score:changed' with { team, score }.const W = 320;const H = 180;const scores = { Red: 4, Blue: 7, Green: 2 };const COLORS = { Red: '#ef4444', Blue: '#3b82f6', Green: '#22c55e' };let chart;
function paintChart(ctx) { const teams = Object.keys(scores); const top = Math.max(1, ...Object.values(scores)); const slot = W / teams.length; ctx.font = '14px sans-serif'; ctx.textAlign = 'center'; teams.forEach((team, i) => { const barHeight = (scores[team] / top) * (H - 40); ctx.fillStyle = COLORS[team]; ctx.fillRect(i * slot + 16, H - 24 - barHeight, slot - 32, barHeight); ctx.fillStyle = '#0f172a'; ctx.fillText(`${team} ${scores[team]}`, i * slot + slot / 2, H - 6); });}
function redraw() { chart.clear(); chart.draw(paintChart);}
function startup() { chart = findEntity('Chart').getComponent(UIDrawingComponent); chart.setProps({ width: W, height: H, resolution: 2, background: '#f8fafc', borderRadius: 8 });
on('score:changed', (data) => { scores[data.team] = data.score; redraw(); });
redraw();}How it works:
clear()fills the canvas with itsbackgroundbefore each redraw.paintChartuses standard Canvas 2D calls; coordinates are layout pixels, so the chart fits the 320 × 180 surface at anyresolution.- The chart is redrawn only when a score changes, so it costs nothing in between.
Tap to place markers
Section titled “Tap to place markers”Goal: each tap on a map canvas places a numbered marker; a button clears them.
#pragma lifecycle(startup)
// Scene: a UI Drawing named 'Map' (Signal Emitter: 'map') and a UI Button named 'Clear Map'// (Signal Emitter: 'map:clear').let count = 0;
function startup() { const map = findEntity('Map').getComponent(UIDrawingComponent); map.setProps({ width: 300, height: 300, background: '#e2e8f0', cursor: 'pointer' }); map.clear();
on('map', (event) => { if (event.eventType !== EventType.UIDrawing.PointerDown) return; const { x, y } = event.data; count += 1; map.draw((ctx) => { ctx.fillStyle = '#dc2626'; ctx.beginPath(); ctx.arc(x, y, 10, 0, M_TAU); ctx.fill(); ctx.fillStyle = '#ffffff'; ctx.font = 'bold 12px sans-serif'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText(String(count), x, y); }); emit('map/marker', { x, y, number: count }); });
on('map:clear', (event) => { if (event.eventType !== EventType.UIButton.Press) return; count = 0; map.clear(); });}How it works:
- The Signal Emitter makes the canvas take the pointer;
UIDrawing::pointer-downcarries the tap point in layout pixels. - Each tap draws a red disc and its number on top of what is already there.
M_TAUis a full turn in radians. cursor: 'pointer'shows a hand over the canvas on desktop, hinting that it can be tapped.clear()wipes all markers at once.
A signature pad
Section titled “A signature pad”Goal: let the player sign with a finger, mouse or controller, with separate strokes per pointer.
#pragma lifecycle(startup)
// Scene: a UI Drawing named 'Signature' (Signal Emitter: 'signature').const last = new Map(); // pointerId -> last point
function startup() { const pad = findEntity('Signature').getComponent(UIDrawingComponent); pad.setProps({ width: 360, height: 140, resolution: 2 });
on('signature', (event) => { const { x, y, pointerId } = event.data; if (event.eventType === EventType.UIDrawing.PointerDown) last.set(pointerId, { x, y }); if (event.eventType === EventType.UIDrawing.PointerUp) last.delete(pointerId); if (event.eventType !== EventType.UIDrawing.PointerMove || !last.has(pointerId)) return;
const from = last.get(pointerId); last.set(pointerId, { x, y }); pad.draw((ctx) => { ctx.strokeStyle = '#1e3a8a'; ctx.lineWidth = 3; ctx.lineCap = 'round'; ctx.beginPath(); ctx.moveTo(from.x, from.y); ctx.lineTo(x, y); ctx.stroke(); }); });}How it works:
- A press stores the starting point for that
pointerId; each move draws one short segment from the last point; a release ends the stroke. - Keying by
pointerIdkeeps two fingers or two XR controllers from joining their lines. resolution: 2keeps the strokes smooth up close.
Components
Section titled “Components”What a new UI Drawing is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.
| Component | Type | Role | Per entity | What it does |
|---|---|---|---|---|
| Layer | comp-layer |
Required | One | Puts the entity on a render/raycast layer so cameras and rays can include or skip it. |
| Lock | comp-lock |
Required | One | Editor-only flag that stops the entity from being selected or moved. |
| Enable | comp-enable |
Required | One | Turns the entity and its children on or off: hides them and stops their behavior. |
| Transform | comp- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| UI Drawing | comp- |
Required | One | A canvas scripts can draw on: paint, minimaps, charts. |
You can also add 2 optional components, such as Attribution, Signal Emitter.
The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).
| Prop | Default | Range |
|---|---|---|
width |
256 |
0 ≤ x ≤ 16384 |
height |
256 |
0 ≤ x ≤ 16384 |
background |
"#ffffff" |
Events
Section titled “Events”With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.
UIDrawing::pointer-downUIDrawing::pointer-moveUIDrawing::pointer-upUIDrawing::pointer-hoverUIDrawing::pointer-leave
Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
Every frame a script redraws a round UI Drawing in the corner of the screen with a dot at the player's position on a 40 m map.
Watch the red dot on the minimap follow the amber player around the yard. Drag to orbit the camera, scroll to zoom.
From a script
Section titled “From a script”Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My UI Drawing.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My UI Drawing'); if (!(entity instanceof UIDrawingEntity)) return;
// Its UI Drawing component holds the settings listed under Props. const component = entity.getComponent(UIDrawingComponent); log('width is', component.props.width); component.setProps({ width: 384 });}