VOOZH about

URL: https://blog.logrocket.com/getting-started-babylon-js/

⇱ Getting started with Babylon.js - LogRocket Blog


2022-06-24
1400
#vue
Muyiwa Femi-Ige
115938
πŸ‘ Image

See how LogRocket's Galileo AI surfaces the most severe issues for you

No signup required

Check it out

Building 3D graphics in a web browser has never been more straightforward. Join me on this journey as we show you how to create a basic scene using Babylon.js.

πŸ‘ Getting Started Babylonjs

πŸš€ Sign up for The Replay newsletter

The Replay is a weekly newsletter for dev and engineering leaders.

Delivered once a week, it's your curated guide to the most important conversations around frontend dev, emerging AI tools, and the state of modern software.

Aim

This article aims to guide you on how to:

  1. Create a Vue component
  2. Create a Babylon class
  3. Render a scene on a canvas
  4. Create a 3D mesh

Prerequisites

This article is a beginner-friendly tutorial as little hassle as possible. I would, however, suggest that you have a strong understanding of JavaScript to do this.

Installations

Installing Vue

To begin, we will need Vue3 in our workspace. To do so, we type in the command below in our terminal:

npm install -g @vue/cli

(Note: you are required to have Node.js installed as you will need the node package manager as we move forward)

Within the terminal, we use the command below to create a new project with the folder name bb101:

vue create bb101

After creating the project folder, we are prompted with a few choices to pick from. Do the following:

  1. First, we want to select the features we want manually, so we start by selecting that
  2. Then, we add TypeScript to the already present list of features we want
  3. Next, we select 3x (Vue 3) as the version of Vue.js to use in the project
  4. We select No as the response for the following two options by typing β€œn”
  5. We will go with the β€œESLint with error prevention only” and β€œlint on save” as our additional lint feature for our linter
  6. To place the config for ESLint, we select the option β€œin dedicated config file” and select No for saving the preset for future projects

Now, we wait for a few moments to have these processes installed. Next, we change the directory in our terminal to that of the project we are working on using the command cd bb101, and we use npm run serve to run our Vue application. Once it compiles, we will have a localhost server to open in our browser.

πŸ‘ Localhost Server Compiled Example

Installing Babylon.js

We need to install the Babylon package into our project. We will utilize several Babylon packages during this project, but for now, let’s start with the core package of Babylon. To do this, we use the following command in our terminal:

npm install @babylonjs/core

The command above will install babylon.js into the node module folder of our project. After the installation, we can proceed to the next step.

Getting started

Creating the Vue component (BabylonOne.vue)

We begin by modifying the default helloworld.vue file in the component folder. We want to reuse the component but with the name BabylonOne rather than HelloWorld.

In our newly named BabylonOne.vue file, we will clear out the default content in our HTML section (everything within the starting and closing div) and make a little change to the both the HTML section and script tag’s section by replacing them with the following:

<template>
<div>
 <h3>BabylonOne</h3>
 <canvas></canvas>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
name: 'BabylonOne',
mounted(){ //lifecycle hook
const canvas = document.querySelector("canvas")
 }
});
</script>

Changing the initial file name will trigger a warning as HelloWorld was the name used in the App.vue file. In the App.vue file, we will clear the content in the template tag and change the previous component name to BabylonOne (do this in every line HelloWorld is in). After we run the App in our terminal using npm run serve and within our browser, the result should be as below:

πŸ‘ Vue Component Result

Creating the Babylon class

In this section, we want to create a TypeScript class for Babylon. To do this, we will create a subfolder in the src folder called BabylonOne.

In this folder, we will create a new TypeScript file called BabylonScene. Within this file, we will import Scene and Engine from our Babylon core package, and we will create a class called BabylonScene.

Within this class, we will create a scene and engine variable and a constructor that we automatically call when creating an instance of the class. We need the constructor to grab the canvas element created in our Vue component.

In our scene variable, we specify the type as Scene and engine variable of the type as Engine. Next, we add the engine variable to our constructor and set anti-aliasing to True.

We will create a separate method outside the constructor and assign it to the scene variable for the Scene variable. Finally, we want to render our scene simultaneously as the engine runs. Thus, we will use runRenderLoop to do this. Below is an implementation of the above:

import { Scene, Engine } from "@babylonjs/core"
export class BabylonScene {
 scene: Scene;
 engine: Engine;
 constructor(private canvas: HTMLCanvasElement) {
 this.engine = new Engine(this.canvas, true);
 this.scene = this.CreateScene();
 this.engine.runRenderLoop(() => {
 this.scene.render();
 });
 }
 CreateScene(): Scene {
 const scene = new Scene(this.engine);
 return scene;
 }
}

Rendering the scene in Vue

To do this, we head back to the BabylonOne file and import the BabylonScene class from BabylonScene.ts file. Within the mounted, we call the BabylonScene class with the parameter β€œcanvas”. Here is how the script tag looks at the moment:

