Build your own building blocks for WebVR using A-frame

If you’re interested in building virtual reality experiences for the web, then one of the most approachable ways to go about it is to use a framework like A-Frame. A-Frame makes it really easy to get started with WebVR, it’s an active, well-managed project with a vibrant and growing community, and it’s very fun to use and play around with.

What is A-Frame?

A-Frame is a web framework designed to make WebVR content creation easier, faster, and more accessible. A-frame was started by Mozilla, and has been around since mid-2015. From the A-frame documentation:

A-Frame lets you build scenes with just HTML while having unlimited access to JavaScript, three.js, and all existing Web APIs. A-Frame uses an entity-component-system pattern that promotes composition and extensibility. It is free and open source with a welcoming community and a thriving ecosystem of tools and components.

If you’ve never used it at all, here’s a super-minimal “Hello World” example. It uses a declarative HTML-type syntax as seen below:

See the Pen A-Frame super minimal example by Mark Lee (@leemark) on CodePen.

At the top there’s a link to the A-Frame script, then the <a-scene> element encompasses everything in the WebVR scene, the same way that the <body> element encompasses everything the uses sees in a web page.

Inside the scene there is an <a-box> element which makes–you guessed it–a box appear in the scene. The box has attributes that describe how it should appear, including its position within the scene, its rotation, and its color.

The <a-box> element is a built-in primitive provided by A-Frame. The rest of this post is going to focus on how to build your own reusable components that don’t already exist in A-Frame, how to build your own building blocks in a sense. If this is truly your first time using A-Frame I recommend that you play around with it a little bit to get a feel for it.

Here’s a starter CodePen you can fork and play with, or a Glitch project if you prefer (p.s. both CodePen and Glitch are awesome, and if you haven’t used them you should!). Then check out the introductory parts of the documentation especially the Introduction and Getting Started sections, and maybe even go through the Building a Basic Scene guide. Also here’s a great YouTube playlist of A-Frame tutorials.

Build Your Own Components

Once you’re up to speed with building basic scenes using built-in components and primitives like boxes, spheres, cylinders, etc. you will reach a point where you might want to build your own custom components that you can use in your projects, and even share for others in the community to use.

For a simple example, here’s a “clock” component showing the current time. Next we’ll walk through how it’s built, step-by-step.

See the Pen a-frame clock by Mark Lee (@leemark) on CodePen.

hint: click and drag around

Register Your Component

The first thing you need to do is let A-Frame know about your component by registering it. You do this with AFRAME.registerComponent(), as seen below:

AFRAME.registerComponent() takes two arguments, the first will be the name of your component, and the second is an object that represents your component itself, containing all of its properties and methods.

Component Lifecycle Methods

A-Frame provides a number of lifecycle methods for your component. These are built-in methods that will automatically get called at certain times, like when it is first initialized, or when its properties are changed. This is where you make your component “do stuff”. In this case we’ll be using the init() and tick() lifecycle methods.

First, we’ll take a look at init().

Hello World

Here’s how you can create a text element and add it to the scene (add it to the DOM).

See the Pen a-frame hello world component by Mark Lee (@leemark) on CodePen.

One great thing about A-Frame is, if you are a web developer and are used to working with the DOM, everything pretty much works the way you would expect. For example, creating an element, appending it to another element, and adding a text value, as seen below:

You can now add the Hello World text to the scene with <a-entity clock></a-entity>. Congratulations, you’ve now built your first component!

In the Codepen above I have also given the entity a position since otherwise it would be at the origin (0, 0, 0) position in the same place as the camera, which would make it hard to see.

Passing in properties

What’s the fun in having a reusable component if you’re only going to have one instance of it, or if all instances will be exactly the same? Let’s make it so that we can pass in some properties and customize each instance of the component.

To do that we will use the schema. The schema is an object that defines the properties for your component. In other words, the schema tells A-Frame what properties to expect to be passed in and what data types (or “property types”) those properties will be. It also lets you define default values in case any of those properties aren’t passed in.

