Confetti in front of the camera
Updated
canvas-confetti, imported from a CDN, draws on a canvas the script made, shown on a plane kept in front of the camera so the burst covers the view.
Click the scene, then press Space to fire confetti from a random spot. Drag to orbit the camera, scroll to zoom.
Confetti in front of the camera
#pragma import(confetti="https://cdn.jsdelivr.net/npm/canvas-confetti@1.9.3/+esm")#pragma lifecycle(startup, update)
const DISTANCE = 1; // metres in front of the camera
let fire = null;let texture = null;let screen = null;
// At most one burst every half second, from a random point along the bottom.const burst = throttle(() => { fire({ particleCount: 150, spread: 80, scalar: 2, origin: { x: Math.random(), y: 0.8 } });}, 500);
function startup() { const canvas = Canvas.create(1280, 720); fire = confetti.create(canvas, { resize: false }); // draw on our canvas, not the page texture = Canvas.texture(canvas); screen = new THREE.Mesh( new THREE.PlaneGeometry(1, 1), new THREE.MeshBasicMaterial({ map: texture, transparent: true, depthTest: false }), ); screen.renderOrder = 1; // drawn over the scene self.add(screen);}
function update() { if (Input.keyboard.isKeyReleased(Key.Space)) burst(); // Keep the canvas in front of the camera, filling its view. camera.getWorldPosition(screen.position); camera.getWorldQuaternion(screen.quaternion); screen.translateZ(-DISTANCE); const height = 2 * DISTANCE * Math.tan(THREE.MathUtils.degToRad(camera.fov / 2)); screen.scale.set(height * camera.aspect, height, 1); texture.needsUpdate = true; // show the canvas's latest frame}How it works
Section titled “How it works”The script is explained step by step in Confetti in front of the camera.
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.
ConfettiBurst.xrproj