Flock XR is a creative coding platform for 3D development using Blockly and Babylon.js. This document describes the JavaScript API that's available when writing code in Flock.
Initializes the Flock environment and sets up the 3D scene.
Example:
await initialize();Creates a new Babylon.js rendering engine.
Creates a new 3D scene with default lighting and camera setup.
Plays an animation on a specified mesh.
Parameters:
meshName(string): Name of the mesh to animateoptions(object): Animation configurationanimationName(string): Name of the animation to playloop(boolean, default: false): Whether to loop the animationrestart(boolean, default: true): Whether to restart if already playing
Example:
await playAnimation("character1", {
animationName: "walk",
loop: true
});Switches to a different animation on a mesh.
Parameters:
meshName(string): Name of the meshoptions(object): Animation optionsanimationName(string): Name of the animation to switch toloop(boolean, default: true): Whether to looprestart(boolean, default: false): Whether to restart
Example:
await switchAnimation("character1", {
animationName: "run"
});Animates a property using keyframes.
Parameters:
meshName(string): Name of the mesh to animateoptions(object): Animation configurationkeyframes(array): Array of keyframe objects withdurationandvalueproperty(string): Property to animate (e.g., "color", "alpha", "position")easing(string, default: "Linear"): Easing functionloop(boolean, default: false): Whether to loopreverse(boolean, default: false): Whether to reverse
Example:
await animateKeyFrames("box1", {
keyframes: [
{ duration: 0, value: "#FF0000" },
{ duration: 2, value: "#00FF00" }
],
property: "color",
loop: true
});Smoothly moves a mesh to a new position.
Parameters:
meshName(string): Name of the mesh to moveoptions(object): Movement configurationx,y,z(number, default: 0): Target position coordinatesduration(number, default: 1): Duration in secondseasing(string, default: "Linear"): Easing functionreverse(boolean, default: false): Whether to reverseloop(boolean, default: false): Whether to loop
Example:
await glideTo("player", {
x: 5, y: 0, z: 3,
duration: 2,
easing: "SineEase"
});Rotates a mesh with animation.
Parameters:
meshName(string): Name of the mesh to rotateoptions(object): Rotation configurationx,y,z(number, default: 0): Rotation angles in degreesduration(number, default: 1): Duration in secondseasing(string, default: "Linear"): Easing functionreverse(boolean, default: false): Whether to reverseloop(boolean, default: false): Whether to loop
Example:
await rotateAnim("box1", {
x: 90, y: 180, z: 0,
duration: 1.5
});Animates a specific property of a mesh.
Parameters:
meshName(string): Name of the meshoptions(object): Animation configurationproperty(string): Property to animatetargetValue(any): Target value for the propertyduration(number, default: 1): Duration in secondsreverse(boolean, default: false): Whether to reverseloop(boolean, default: false): Whether to loopmode(string, default: "AWAIT"): Animation mode
Plays a sound effect or music.
Parameters:
soundName(string): Name of the sound fileoptions(object): Sound configurationvolume(number, 0-1): Volume levelloop(boolean): Whether to loop the soundspatial(boolean): Whether to use 3D spatial audio
Stops all currently playing sounds.
Creates a character model in the scene.
Parameters:
options(object): Character configurationmodelName(string): Name of the character model filemodelId(string): Unique identifier for the characterscale(number, default: 1): Scale factorposition(object): World position {x, y, z}colors(object): Color configuration for different parts
Example:
const player = createCharacter({
modelName: 'Character2.glb',
modelId: 'player_unique_id',
scale: 1,
position: { x: 0, y: 0, z: 0 },
colors: {
hair: "#ffcc00",
skin: "#f0d5b1",
eyes: "#33cc00"
}
});Creates an object in the scene.
Loads and creates a custom 3D model.
Creates a box geometry.
Parameters:
name(string): Unique identifiercolor(string): Hex color valuewidth,height,depth(number): Dimensionsposition(array): Position [x, y, z]
Example:
const box1 = createBox("myBox", "#ff0000", 2, 2, 2, [0, 1, 0]);Creates a sphere geometry.
Creates a cylinder geometry.
Creates a capsule geometry.
Creates a plane geometry.
Sets the transparency of a mesh.
Parameters:
meshName(string): Name of the meshalpha(number, 0-1): Transparency value (0 = transparent, 1 = opaque)
Example:
await setAlpha("box1", 0.75);Changes the color of a mesh.
Adds a highlight effect to a mesh.
Adds a glow effect to a mesh.
Applies a color tint to a mesh.
Removes all visual effects from a mesh.
Moves a mesh to a specific position instantly.
Moves a mesh forward by a specified distance.
Parameters:
meshName(string): Name of the mesh to movedistance(number): Distance to move forward
Rotates a mesh instantly.
Parameters:
meshName(string): Name of the meshx,y,z(number): Rotation values
Rotates a mesh to a specific 3D rotation instantly.
Scales a mesh.
Applies physics properties to a mesh.
Parameters:
meshName(string): Name of the meshphysicsType(string): Type of physics ("DYNAMIC", "STATIC", "KINEMATIC")
Example:
await setPhysics("player", "DYNAMIC");Applies a force to a physics-enabled mesh.
Creates a ground plane.
Parameters:
color(string): Hex color for the groundname(string): Name identifier for the ground
Example:
createGround("#ffffff", "ground");Sets the skybox/environment color.
Parameters:
color(string): Hex color for the sky
Example:
setSky("#ffffff");Adjusts the scene lighting intensity.
Adds fog to the scene for atmospheric effects.
Parameters:
fogColorHex(string): Fog color as a hex string.fogMode(string): Fog mode ("NONE","LINEAR","EXP","EXP2").fogDensity(number, optional): Fog density. Defaults to0.1.fogStart(number, optional): Distance from the camera where fog begins. Defaults to50.fogEnd(number, optional): Distance from the camera where fog ends. Defaults to100.
Example:
setFog({ fogColorHex: "#ffffff", fogMode: "LINEAR", fogDensity: 0.1, fogStart: 50, fogEnd: 100 });Returns the active camera object.
Example:
const camera = getCamera();Enables/disables camera controls.
Attaches the camera to follow a specific mesh.
Parameters:
meshName(string): Name of the mesh to followdistance(number): Distance from the mesh
Example:
await attachCamera("player", 7);Displays text in the UI.
Parameters:
text(string): Text to displaysize(number): Font sizecolor(string): Hex color
Example:
printText('π Hello', 30, "#000080");Creates button controls for user interaction.
Parameters:
type(string): Type of controls (e.g., "ARROWS")enabled(boolean): Whether controls are enabledcolor(string): Hex color for the buttons
Example:
buttonControls("ARROWS", true, "#cc33cc");Displays speech bubble text above a mesh.
Creates UI text elements.
Creates interactive UI buttons.
Pauses execution for a specified time.
Parameters:
duration(number): Wait time in seconds
Example:
await wait(0.1);Returns a random integer between min and max.
Example:
const random = randomInteger(1, 5);Returns a random hex color.
Example:
const color = randomColour();Checks if a specific key is currently pressed.
Parameters:
key(string): Key to check (e.g., "w", "a", "s", "d")
Example:
if (keyPressed("w")) {
// Move forward
}Checks if a movement or action input is active across keyboard, touch, or XR controllers using a platform-neutral action name.
Parameters:
action(string): One ofFORWARD,BACKWARD,LEFT,RIGHT,BUTTON1,BUTTON2,BUTTON3, orBUTTON4.
Example:
if (actionPressed("FORWARD")) {
// Move forward regardless of whether the player is using W, Z, touch, or XR input
}Run a callback when the chosen action is pressed or released across keyboard, touch, or XR sources.
Parameters:
action(string): One ofFORWARD,BACKWARD,LEFT,RIGHT,BUTTON1,BUTTON2,BUTTON3, orBUTTON4.callback(function): Function to run when the action triggers.isReleased(boolean, optional): Set totrueto fire on release instead of press.
Example:
whenActionEvent("BUTTON1", async () => {
// Respond to the action button, regardless of whether it came from E, touch, or XR
});Gets a property value from a mesh.
Runs a function in an infinite loop.
Parameters:
callback(function): Function to execute repeatedly
Example:
forever(async () => {
if (keyPressed("w")) {
moveForward("player", 3);
await switchAnimation("player", { animationName: "Walk" });
} else {
await switchAnimation("player", { animationName: "Idle" });
}
});Registers an event handler.
Broadcasts a custom event.
Sets up collision/trigger detection for a mesh.
For a complete working example, see example.html in the repository, which demonstrates a full Flock XR application with character movement, physics, and camera controls.
setSky("#ffffff");
createGround("#ffffff", "ground");
printText('π Hello', 30, "#000080");
buttonControls("ARROWS", true, "#cc33cc");
const player = createCharacter({
modelName: 'Character2.glb',
modelId: 'player_unique_id',
scale: 1,
position: { x: 0, y: 0, z: 0 }
});
await setPhysics(player, "DYNAMIC");
await attachCamera(player, 7);forever(async () => {
if (keyPressed("w")) {
moveForward("player", 3);
await switchAnimation("player", { animationName: "Walk" });
} else if (keyPressed("s")) {
moveForward("player", -3);
await switchAnimation("player", { animationName: "Walk" });
} else {
await switchAnimation("player", { animationName: "Idle" });
}
});// Keyframe animation
await animateKeyFrames("box1", {
keyframes: [
{ duration: 0, value: "#FF0000" },
{ duration: 2, value: "#00FF00" },
{ duration: 4, value: "#0000FF" }
],
property: "color",
loop: true
});
// Smooth movement
await glideTo("player", {
x: 10, y: 0, z: 5,
duration: 3,
easing: "SineEase"
});"Character1","Character2","Character3","Character4""Cat","Monkey","Person"
"DYNAMIC"- Object affected by forces and gravity"STATIC"- Fixed object that doesn't move"KINEMATIC"- Object that can be moved but isn't affected by forces
"Linear","SineEase","CubicEase","QuadraticEase""ExponentialEase","BounceEase","ElasticEase","BackEase"
- Movement keys:
"w","a","s","d" - Special keys:
" "(space),"ANY","NONE"
Most Flock functions are asynchronous and should be awaited. If a mesh or resource is not found, functions will typically fail silently or log warnings to the console.
See CONTRIBUTING.md for guidelines on contributing to Flock XR.