This schema is saying that we can expect a position, a color, and a font to potentially be passed in, that the types of these properties will be vec3, color, and string respectively, and then setting a default value for each.

Once they are defined in the schema, we can access each of these properties via the component’s data object, allowing us to easily grab these values and set them for our component instance using setAttribute().

Now when you add the component to the A-Frame scene, you will pass in the position, color, and font like so:
<a-entity clock="font: sourcecodepro; color: #191; position: -1 1.5 -.75"></a-entity>

You can see how this all comes together in the Codepen:

See the Pen a-frame hello world component, with schema by Mark Lee (@leemark) on CodePen.

Make time for making time

We’re actually getting pretty close here, now that you know how to register your component, build your component by adding elements to the DOM, use the built-in lifecycle methods, and use schema to define and pass in properties.

Two things still need to be done in order to turn this Hello World example into a working clock component: we have to get the current time, and then update it regularly. First let’s add a getTime() method to our component that will get a JavaScript Date object and return it as a formatted time string.

Then we want to use the tick() lifecycle method to call getTime periodically and update the text element accordingly with the current time (tick() will be called every render loop, many times per second).

With those last two pieces in place, we’ve got a fully working and reusable a-frame clock component, which you can instantiate like so:

See the Pen a-frame clock by Mark Lee (@leemark) on CodePen.

“But wait,” you say, “what’s all this <a-entity> nonsense!? I want a proper <a-clock> element!”

In that case, what you want to do is register your brand-new component as a primitive.

Extra Credit: Registering a new Primitive

Primitives extend the <a-entity> as a new custom element, allowing us to instantiate our clock using <a-clock>. Here’s how it works:

And here’s the final Codepen, with everything put together, and three <a-clock>s added to the A-Frame scene:

See the Pen a-frame clock as primitive by Mark Lee (@leemark) on CodePen.

Resources & further reading

360: Exploring New Forms of Media

Over the past year or so I’ve been branching out from traditional web development and have become more and more interested in working with newer forms of digital media like Virtual Reality (VR), Augmented Reality (AR), and 360° video + photos which I want to focus on here.
Continue reading “360: Exploring New Forms of Media”

A New Generation of Campus Maps

Campus Map

“Anytime I feel lost, I pull out a map and stare. I stare until I have reminded myself that life is a giant adventure, so much to do, to see.”
— Angelina Jolie

conf-listing2014 has been a great year for getting out of my comfort zone, and in keeping with that theme, last month I gave my first-ever conference presentation with a couple of amazing colleagues at HighEdWeb 2014 in Portland, Oregon. I wish I could say that I nailed it, but honestly I tried to pack waaaay too much information into a limited amount of time, and was pretty nervous for the first 10 minutes or so. Still, it was well attended, we got some great questions and feedback afterward, and it was an exciting new experience that I would definitely recommend to anyone else looking to push out of their professional or personal comfort zone.

This presentation was about online maps—specifically higher-ed campus maps—and over the past several months I’ve collected a lot of information and resources that I’d like to share, in case it’s helpful to anyone else who is new-ish to online maps. Some of this will apply specifically to people working at colleges and universities, but there’s also some that could be useful to anyone interested in web-based mapping.

Before we jump in:

« here’s a link to the full slide deck ».

Continue reading “A New Generation of Campus Maps”

A better simple slideshow

Looking back in time by 900hp, on Flickr

If you don’t have time to do it right, when will you have time to do it over?
— John Wooden

It’s a do-over! This is another fairly basic slideshow, written in javascript, html, and css. This is a dual-purpose project, it’s meant (1) to be something you can drop right into your page and use if you so choose, but it’s also meant (2) as an example/tutorial showing you how to build a simple DIY slideshow from scratch on your own. You can see a couple of demos of the finished product here: Continue reading “A better simple slideshow”

Pulling JSON data from a public data API

“It is a capital mistake to theorize before one has data.” ― Arthur Conan Doyle

The Go Code Colorado crew, photo by Allison Daniell of Stellar Propeller Studio
Go Code Colorado photos by Allison Daniell of Stellar Propeller Studio

