Getting Started – A-Frame VR Development Part 1
Tags: virtualreality, aframe, javascriptvr, opensourcevr
Published: 2018-10-26 17:03:35

---
title: Getting Started – A-Frame VR Development Part 1
published: true
tags: Virtual Reality,A-Frame,javascript vr,open source vr
canonical_url: https://hasanali.me/getting-started-a-frame-vr-development-part-1/
---

 

![](https://hasanali.me/wp-content/uploads/2018/10/Copy-of-Rufus-Strikes-Wins1.png)

This is the first part of A-Frame Virtual Reality Development series, in this part I will explain the basic components and syntax of the A-Frame framework and will create a simple Hello World Virtual Reality project using A-Frame.

Before getting started, if you don’t know basic web development technologies and never worked in the web then please first learn some HTML, CSS, and Javascript basic skills, then follow this tutorial.

Throughout the tutorial, I will be sharing the code with pictures step by step so you can understand easily. So lets get started.

Create an _index.html_ file and write basic HTML DOCTYPE syntax in it.

![](https://hasanali.me/wp-content/uploads/2018/10/01-1024x586.png)

Insert the A-Frame framework script inside `\<head\>\</head\>` container

```
\<script src="https://aframe.io/releases/0.8.0/aframe.min.js"\>\</script\>
```

`Note : You can also install A-Frame using NPM. Use `$ npm install aframe` command.`

Now let’s explore the A-Frame syntax. A-Frame is based on custom created HTML elements. As you can is in the picture below every object is represented by a custom HTML element.

![](https://hasanali.me/wp-content/uploads/2018/10/02-1024x579.jpg)

## Creating a Scene

First, we have to create a scene, which will configure the basic components for us like canvas, rendered, camera, lights etc. It’s a container where all the elements of VR environment will come inside it.

Add the `_\<a-scene\>\</a-scene\>_` tag inside the body to initialize the scene.

![](https://hasanali.me/wp-content/uploads/2018/10/03-1024x586.png)

If you open _the index.html_ file in your browser you will notice that after initializing the scene a small VR icon can be seen on the page.

##  Adding a Cube/Box

Let’s insert a 3d box in it.

Add `_\<a-box color=”red”\>\</a-box\>_` ` inside the `_\<a-scene\>_` tag.

Now if you refresh _index.html_ you’ll see nothing. But if your press S key on your keyboard, the camera will start moving backward and you’ll see a red box. The reason that you didn’t see the box in the first place is that the position of the camera and the box is same, both are placed at the same point (origin [0,0,0]) in the screen.

![](https://hasanali.me/wp-content/uploads/2018/10/04-1024x586.png)

Now before adding more elements let me tell you what are Primitives and Entities in A-Frame.

`_\<a-scene\>_`,` _\<a-box\>_`,` _\<a-sphere\>_` etc all are pre-defined custom HTML tags by A-Frame, which are easy to use tags and are called Primitives.

On the hand `\<a-entity\>` can by any element. It can be a box, a scene, a sphere, a light or camera. All the `_\<a-scene\>_`,` _\<a-box\>`_,` _\<a-cube\>_` etc are the pre-defined entities for ease of use. However, if you want custom shapes and advanced features like sound in your VR environment `\<a-entity\>` will help you there.

For example underneath a `_\<a-box\>_` tag there is an `_\<a-entity\>_` tag with pre-defined position and geometry :

_\<a-entity id=”box” geometry=”primitive: box” material=”color: red”\>\</a-entity\>_

I hope you got the different between the Primitives and Entities. Don’t worry if you’re still confused, you’ll understand it much better once we’ll use \<a-entity\> tags.

##  Changing Properties

Changing the properties of Primitives is also very easy. Lets put our box in front of our camera.

Add a position attribute in the `a-box` tag. Position attribute specifies the position on X, Y and Z axis of the element.

_\<a-box position=”0 2 -5″ color=”red”\>\</a-box\>_

Add a rotation attribute in the `a-box` tag. Rotation attribute specifies the rotation on X, Y and Z axis of the element.

_\<a-box position=”0 2 -5″ rotation=”0 45 45″ color=”red”\>\</a-box\>_

And add scale attribute to scale the element on X, Y and Z axis.

_\<a-box position=”0 2 -5″ rotation=”0 45 45″ scale=”2 2 2″ color=”red”\>\</a-box\>_

Now your box will look like this.

![](https://hasanali.me/wp-content/uploads/2018/10/05-1024x586.png)

Play around with the properties of the box all are listed here: [https://aframe.io/docs/0.8.0/primitives/a-box.html#attributes](https://aframe.io/docs/0.8.0/primitives/a-box.html#attributes)

## Adding background / Skybox

Skybox is the background of the 3D world. `_\<a-sky\>\</a-sky\>_` is used to put background in the scene.

_\<a-sky color=”#94c5d6″\>\</a-sky\>_

Add the `_\<a-sky\>_` tag inside the `_\<a-scene\>_` container. I have used simple hex-coded color for the sky color, but you can also use an image for the sky background. After adding the sky your scene will look like this

![](https://hasanali.me/wp-content/uploads/2018/10/06-1024x586.png)

In this part, we’ve covered the basic syntax of A-Frame framework, made a simple scene with a box and add properties to it. In the next tutorial, we’ll look into the camera, lights and animations with their respective properties.

Complete code available on Github : [https://github.com/hasanaliqureshi/A-Frame-VR-Development/blob/master/A-Frame-VR-Development-Part-1.html](https://github.com/hasanaliqureshi/A-Frame-VR-Development/blob/master/A-Frame-VR-Development-Part-1.html)

Virtual Reality Pakistan Medium Publication : [https://medium.com/vr-pakistan](https://medium.com/vr-pakistan)

Virtual Realiy Pakistan Facebook Group : [https://www.facebook.com/groups/VirtualRealityPK/](https://www.facebook.com/groups/VirtualRealityPK/)