<script lang="ts">
import { defineComponent } from 'vue';
import { BabylonScene } from '@/BabylonOne/BabylonScene';
export default defineComponent({
 name: 'BabylonOne',
 mounted(){ //lifecycle hook
 const canvas = document.querySelector("canvas")!;
 new BabylonScene(canvas)
 }
});
</script>

(Note: Add a single exclamation behind the closing bracket in the canvas variable)

Once completed, we will test it in our terminal, and our result is as below:

πŸ‘ Terminal Test Scene

Modifying the CSS and adding a camera and hemispheric lighting

We want the canvas size to be about 70–80 percent of the screen size. We want to create a canvas CSS with a width and height of 70 percent. Once we implement the above, our canvas will be as below:

Now, we want to see things in our canvas β€” thus, we will add a camera, a light, and some 3D objects (a ground and a sphere ball). To do this, we add the code below to our CreateScene Method in the BabylonScene.ts file:

const Camera = new FreeCamera("camera", new Vector3(0,1,-5), this.scene);
Camera.attachControl();
const light = new HemisphericLight("light", new Vector3(0,1,0), this.scene);
light.intensity = 0.5;
//3D Object
const ground = MeshBuilder.CreateGround("ground", {width: 10, height:10}, this.scene);
const sphereball = MeshBuilder.CreateSphere("sphereball", {diameter:1}, this.scene);
sphereball.position = new Vector3(0,1,0)

Explaining the code snippet

When creating a camera variable, we assign its value as FreeCamera and define its name, starting position, and scene as camera, new Vector3(0,1,-5), this.scene, respectively. To control the camera with our mouse, we use the attachControl method.


Over 200k developers use LogRocket to create better digital experiences

πŸ‘ Image
Learn more β†’

For the camera to work, we need to add light to see the object in our environment. To achieve this, we will create a light variable and assign its value as HemisphericLight. We will add a name, starting position, and scene similar to the camera variable.

Finally, we will add the intensity to adjust the brightness of the HemisphericLight (note that by default, the intensity is set to one, making the environment too bright).

For the 3D object, we will create a ground and a sphere to represent a 3D object in our environment. To create a ground, we create a ground variable and assign the value MeshBuilder.CreateGround while setting the name, width and height, and scene.

We also use the Meshbuilder method to create a spherical ball while setting the name, diameter, and scene. To modify the ball’s position, we will use the position method and assign it to a starting position.

After implementing the code above, we should have the result as below:

πŸ‘ Babylon 3D Mesh Example

Conclusion

Babylon.js is a versatile 3D JavaScript library capable of doing anything imaginable with 3D.

Babylon.js is a perfect 3D library with its only significant drawback being its package size.
In this tutorial, we have shown you how to create a Vue component, a Babylon class, render a scene on a canvas and create a 3D mesh

Thanks for reading and happy coding!

LogRocket understands everything users do in your Vue apps.

Debugging Vue.js applications can be difficult, especially when users experience issues that are difficult to reproduce. If you’re interested in monitoring and tracking Vue mutations and actions for all of your users in production, try LogRocket.

πŸ‘ LogRocket Dashboard Free Trial Banner

LogRocket lets you replay user sessions, eliminating guesswork by showing exactly what users experienced. It captures console logs, errors, network requests, and pixel-perfect DOM recordings β€” compatible with all frameworks.

With Galileo AI, you can instantly identify and explain user struggles with automated monitoring of your entire product experience.

Modernize how you debug your Vue apps β€” start monitoring for free.

πŸ‘ Image
πŸ‘ Image
πŸ‘ Image

Stop guessing about your digital experience with LogRocket

Get started for free

Recent posts:

Debug Next.js apps with AI agents and next-browser

Learn how next-browser gives AI agents runtime context for debugging Next.js apps, including React props, hydration, PPR, forms, and performance.

πŸ‘ Image
Emmanuel John
Jun 17, 2026 β‹… 9 min read

Stop hardcoding LLM SDKs: Dynamic LLM routing with OpenRouter and Next.js

Build dynamic LLM routing in Next.js with OpenRouter, TanStack AI, task classification, model fallbacks, and cost-aware routing.

πŸ‘ Image
Chizaram Ken
Jun 16, 2026 β‹… 13 min read

What is TSRX?: What JSX would look like if it were designed today

TSRX adds first-class control flow, conditional hooks, and scoped styles to React via a TypeScript compiler extension β€” no new framework required.

πŸ‘ Image
Ikeh Akinyemi
Jun 12, 2026 β‹… 6 min read

How to add authentication to a React Native app with Better Auth

Learn how to build a full React Native auth system using Better Auth and Expo β€” with email/password login, Google OAuth, session persistence, and protected routes.

πŸ‘ Image
Chinwike Maduabuchi
Jun 9, 2026 β‹… 13 min read
View all posts

Hey there, want to help make our blog better?

Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.

Sign up now