I had the opportunity recently to participate in Go Code Colorado, an “apps challenge” (think weekend hackathon + startup pitch competition) meant to make public data more accessible. It was an awesome experience and it got me thinking about government/public data, and how to use it and combine it in ways that can help people.

Here’s a quick example of how to pull data from one such public data source,  the Colorado Business Entity Database.   Basically, anyone starting a new business or forming a business entity in Colorado, whether that be a sole proprietership, partnership, LLC, corporation, etc, has to file documents and register their business with the Secretary of State’s office, and that information becomes public data. What we’re going to do in this example is to use state-provided API to pull a list of current businesses in a particular ZIP code, and list out their names, street addresses, and cities.
Continue reading “Pulling JSON data from a public data API”

Looking back, looking forward, and giving thanks

“Fearlessness is not only possible, it is the ultimate joy.
When you touch nonfear, you are free.”
— Thich Nhat Hanh

Happy New Year, and welcome to 2014! I’ve always liked the first part of a new year–it’s a good time to take stock, and take a little time to be mindful about where I’m going and what I’m doing. So this post will probably be a little more me-centric than usual, but hopefully there’s something of interest to you too–assuming you’re a fellow web designer, web dev, front-end developer, or just an all-around web geek :)

Looking back

2013 was a great year for me professionally, definitely one of my best so far. As I talked about in my very first post, I started 2013 in bit of a rut and like I was slipping behind the times a little in my profession. Now at the end of 2013 I’m much more up-to-date on skills, feeling rejuvenated, more engaged in the web community, and very excited for the future. All the people, projects, organizations, resources, and communities that have helped me get to this point are too numerous to list, but I want to call out a few that I’m particularly thankful for.
Continue reading “Looking back, looking forward, and giving thanks”

A simple DIY responsive image slideshow made with HTML5, CSS3, and JavaScript

Slide Projector by macattck, on Flickr

“It takes half your life before you discover life is a do-it-yourself project.” ― Napoleon Hill

IMPORTANT: I’ve written a new more robust slideshow, along with a code walkthough/tutorial. If you’re looking for a slideshow to use on your own site, it’ll be better and easier to use than this one. Go check it out:

A while back I wrote about a technique for building a simple automatically cycling slideshow using CSS animations, no JavaScript required. While that is definitely an interesting technique, it’s also pretty limited in how you can use it. For example, often with an image slideshow rather than just auto-advancing the slides you may want to let the user have control, so they can navigate forwards and backwards through the images at their own pace. These days you also may want a slideshow that’s responsive, so that it will work across a wide range of devices, automatically resize to fit different screen sizes, and maybe even allow some more touch-centric interaction–like swiping to the left or right instead of clicking “previous” and “next” buttons to cycle through the slides.

If this is what you need and you’re just looking for a drop-in solution, there’s no reason to reinvent the wheel, there are some great slideshow options out there already (here are just a few: Unslider, SwipeJS, SlidesJS). However if you’re more of the do-it-yourself type and you want to custom-build your own to suit your exact needs, or just want to see how it can be done, then it’s pretty easy to get started with just a little bit of HTML, CSS, and JavaScript.
Continue reading “A simple DIY responsive image slideshow made with HTML5, CSS3, and JavaScript”

CSS gradients: basic to advanced

“All that’s bright must fade, the brightest still the fleetest; All that’s sweet was made but to be lost when sweetest.” ― Thomas Moore

It used to be when you wanted a nice simple gradient background for your page, the process looked like this: Step 1) fire up Photoshop; Step 2) use the Gradient tool to fill the canvas with a horizontal or vertical gradient; Step 3) crop canvas into a as-small-as-possible slice (often 1px high or 1px wide); Step 4) export .gif or .jpg graphic; and so on. I’m leaving off the rest of the steps, because you either remember how to do it from past experience, or you’ll never need to know how to make a gradient that way thanks to the awesomeness of CSS gradients. Let’s take a look at what they can do! Continue reading “CSS gradients: basic to advanced”