A paint canvas
Updated
A UI Drawing takes the pointer and draws a line for each pointer with Canvas 2D calls; a UI Button clears it.
Draw on the white canvas with the mouse or a finger, then press Clear.
A paint canvas
#pragma lifecycle(startup)
const strokes = new Map(); // pointerId -> last point
function startup() { const paint = findEntity('Paint').getComponent(UIDrawingComponent); paint.setProps({ width: 400, height: 300, resolution: 2 });
on('paint', (event) => { const { x, y, pointerId } = event.data; if (event.eventType === EventType.UIDrawing.PointerDown) { strokes.set(pointerId, { x, y }); } else if (event.eventType === EventType.UIDrawing.PointerMove && strokes.has(pointerId)) { const from = strokes.get(pointerId); strokes.set(pointerId, { x, y }); paint.draw((ctx) => { ctx.strokeStyle = '#2563eb'; ctx.lineWidth = 4; ctx.lineCap = 'round'; ctx.beginPath(); ctx.moveTo(from.x, from.y); ctx.lineTo(x, y); ctx.stroke(); }); } else if (event.eventType === EventType.UIDrawing.PointerUp) { strokes.delete(pointerId); } });
on('paint/clear', (event) => { if (event.eventType === EventType.UIButton.Press) paint.clear(); });}How it works
Section titled “How it works”The script is explained step by step in A paint canvas.
Open it in XR Creator
Section titled “Open it in XR Creator”Download the project file below, then import it in XR Hub with Projects › New › Import, as described in Projects and templates. It opens in XR Creator with the same scene you see running above, each script already attached to its Behavior entity. From there you can edit the scripts, adjust components and their props, preview your changes, and deploy the result as a world of your own.
PaintCanvas.xrproj