Introduction
Note: This documentation is for the old 0.4.0 version of A-Frame. Check out the documentation for the current 1.7.0 version
Primitives are entities that:
- Have a semantic name (e.g.,
<a-box>) - Have a preset assemblage of components
- Have default component property values
- Map HTML attributes to component properties
Primitives abstract the core API to:
- Pre-compose useful components together with prescribed defaults
- Act as a shorthand for complex-but-common types of entities (e.g.,
<a-sky>) - Provide a familiar interface with HTML attributes mapping to only a single value
They are sort of like Prefabs in Unity. Some literature on the entity-component-system pattern refer to these as assemblages.
Example
Below is an assortment of primitives in use:
<a-scene> |
Primitives are Entities
Since every primitive extends <a-entity>s, primitives can do the same things
as entities such as:
- Positioning, rotating, and scaling
- Attaching components and mixins
- Applying animations
For example, let’s take <a-box> primitive, and say someone writes a
third-party physics component. We can attach it to <a-box> just as we would
with any entity:
<a-box color="red" physics="mass: 2.4"></a-box> |
How They Work
To create a wide red box using the primitives API, we could write:
<a-box color="red" width="3"></a-box> |
Which ends up expanding to:
<a-entity geometry="primitive: box; width: 3" material="color: red"></a-box> |
Under the hood, we see that primitives extend <a-entity> as a custom
element while providing some defaults. It defaults the geometry.primitive
property to box. And it maps (i.e., proxies) the HTML width attribute to
the underlying geometry.width property and the HTML color attribute to the
underlying material.color property.
Register a Primitive
We can compose and register our own primitives (i.e., register an element) for other people to easily use.
For example, here is what the registration looks like for <a-box> primitive:
var extendDeep = AFRAME.utils.extendDeep; |
For example, Don McCurdy’s aframe-extras creates <a-ocean>
primitive using his ocean component:
AFRAME.registerPrimitive('a-ocean', { |
Then we’d be able to create oceans using basic HTML syntax with little configuration needed:
<a-ocean color="aqua" depth="100" width="100"></a-ocean> |