Skip to content

UI Drawing

Updated

A canvas scripts can draw on: paint, minimaps, charts.

Type
ent-ui-drawing
Script class
UIDrawingEntity
Family
UI

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:

  • width and height: the surface in layout pixels (px), 256 × 256 by default.
  • resolution: canvas pixels per layout pixel, 1 to 4. 2 keeps lines sharp up close and in VR, for four times the memory.
  • background: the colour clear() 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.

  1. Add a UI Drawing under a UI Panel or UI Layout. See Scenes and hierarchy.
  2. Set width, height, background, and resolution: 2 if it is seen up close or in VR.
  3. Draw on it from a Behavior with draw((ctx) => { … }).
  4. Add a Signal Emitter if players should draw or tap on it.

Goal: draw a bar chart of team scores, and redraw it when a score changes.

ScoreChart.jx
#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 its background before each redraw.
  • paintChart uses standard Canvas 2D calls; coordinates are layout pixels, so the chart fits the 320 × 180 surface at any resolution.
  • The chart is redrawn only when a score changes, so it costs nothing in between.

Goal: each tap on a map canvas places a numbered marker; a button clears them.

MapMarkers.jx
#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-down carries the tap point in layout pixels.
  • Each tap draws a red disc and its number on top of what is already there. M_TAU is 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.

Goal: let the player sign with a finger, mouse or controller, with separate strokes per pointer.

Signature.jx
#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 pointerId keeps two fingers or two XR controllers from joining their lines.
  • resolution: 2 keeps the strokes smooth up close.

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-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.
UI Drawing comp-ui-drawing 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"

With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.

  • UIDrawing::pointer-down
  • UIDrawing::pointer-move
  • UIDrawing::pointer-up
  • UIDrawing::pointer-hover
  • UIDrawing::pointer-leave

Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.

Run it in the browser, read its scripts, and download it as an XR Creator project.

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.

UIDrawingEntity.jx
#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 });
}